简体   繁体   English

我需要使用ArrayList类对数组进行排列。 我不知道怎么了

[英]I need to make permutations with an array using the ArrayList Class. I can't figure out what's wrong

so im taking an ap comp sci class in school, and in the class we're learning about the basics of java. 因此,我在学校参加了ap comp sci课程,并且在课堂上我们学习了Java的基础知识。 For this assignment we have to make permutations by taking numbers from one one-dimensional array, and putting in another, then deleting that number so it can't be picked again. 对于此分配,我们必须进行排列,方法是从一个一维数组中获取数字,然后放入另一个维中,然后删除该数字,这样就无法再次选择它。 The numbers in the array can't repeat. 数组中的数字不能重复。 We have to use the ArrayList Class too. 我们也必须使用ArrayList类。 And I can't figure out what's wrong! 而且我不知道怎么了! This is the method that creates the permutations: 这是创建排列的方法:

 public static ArrayList<Integer> createPerm()
    {
        ArrayList<Integer> list = new ArrayList<Integer>(10);
        Integer x = 1, remove = 0;
        for (Integer i = 0; i < 10; i++)
        {
            list.add(x);
            x++;
        }

        ArrayList<Integer> perm = new ArrayList<Integer>(10);

        for(Integer i = 0; i < 10; i++)
         {
            Integer r = (int)(Math.random() * 10) + 1;
            for (Integer j = 0; j <= list.size() - 1; j++)
            {
                if (list.get(j) == r)
                {
                    remove = j + 1;
                    list.remove(remove);
                    perm.add(r);
                }
            }
        }

        return perm;

} }

I think that you (and I also:)) got a lttle bit confused because you are using Integer-objects as index and as list elements. 我认为您(而且我也:)感到有些困惑,因为您将Integer对象用作索引和列表元素。
That is no problem with the List.get method, because there is only one get method which is expecting an int and Java converts the Integer to int. List.get方法没有问题,因为只有一个get方法期望一个int,并且Java将Integer转换为int。
The problem is in your usage of list.remove(). 问题出在您使用list.remove()时。 There are two methods, one expects an object and one an int. 有两种方法,一种是期望对象,一种是int。
So if you pass an Integer object, the remove(Object) method is called. 因此,如果传递一个Integer对象,则将调用remove(Object)方法。 But you pass the index, not the r-matching object, so the remove method fails sometimes, because it is random if the element is in your list if remove was called before. 但是您传递的是索引,而不是r匹配对象,因此remove方法有时会失败,因为如果以前调用过remove,则该元素在列表中是随机的。 And if the method not fails, you have removed the element with the value of your index(+1), not the one who matches r. 如果该方法没有失败,那么您将删除具有index(+1)值的元素,而不是与r匹配的元素。

     for(Integer i = 0; i < 10; i++)
     {
        Integer r = (int)(Math.random() * 10) + 1;
        for (Integer j = 0; j <= list.size() - 1; j++)
        {
            if (list.get(j) == r)
            {
                remove = j + 1;
                list.remove(remove);//the main error is here you found 4 
                                    //on index 2 and removes 3 (because of +1)
                perm.add(r);
            }
        }
    }

The next thing is, that random can deliver the same number more than once, 接下来的事情是,随机数可以多次传递相同的数字,
so you should not loop only 10 times. 因此您不应只循环10次。 Loop until the list is empty. 循环直到列表为空。
I have corrected the code as below, the original lines are commented before the correction. 我已经更正了以下代码,在更正之前对原始行进行了注释。

    //for (Integer i = 0; i < 10; i++) {
    while (!list.isEmpty()) {
        Integer r = (int) (Math.random() * 10) + 1;
        for (Integer j = 0; j <= list.size() - 1; j++) {
            //if (list.get(j) == r) {
            if (list.get(j).equals(r)) {
                //remove = j + 1;
                remove = list.get(j);
                list.remove(remove);
                perm.add(r);
            }
        }
    }

And here I put the code somewhat more clearly, so that it is easier to read 在这里,我将代码更清楚一些,以便于阅读

public static ArrayList<Integer> createPerm() {
    ArrayList<Integer> list = new ArrayList<Integer>(10);
    for (int i = 0; i < 10; i++) {
        list.add(i+1);
    }

    ArrayList<Integer> perm = new ArrayList<Integer>(10);

    while (!list.isEmpty()) {
        int r = (int) (Math.random() * 10) + 1;
        for (int j = 0; j < list.size(); j++) {
            if (list.get(j).intValue() == r) {
                perm.add(list.remove(j));
            }
        }
    }

    return perm;
}

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

相关问题 我无法弄清楚比较数组索引有什么问题 - I can't figure out what's wrong with comparing array indexes 我无法弄清楚出了什么问题,出现了“类型已定义的错误” - I can't figure out what's wrong, getting a 'type already defined error' 我不知道怎么了,我的逻辑似乎正确 - I can't figure out what's wrong, my logic seems correct 我无法弄清楚我的NullPointerException有什么问题或者为什么它甚至存在 - I can't figure out what's wrong with my NullPointerException or why it even exists 项目Euler#23(Java)。 我不知道怎么了。 答案是64 - Project Euler #23 (Java). I can't figure out what's wrong. Answer is off by 64 解析错误; 我已经尝试过,无法弄清楚出了什么问题? - Parsing error; I have tried and can't figure out what's wrong? 我需要使此代码正常工作,但无法找出问题所在。 这是System.out.format()问题 - I need to make this code work, but cannot figure out what is wrong. It is a System.out.format() issue 无法弄清楚我的DocumentFilter有什么问题 - Can't figure out what's wrong with my DocumentFilter 我无法弄清楚此错误消息(ArrayList BankAccount代码) - I can't figure out this error message (ArrayList BankAccount code) 我无法弄清楚这个线程有什么问题,我试图理解多线程 - I can't figure out what is wrong with this threads, I am trying to understand multithreading
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM