0

I would like to know how to delete a randomly selected item from an arraylist and add that selected item to another empty arraylist.

An Additonal (Optional): I would like to also know how to compare elements in an ArrayList with another ArrayList. This one is a pain in my patuski.

1
  • Removed misc commentary Commented May 10, 2015 at 13:15

3 Answers 3

2

well to move an element from one ArrayList to the other you can do something like this:

ArrayList< SomeClass > firstList;
ArrayList< SomeClass > secondList;
int randomlySelectedIndex; //initialize this to be random
SomeClass element = firstList.get( randomlySelectedIndex );
firstList.remove( randomlySelectedIndex );
secondList.add( randomlySelectedIndex );

As for comparing elements from 2 lists, you could make a compare method like so:

int compare( SomeClass first, SomeClass second ) {
    //return 0, 1 or -1 depending on your criteria of how first relates to second
}

and then use the compare method when iterating through both lists

int result;
for( int x = 0; x < firstList.size(); x++ ) {
    for( int y = 0; y < secondList.size(); y++ ) {
        result = compare( firstList.get( x ), secondList.get( y ) );
        if( result == 0 ) {
            //do stuff
        }
        else if( result < 0 ) {
            //do stuff
        }
        else {
            //do stuff
        }
    }
}

please note that you should not be adding or removing elements from either of the arraylists from inside the 2 for loops unless you know the consequences of doing so.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this, I can work with this, I was skeptical on using a nested loop, but I guess I won't be able to avoid it
1

Something like this should do the trick

List<T> list;
List<T> emptyList;
...
Random rand = new Random();
int randomIndex = rand.getInt() % list.size();
T item = list.remove(randomIndex);
emptyList.add(item);

As far a comparing two lists, you should clarify what exactly you want to compare. Also, since the two questions are fairly different, you may want to ask it in a separate post.

1 Comment

Thanks for this, this makes sense, I will be able to incorporate this to Jack's Answer
1

You can remove object from the ArrayList by calling remove(int index) which returns the object that was removed. Then you add that object to the other ArrayList with add():

SomeObject removedObject = firstList.remove(randomNum);
secondList.add(removedObject);

Note: make sure that random number is from 0 to the size of your ArrayList.


You can compare objects of the same class by implementing Comparable interface in that class. See more: https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html

1 Comment

Thanks for this, I would try to avoid the Comparable Interface, but thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.