Results 1 to 9 of 9
  1. #1
    Ace
    Ace is offline Member
    Join Date
    Nov 2011
    Location
    Earth
    Posts
    16
    Rep Power
    0

    Question 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 :)"

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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!

  3. #3
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default 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.");
        }
    }

  4. #4
    Ace
    Ace is offline Member
    Join Date
    Nov 2011
    Location
    Earth
    Posts
    16
    Rep Power
    0

    Default 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?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  6. #6
    Ace
    Ace is offline Member
    Join Date
    Nov 2011
    Location
    Earth
    Posts
    16
    Rep Power
    0

    Default 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).

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: For-each loop & ArrayList collection - I am searching for better solution

    Quote Originally Posted by Ace View Post
    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).
    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.

  8. #8
    Ace
    Ace is offline Member
    Join Date
    Nov 2011
    Location
    Earth
    Posts
    16
    Rep Power
    0

    Default Re: For-each loop & ArrayList collection - I am searching for better solution

    It solved my problem, thanks :)

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: For-each loop & ArrayList collection - I am searching for better solution

    You're welcome, and look forward to seeing you around here in the future!

Similar Threads

  1. Searching and changing ArrayList
    By sinister in forum New To Java
    Replies: 36
    Last Post: 03-31-2011, 03:55 PM
  2. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  3. Searching an arraylist
    By adelgado0723 in forum New To Java
    Replies: 1
    Last Post: 04-15-2008, 02:09 PM
  4. Object ArrayList - increment solution needed badly!!
    By rugbyGeek in forum New To Java
    Replies: 4
    Last Post: 03-08-2008, 01:47 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •