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 7 of 7

Thread: Transferring elements between arraylists

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face Transferring elements between arraylists

    Ok so I have a problem thats slightly more complicated than the title suggests.

    I have to write a method that randomly selects objects from one ArrayList and adds them to another ArrayList. The objects have an integer instance variable, "type" that is limited to 1-4. The second ArrayList should contain at least one element of each type. After that the objects can be selected at random until the second arraylist contains a number of objects determined by the user.

    Can anyone suggest a relatively efficient way of doing this?
    I cant think of any way that doesnt involve multiple additional arraylists.

    P.S Sorry if I haven't phrased the problem particularly well, im a newbie to these forums


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Transferring elements between arraylists

    Ok, so tell me if I'm correct.

    ArrayList 1 contains Integer Objects ranging from 1 to 4.

    ArrayList 2 should contain at least one Integer Object for each number (from 1 to 4).

    Is that correct?

  3. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Transferring elements between arraylists

    Quote Originally Posted by aussiemcgr View Post
    Ok, so tell me if I'm correct.

    ArrayList 1 contains Integer Objects ranging from 1 to 4.

    ArrayList 2 should contain at least one Integer Object for each number (from 1 to 4).

    Is that correct?
    ArrayList 1 contains Objects with an Integer Variable "Type" that is limited to 1-4

    ArrayList 2 should contain a selection of objects from ArrayList 1, but it should contain at least one object with Type == 1, one with Type == 2 etc. Apart from that the objects can be selected randomly.

    Hopefully thats a bit clearer

  4. #4
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Transferring elements between arraylists

    It doesn't sound too hard. (but I'm not going to do your homework for you) Do you have any code, do you know how to generate a random number? If you have any code, then post it! (Don't forget to put it between [highlight=java]Code here[/highlight] tags )

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Transferring elements between arraylists

    To satisfy the 'must contain at least one' in the second array list, use the addAll function of List. From then on, you can randomize the selection and addition until the size is met. To reiterate brt93yoda's advice, posting a simplified version of code could help immensely in describing the problem and pointing you in the right direction.

  6. #6
    Junior Member
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Transferring elements between arraylists

    Quote Originally Posted by Brt93yoda View Post
    It doesn't sound too hard. (but I'm not going to do your homework for you) Do you have any code, do you know how to generate a random number? If you have any code, then post it! (Don't forget to put it between [highlight=java]Code here[/highlight] tags )
    Not looking for somone to do it for me, just a shove in the right direction

    I haven't got a problem with the random selection or adding from one list to the other its just checking that theres at least one of each type in the final list.

    This is what i've got so far...

    public ArrayList<Item> selectItems( ArrayList<Item> items, int numberOfElements ) {
     
    		ArrayList<Item> selection = new ArrayList<Item>();
     
    		Random rng = new Random(System.currentTimeMillis ( ));
     
    		while (selection.size ( ) < numberOfElements) {
     
    			// select a random element from items list
    			Item a = items.get ( rng
    			        .nextInt ( items.size ( ) - 1 ) );
     
    			// add to the final ArrayList
    			selection.add ( a );
     
    			// remove selected element to prevent duplicate entries in final
    			// list
    			items.remove ( a );
     
    		}
     
    		return selection;
     
    	}

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Transferring elements between arraylists

    Quote Originally Posted by KipTheFury View Post
    Not looking for somone to do it for me, just a shove in the right direction

    I haven't got a problem with the random selection or adding from one list to the other its just checking that theres at least one of each type in the final list.
    As I suggested above, use the addAll function of list
    selection.addAll(items);

    Which will add all the elemets from items into selection. This will guarantee at least one from item is in you new list. If you wish this addition to be randomized, you can use the Collections.shuffle function.

Similar Threads

  1. Help with Arrays - Counting elements
    By ShakeyJakey in forum Collections and Generics
    Replies: 7
    Last Post: August 8th, 2010, 04:09 PM
  2. accessing elements in a 2d ArrayList
    By Flamespewer in forum Collections and Generics
    Replies: 2
    Last Post: March 8th, 2010, 06:11 PM
  3. Method Adding elements to an array with certain restrictions
    By Newoor in forum Collections and Generics
    Replies: 1
    Last Post: December 13th, 2009, 11:13 AM
  4. [SOLVED] How to dynamically create ArrayLists (or something similar)?
    By igniteflow in forum Collections and Generics
    Replies: 4
    Last Post: August 6th, 2009, 06:00 AM
  5. Java program to encrypt an image using crypto package
    By vikas in forum Java Networking
    Replies: 1
    Last Post: July 7th, 2009, 11:00 AM