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
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?
Re: Transferring elements between arraylists
Quote:
Originally Posted by
aussiemcgr
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
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 :D)
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.
Re: Transferring elements between arraylists
Quote:
Originally Posted by
Brt93yoda
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 :D)
Not looking for somone to do it for me, just a shove in the right direction b-(
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...
Code java:
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;
}
Re: Transferring elements between arraylists
Quote:
Originally Posted by
KipTheFury
Not looking for somone to do it for me, just a shove in the right direction b-(
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
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.