Results 1 to 12 of 12
- 12-14-2012, 10:24 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 4
- Rep Power
- 0
- 12-15-2012, 12:06 AM #2
Re: Is There a way to Only Execute A line Of Code once?
You didn't bother to reply to the two responses in the first thread you started. Why should we expect any better behavior this time round?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-15-2012, 12:13 PM #3
Member
- Join Date
- Aug 2012
- Location
- Switzerland
- Posts
- 49
- Rep Power
- 0
Re: Is There a way to Only Execute A line Of Code once?
I would do so
so the first time the boolean will be false, and the code will be executed, and the other times the boolean will be true, and the code will be skipped ;)Java Code:boolean check = false; public void onlyOnce(){ if(check == false){ //your code here } check = true; }
- 12-15-2012, 02:14 PM #4
- 12-15-2012, 02:19 PM #5
Member
- Join Date
- Aug 2012
- Location
- Switzerland
- Posts
- 49
- Rep Power
- 0
- 12-15-2012, 03:38 PM #6
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Is There a way to Only Execute A line Of Code once?
You can simply write your code like:
Java Code:if (!check) { ... }Website: Learn Java by Examples
- 12-15-2012, 06:55 PM #7
Member
- Join Date
- Aug 2012
- Posts
- 4
- Rep Power
- 0
- 12-15-2012, 07:10 PM #8
Member
- Join Date
- Aug 2012
- Posts
- 4
- Rep Power
- 0
Re: Is There a way to Only Execute A line Of Code once?
Thanks for the help guys but i should have been clearer in my original post, the problem that I am having is that within A method I need to create an ArrayList, the only problem is that when i call the method more then once it continually creates a new array list resetting the size of the array list to null each time, i need to be able to call the method without it clearing the array list so it can hold the values of the list each time when the method is called.
- 12-16-2012, 12:22 AM #9
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Is There a way to Only Execute A line Of Code once?
You can just convert the code above and use it in your scenario. You just need a condition when you will create the ArrayList and when not. For example check if the ArrayList is null or not. By knowing this condition you know whether you need to instantiate the ArrayList or not.
Website: Learn Java by Examples
- 12-16-2012, 12:25 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Is There a way to Only Execute A line Of Code once?
I have trouble understanding this.within A method I need to create an ArrayList, the only problem is that when i call the method more then once it continually creates a new array list
You say you "need" (=="have freely decided") to create an array list, but then say that it is problematic that the method does this very thing.
Perhaps you meant something like that you intend to create an array list under certain circumstances and not every time the method gets called, in which case that's precisely what you should do. In the discussion above these "certain circumstances" were illustrated with a boolean variable check, but anything your method can check will do: a common case is to check the value of the variable and only create an array list if it is null.
If this is all a bit vague, consider posting a code example: something brief, but compilable and runnable. Describe both what it does and what you intend its behaviour to be.
- 12-16-2012, 11:20 AM #11
Member
- Join Date
- Aug 2012
- Location
- Switzerland
- Posts
- 49
- Rep Power
- 0
Re: Is There a way to Only Execute A line Of Code once?
I think that if he surrounds the declaration of the arraylist with an if statement, any time he will try to use it outside the if statement he will get an error, cause the program don't know if the arraylist will exist or not (I'm not totally sure of what I'm saying, but trying I obtained this result)
EDIT: can't you simply create the arraylist outside the method?Last edited by Chri; 12-16-2012 at 01:06 PM.
- 12-16-2012, 05:51 PM #12
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Is There a way to Only Execute A line Of Code once?
The ArrayList can be declared as an instance variable of the class and the method will only contains the instantiation process of the ArrayList.
Website: Learn Java by Examples
Similar Threads
-
Code Won't Execute, Help Apreciated
By danielinthebed in forum NetworkingReplies: 2Last Post: 01-01-2012, 12:00 AM -
how to execute a code after the endless loops?
By Jhovarie in forum Threads and SynchronizationReplies: 4Last Post: 07-24-2011, 04:21 AM -
Constructors pls explan the code line by line in comments
By vibaviattigala in forum New To JavaReplies: 1Last Post: 02-19-2011, 04:03 AM -
Unable to execute my code can u plz help me out
By Gayathri12 in forum New To JavaReplies: 6Last Post: 07-21-2010, 08:28 AM -
Execute code from ActionListener
By Viola in forum New To JavaReplies: 3Last Post: 05-29-2010, 05:10 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks