Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Arraylist get problem

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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:
    1 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
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Arraylist get problem

    So the declaration of your ArrayList<Lot> should look something like:

    List<Lot> lotList = new ArrayList<Lot>();

    Then the lotList is filled with Lot objects:

    lot1 = new Lot();
    lotList.add( lot1 );

    And somewhere in the process of creating an instance of a Lot object or subsequently, the Lot object's field lotNumber is defined. Then to obtain the lotNumber fields of the ArrayList<Lot> lotList, lotList could be iterated or each element of lotList obtained, but an iteration would look like
    for ( Lot lot : lotList )
    {
        System.out.println( "The Lot Number is: " + lot.getLotNumber() );
    }
    Hope this helps. Good luck.

Similar Threads

  1. ArrayList problem, probably very simple.
    By tomtensfarfar in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 4th, 2013, 06:16 PM
  2. ArrayList problem
    By Renhik in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 8th, 2012, 11:48 AM
  3. ArrayList Sorting Problem
    By coke32 in forum Object Oriented Programming
    Replies: 3
    Last Post: April 29th, 2012, 08:53 AM
  4. Problem with ArrayList
    By waltersk20 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 26th, 2010, 08:37 PM
  5. ArrayList Problem
    By Marty in forum Collections and Generics
    Replies: 16
    Last Post: August 31st, 2010, 03:47 AM