Results 1 to 9 of 9
- 11-18-2011, 04:33 PM #1
Member
- Join Date
- Nov 2011
- Location
- Earth
- Posts
- 16
- Rep Power
- 0
For-each loop & ArrayList collection - I am searching for better solution
Hi :)
I am first year student of Computer Science. Recently I was doing a small project for Java programming exercises. My lecturer wanted me to do the library class using the ArrayList collection and for-each loop anywhere I could implement it. I have done it properly but I am not satisfied with this solution. I have one for-each loop which is executing till the end of the list. I would like to add some conditions to stop it earlier. Do I have to implement own iterator? Thanks for your time. :)
Sorry for my English - I'm not native :)"
-
Re: For-each loop & ArrayList collection - I am searching for better solution
I believe that a break statement would exit the loop if desired.
p.s.: your English is fine, and in fact better than most.
p.p.s.: and welcome to the Java-Forums.org!
- 11-18-2011, 04:44 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 49
- Rep Power
- 0
Re: For-each loop & ArrayList collection - I am searching for better solution
You can use the break statement to break a loop once a condition is met, here is an example
Java Code:class Foo { public static void main(String args[]) { for(int i=0; i<100; i++) { if(i == 10) break; // terminate loop if i is 10 System.out.println("i: " + i); } System.out.println("Loop complete."); } }
- 11-18-2011, 05:09 PM #4
Member
- Join Date
- Nov 2011
- Location
- Earth
- Posts
- 16
- Rep Power
- 0
Re: For-each loop & ArrayList collection - I am searching for better solution
Fubarable - thanks for your words!
About given solution: I knew about the break statement, but I wanted to avoid this, because I'm trying to do structural loops :) (My lecturer hates the break, return statements in the loops :| I'm not that hater, but when I think about 1000 lines of code than I can agree with him). Is there other way?
-
Re: For-each loop & ArrayList collection - I am searching for better solution
The for-each construct is somewhat limited since there is no boolean check available where you can add additional logic, such as is available in a standard for loop or while loop. It is built for iterating through each item in a collection, and that's it. If you want more control, you'll need to use a different loop I think.
- 11-18-2011, 05:32 PM #6
Member
- Join Date
- Nov 2011
- Location
- Earth
- Posts
- 16
- Rep Power
- 0
Re: For-each loop & ArrayList collection - I am searching for better solution
Thank you, Fubarable :) I have one more question: is there a simple way to check if specified element exists in the ArrayList<Book>? I have to check it by Book's field "title". I wonder if there is some ability of collections which would avoid making loops for checking searched elements :D (I readed ArrayList documentation and I think that I must do it with own loop).
-
Re: For-each loop & ArrayList collection - I am searching for better solution
If the type held by the ArrayList has its own equals and hashCode override methods, then you can see if the collection has the desired item via ArrayList's contains(...) method. Otherwise, either iterate through the collection with a for loop, or use a different type of collection such as a HashMap where the String of interest is held in the Map's key.
- 11-19-2011, 12:26 AM #8
Member
- Join Date
- Nov 2011
- Location
- Earth
- Posts
- 16
- Rep Power
- 0
Re: For-each loop & ArrayList collection - I am searching for better solution
It solved my problem, thanks :)
-
Similar Threads
-
Searching and changing ArrayList
By sinister in forum New To JavaReplies: 36Last Post: 03-31-2011, 02:55 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
Searching an arraylist
By adelgado0723 in forum New To JavaReplies: 1Last Post: 04-15-2008, 01:09 PM -
Object ArrayList - increment solution needed badly!!
By rugbyGeek in forum New To JavaReplies: 4Last Post: 03-08-2008, 12:47 AM


LinkBack URL
About LinkBacks


Bookmarks