Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Arrow Moving records from one Arraylist to another?

    Hi,

    I've got two ArrayLists;

    • Cars Sold
    • Cars Available


    How do I 'move' the data about a car from the 'Cars Available' to 'Cars Sold'?

    I know that this may sound very simple but I've never come across this :(

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    I'm assuming both arraylists are within the class:
    Java Code:
    public boolean sellCar(Car c) {
        if(carsAvailable.contains(c)) {
               carsAvailable.remove(c);
               carsSold.add(c);
               return true;
        }
        else return false;
    }

  3. #3
    ess
    ess is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default

    This code can be optimized:

    Java Code:
    public boolean sellCar(Car c) {
        if(carsAvailable.remove(c)) {
               carsSold.add(c);
               return true;
        }
        else return false;
    }

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Oh yeah, I forgot the remove method returns a boolean. Good catch!

  5. #5
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

Similar Threads

  1. insert records
    By rudravaram in forum JDBC
    Replies: 3
    Last Post: 01-03-2010, 05:54 PM
  2. Moving Box
    By anilanar in forum New To Java
    Replies: 2
    Last Post: 08-30-2009, 12:29 PM
  3. How to delete the records
    By kiran kumar in forum Java Servlet
    Replies: 6
    Last Post: 11-09-2008, 01:16 PM
  4. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  5. Number of updated records
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 04:32 PM

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
  •