Results 1 to 5 of 5
  1. #1
    Java-Guy is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Question Adding items from one Array List to another Array List

    I'm teaching myself java, and need to fill in an incomplete program.
    In this case, I'm having trouble adding items from an ArrayList called Integer, to an ArrayList called Car. My problem is filling in line 23 from the Car class.
    Here are the two classes:

    The book is from the library, so the answer CD is long gone, and the publisher doesn't support it any more.
    Any ideas?
    Last edited by Java-Guy; 01-19-2011 at 09:55 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    Have a look at this:

    Java Code:
    List<Apple> apples;
    List<Pear> pears;
    Do you think you can add an item from one list to the other list?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Java-Guy is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    So is there a way to convert it from on type to the other?
    I guess I could use arr.get for each item in the array and put them into some variable, and then write those to the other array. Is there a better solution?
    Thanks.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Java-Guy View Post
    So is there a way to convert it from on type to the other?
    I guess I could use arr.get for each item in the array and put them into some variable, and then write those to the other array. Is there a better solution?
    Thanks.
    You can't put an apple in a list of pears (or vice versa), not even if you stick it in array first. There's something wrong with your design and you have to think again why you want to do this. Or you have to elaborate on your question because as I understand it now you want to use apples for pears.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

Similar Threads

  1. Adding \ removing items from Jlist
    By Desperado in forum AWT / Swing
    Replies: 10
    Last Post: 12-17-2009, 12:48 PM
  2. Null Pointer Exception when adding items to ArrayList
    By ShadowCopy in forum New To Java
    Replies: 3
    Last Post: 08-27-2009, 09:23 AM
  3. Adding existing items to a project
    By Katherine in forum Eclipse
    Replies: 4
    Last Post: 01-20-2009, 04:23 AM
  4. Adding recieved items to Server repository.
    By nel636 in forum New To Java
    Replies: 1
    Last Post: 11-28-2008, 01:46 PM
  5. Adding items to a jComboBox
    By tronovan in forum New To Java
    Replies: 0
    Last Post: 08-08-2007, 08:48 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
  •