It's not what your program can do, it's what your users do with the program.
SCJP
Visit my download page
Sterling Crapser wrote:So with the Iterator there is no way to stop looping once you find a match? This is my impression. Or does it stop looping automatically? Is re-instantiating the Iterator for each use of the same ArrayList a bad practice? Has anyone ever had the need to use the same array with the same data more than once? It seems to be impossible to find anyone talking about this situation on the Internet. I find things like how to loop through the array only once using different approaches (Iterater, FOR loop, etc.), how to clear a loop for reuse...things like that but nobody is asking how to use the same array with the same data over and over again. I find this strange.
Henry Wong wrote:
Sterling Crapser wrote:So with the Iterator there is no way to stop looping once you find a match? This is my impression. Or does it stop looping automatically? Is re-instantiating the Iterator for each use of the same ArrayList a bad practice? Has anyone ever had the need to use the same array with the same data more than once? It seems to be impossible to find anyone talking about this situation on the Internet. I find things like how to loop through the array only once using different approaches (Iterater, FOR loop, etc.), how to clear a loop for reuse...things like that but nobody is asking how to use the same array with the same data over and over again. I find this strange.
The iterator doesn't do any looping. Your loop that uses the iterating is the thing that does the looping. So, if you want to stop looping, have your program terminate the loop -- and what's wrong with throwing away the iterator? It has done its job. Why keep it around?
Henry
Sterling Crapser wrote:I have an application I'm building that will make extensive use of ArrayLists (using the List Collection). The ArrayLists will be implemented in nested loops so they will need to be cycled through repeatedly. At least three of the ArrayLists will be of type (object) and loaded with a custom object containing local variables with the necessary setters and getters.
E Armitage wrote:Can't you remove nested looping by creating POJOs that represent your database tables entirely?
It is not so much a case of keeping it around as you ought to throw Iterators away. They should be declared as loop variables whose scope finishes when the loop completes. Otherwise you may suffer an Exception.Henry Wong wrote: . . . and what's wrong with throwing away the iterator? . . . Why keep it around?
Henry
Randall Twede wrote:the best way IMO is the "for each". i'm not sure when it came out. java 7, java 6 i'm not sure. it is a big improvement over both.
Sterling Crapser wrote:Probably...but since I'm literally starting to develop Java code as a beginner I wouldn't have the first clue how to do it.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Sterling Crapser wrote:Now here's another doozy...I'm being asked to store the names of methods in a table, then later in the code retrieve those methods I need to do some processing. But now the method names are within variables. Is it possible to execute a method as a variable?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Sterling Crapser wrote:So with the Iterator there is no way to stop looping once you find a match? This is my impression. Or does it stop looping automatically? Is re-instantiating the Iterator for each use of the same ArrayList a bad practice? Has anyone ever had the need to use the same array with the same data more than once? It seems to be impossible to find anyone talking about this situation on the Internet. I find things like how to loop through the array only once using different approaches (Iterater, FOR loop, etc.), how to clear a loop for reuse...things like that but nobody is asking how to use the same array with the same data over and over again. I find this strange.
I think the FOR EACH seems best. I would get the size of the array to determine the max number of loops and when I find a match, exit the loop and return the object. I could put all this code in a method and call that method each time I cycle the main loop that's parsing the XML file for the next data value. I could pass it the XML data value and have it return the object in the arrayList that has the match.
Does this sound right? I think one of my problems is thinking there is a best way to do anything in Java. My experience so far shows there are many solutions to the same problem...some better than others but they all will work more or less. When you don't know a lot about Java, it's very easy to get confused by all the different answers.
Campbell Ritchie wrote:List<Object> ???
List<String> and Iterator<String> please, then you can get rid of that nasty cast.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |