• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on Iterators vs. For Loops  RSS feed

 
Sterling Crapser
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. These ArrayLists will, in effect hold the contents of three database tables.

I will be parsing data from an XML file using a StAX parser and checking the node name against one of the ArrayLists via a loop. If I find a match, there will be processing of the ensuing data for that node. Once finished, I get the next XML node data value and start again.

I'm accustommed to using an array index in a FOR loop. The Iterator in Java appears to be designed for one-shot usage. Can I set up an array index or must I use the Iterator? It appears I would need to re-instantiate the Iterator for each loop cycle if I don't use the array index approach.

Thanks
 
Joe Areeda
Ranch Hand
Posts: 334
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sterling,

My favorite approach is called enhanced for loops but both iterators and indexed access do work.

The enhanced thing looks like:


If you are looking at only the root node to decide which object to use, a HashMap or TreeMap might save you a lot of time and effort.

Joe
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


Okay...so the Iterator is not a loop. Thank you for pointing that out...I am new to this language. I just looked at my code again and realized the Iterator is in a WHILE loop! I know I'm in over my head so pardon my distraction. At least I learned something today.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Can't you remove nested looping by creating POJOs that represent your database tables entirely?
 
Sterling Crapser
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage wrote:Can't you remove nested looping by creating POJOs that represent your database tables entirely?


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. If you read some of my other threads you will get the gist of my situation. I've been pushed into Java programming with the only experience being that of a PowerBuilder developer (16 years). And the program I'm tasked to develop is insanely complicated (probably much easier ways to do what the company wants but I cannot force them to go in another direction). So I'm cutting my teeth on some very harsh experiences. Not the best way to learn programming I suppose.

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?

Trial by fire at its finest!
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote: . . . and what's wrong with throwing away the iterator? . . . Why keep it around?

Henry
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.
 
Sterling Crapser
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get that. I also figured out how to invoke a set of methods based on data retrieve from a table to drive which methods to call. I will use a SWITCH statement. The table will contain the name of the necessary method and an ID (int). The int value will be used in the SWITCH to invoke its associated method. Any necessary parameters can be loaded and passed at that time as well.

Happy Thanksgiving folks!
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


1.5
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

And there, I suspect, is your problem. You're too focused on HOW you're going to do it. My suggestion would be to back up and describe, in detail, WHAT you need to do...and possibly also WHY you need to do it.

For example: What do these nested loops represent? It sounds like there is some underlying structure to the data that you're reading/parsing/extracting, but we (or I) are still in the dark as to what that is.

In the very first sentence of your OP you say "I have an application I'm building that will make extensive use of ArrayLists", which sounds to me like you've leapt to an implementation before you've got a design - or at the very least, you haven't explained it to us.

XML data, by its nature, is hierarchical, but Lists are sequential so, while there's nothing to stop you storing your results in a List, if you base your entire solution around sequential logic, it's unlikely to be optimal.

This thread appears to be a follow-up to this one, where some of us tried to explain that you might be able to use polymorphism to solve your problem. And what I see here simply confirms that suspicion even more.

HIH

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Sure, but I'd think long and hard before you do it. Java even has a Method class (java.lang.reflect.Method) that you can use and execute reflectively. But Java reflection is cumbersome, error-prone and difficult to test, and personally I'll go a long way to avoid it if I can.

Again, I would back up and describe WHAT this logic is intended to do, in detail, because there are likely to be many ways you could implement it, and the folks here may well be able to steer you in the right direction.

HIH

Winston
 
Ashwani Singh
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



You can break the loop when you find the match. For example:

List<Object> mylist=new ArrayList<Object>();
Iterator myiter=mylist.iterator();
while(myiter.hasNext())
{
String obj=(String)myiter.next();

if( //// )
break;
}


So you can break from the while loop if that condition satisfies.


Thanks.




 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List<Object> ???

List<String> and Iterator<String> please, then you can get rid of that nasty cast.
 
Ashwani Singh
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:List<Object> ???

List<String> and Iterator<String> please, then you can get rid of that nasty cast.


Yes,you are right.To avoid casting,we need to use string type list as well as iterator.Thanks Campbell for the correction.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!