Results 1 to 8 of 8
  1. #1
    Maarten is offline Banned
    Join Date
    Dec 2011
    Location
    Utrecht
    Posts
    26
    Rep Power
    0

    Default Arraylist get problem

    Hello,
    Im studying the book programming in BlueJ (Barnes/Kolling 5th ed.) and i am stuck at question 4.51.

    IŽll try to explain:
    There is an arraylist and I must get an object with a specific number, not an indexnumber, but a number which is one of the fields of the objects in the arraylist.

    So the header of the method I am trying to write is:

    Java Code:
    public Lot getLot(int lotNumber)
    but the number is an attribute of the objects (Lots) in the arraylist, not the indexnumber. I have no idea how to get the right Lot out of the arraylist. I only know how to access an object in an arraylist via its indexnumber. I hope you can help, I can provide more code from this project if neccesary.
    thanks, Maarten.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Arraylist get problem

    how to get the right Lot out of the arraylist
    If you have a variable with a value that you want to use to search a list for an object in that list with a field holding that value, use a loop to access every object in the list and call a method in the object to match the value in the variable with the field in the object.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Maarten is offline Banned
    Join Date
    Dec 2011
    Location
    Utrecht
    Posts
    26
    Rep Power
    0

    Default Re: Arraylist get problem

    Thank you, I solved it using a while loop.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Arraylist get problem

    You might want to investigate also;

    - using a for-each loop
    - using an Iterator (old-fashioned)

    For learning purposes. Probably the for-each loop will appeal you the most; the Iterator approach becomes interesting when you want to loop through a collection to find an element to remove.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: Arraylist get problem

    If you're searching for an element in an array with a specific member variable value, remember to consider the condition where there are more than 1 item satisfies the search.

  6. #6
    Maarten is offline Banned
    Join Date
    Dec 2011
    Location
    Utrecht
    Posts
    26
    Rep Power
    0

    Default Re: Arraylist get problem

    So a normal while loop is not usable when there could be more than one element with a certain variable value. Is it best to use an Iterator in that context?

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,034
    Rep Power
    6

    Default Re: Arraylist get problem

    It depends on the condition of the while loop. If the condition is used to match a particular aspect of an item and there are more than one such items in a list, then the while loop will terminate when the condition is first met. But the condition of a while loop may also be a hasNext() of an iterator or a simple index update. In general, while loops may be used like for loops and for loops like while loops. Here is an example of the latter.

    Java Code:
    int count = 0;
    for(;count < 10;) {
     // do some stuff
     count++;
    }
    It's ugly, but it works.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Maarten is offline Banned
    Join Date
    Dec 2011
    Location
    Utrecht
    Posts
    26
    Rep Power
    0

    Default Re: Arraylist get problem

    So it is not as black/white as I suggested, thought so. Thank you for your post, helps me getting more feel for the different loops.

Similar Threads

  1. Problem with ArrayList's
    By Teedo in forum New To Java
    Replies: 18
    Last Post: 04-21-2012, 05:00 PM
  2. ArrayList problem
    By sharingan90 in forum New To Java
    Replies: 5
    Last Post: 12-17-2011, 02:10 PM
  3. Replies: 12
    Last Post: 07-07-2011, 09:49 PM
  4. Problem in ArrayList
    By justbeller in forum New To Java
    Replies: 8
    Last Post: 02-15-2011, 04:19 PM
  5. ArrayList problem
    By khamuruddeen in forum New To Java
    Replies: 7
    Last Post: 12-22-2007, 06:46 AM

Posting Permissions

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