简体   繁体   English

从ArrayList获取随机元素,添加到另一个ArrayList并从第一个ArrayList Android中删除该元素

[英]Get a random element from ArrayList, add to another ArrayList and delete the element from first ArrayList Android

I would like to know how to delete a randomly selected item from an arraylist and add that selected item to another empty arraylist. 我想知道如何从arraylist中删除一个随机选择的项目,并将该选择的项目添加到另一个空的arraylist。

An Additonal (Optional): I would like to also know how to compare elements in an ArrayList with another ArrayList. 附加(可选):我还想知道如何将ArrayList中的元素与另一个ArrayList进行比较。 This one is a pain in my patuski. 这是我的patuski的痛苦。

well to move an element from one ArrayList to the other you can do something like this: 很好地将元素从一个ArrayList移到另一个,您可以执行以下操作:

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: 至于比较2个列表中的元素,您可以制作一个compare方法,如下所示:

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 然后在遍历两个列表时使用compare方法

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. 请注意,除非您知道这样做的后果,否则不要在2个for循环内的任何一个arraylist中添加或删除元素。

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. 另外,由于两个问题完全不同,因此您可能需要在单独的帖子中提问。

You can remove object from the ArrayList by calling remove(int index) which returns the object that was removed. 您可以通过调用remove(int index)ArrayList删除对象,该方法返回被删除的对象。 Then you add that object to the other ArrayList with add() : 然后使用add()将该对象添加到另一个ArrayList

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

Note: make sure that random number is from 0 to the size of your ArrayList . 注意:确保随机数从0到ArrayList的大小。


You can compare objects of the same class by implementing Comparable interface in that class. 您可以通过在该类中实现Comparable接口来比较同一类的对象。 See more: https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html 查看更多: https : //docs.oracle.com/javase/tutorial/collections/interfaces/order.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM