Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从arraylist中获取随机项而不在android中重复_Java_Android - Fatal编程技术网

Java 如何从arraylist中获取随机项而不在android中重复

Java 如何从arraylist中获取随机项而不在android中重复,java,android,Java,Android,我有一个对象的ArrayList,我想从中获取特定位置的项目,但每次启动活动时,检索到的位置都应该是随机的,并且在完全检索到每个位置项目之前不会重复。我用了这个方法: public static int getRandomNumber(ArrayList<Integer> arr) throws IllegalArgumentException { try { Random random = new Random();

我有一个对象的
ArrayList
,我想从中获取特定位置的项目,但每次启动活动时,检索到的位置都应该是随机的,并且在完全检索到每个位置项目之前不会重复。我用了这个方法:

public static int getRandomNumber(ArrayList<Integer> arr)
            throws IllegalArgumentException {
        try {
            Random random = new Random();
            int select = random.nextInt(arr.size());
            int randomnum = arr.get(select);
            GlobalData.randList.remove(select);
            return randomnum;
        } catch (IllegalArgumentException e) {

            for (int i = 0; i < arr.size(); i++) {

                GlobalData.randList.add(i);

            }
            return 0;
        }
public static int getRandomNumber(ArrayList arr)
抛出IllegalArgumentException{
试一试{
随机=新随机();
int select=random.nextInt(arr.size());
int randomnum=arr.get(选择);
GlobalData.randList.remove(选择);
返回随机数;
}捕获(IllegalArgumentException e){
对于(int i=0;i

但它不起作用,就像重复号码即将出现一样,这可能是有原因的,因为每次我重新启动活动时。我都是在
oncreate
中完成的,而不是
onResume
,但它并没有像我预期的那样起作用?有没有其他方法来处理它?有什么解决方案?

使用
Collections.shuffle()
对数组进行洗牌。使用另一个变量跟踪数组中的当前位置。每次检索新值时,该变量都会递增。到达数组末尾后,重新洗牌

参考:

公共类随机数组{
ArrayList数组=null;
int位置=0;
公共随机数组(ArrayList arr){
数组=arr;
位置=阵列尺寸();
}
公共int getNext(){
if(position==array.size()){
位置=0;
集合。洗牌(数组);
}
返回数组.get(position++);
}
}

如果您不在乎原始订单,可以尝试以下方法:

Object[] array = new Object[10];    // say 10 objects
int remain = array.length;
Random rnd = new Random();

public Object next () {
    if (remain == 0) {
        return null;
    } else {
        int i = rnd.nextInt(remain--);
        Object tmp = array[i];
        array[i] = array[remain];
        array[remain] = tmp;
        return tmp;
    }
}
您也可以使用ArrayList执行类似的操作


这样,它比shuffle()方法快,时间复杂度为O(n),而我的代码是O(1).

使用Collections.Shuffleca你能解释一下吗?在完成每个职位之前不会重复吗?评论很简短,因为我在过去几周内已经多次给出了这个答案。;)我使用了你的系统,但它重复了:(是的,它会在数组中的每个元素显示一次后重复。如果数组中有重复项,它也会重复。在获取一个对象后,我正在更改活动,如果我再次返回此活动,我如何继续?您可以在单独的类中编写随机代码,并使方法保持静态。
Object[] array = new Object[10];    // say 10 objects
int remain = array.length;
Random rnd = new Random();

public Object next () {
    if (remain == 0) {
        return null;
    } else {
        int i = rnd.nextInt(remain--);
        Object tmp = array[i];
        array[i] = array[remain];
        array[remain] = tmp;
        return tmp;
    }
}