在 toString 方法中返回 ArrayList 时,排列列表会重复自身 [英] Permutations list repeating itself when returning ArrayList in toString method

查看:21
本文介绍了在 toString 方法中返回 ArrayList 时,排列列表会重复自身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

*SOLVED:没有清除 ArrayList ebwteen 调用,所以列表一直在增加.我在 nextPermutation() 的第二个 for 循环之前添加了 perm.clear(),问题解决了!感谢所有帮助过的人.

*SOLVED: Wasn't clearing out the ArrayList ebwteen calls, so the list kept incrementing. I added perm.clear() before the 2nd for loop in nextPermutation(), and problem solved! Thanks to all who helped.

我无法在 toString 方法中返回 ArrayList.我的任务是从数字 1-10 生成 10 个排列列表.方法 nextPermutation 在驱动程序中被调用 10 次.

I'm having trouble returning an ArrayList within a toString method. My assignment is to generate 10 lists of permutations from the number 1-10. The method nextPermutation is called 10 times in the driver.

我希望我的输出看起来像这样:

I want my output to look like this:

List 1: [4 6 8 1 9 7 10 5 3 2]
List 2: [6 8 1 7 3 4 9 10 5 2]

等等.

相反,每个新列表在添加新排列时都保留上一个列表的排列,所以我得到了这个:

Instead, each new list keeps the permutation from the last list while adding a new permutation, so I get this:

List 1:  [6, 2, 3, 9, 4, 5, 10, 8, 7, 1]
List 2:  [6, 2, 3, 9, 4, 5, 10, 8, 7, 1, 3, 7, 2, 9, 1, 10, 6, 4, 8, 5]

这是我的代码:

import java.util.*;

public class PermutationGenerator {
    Random rand;
    ArrayList<Integer> perm = new ArrayList<Integer>();

    public PermutationGenerator(int s) {
        rand = new Random(s);
    }

    public void nextPermutation() {
        ArrayList<Integer> myArrayList = new ArrayList<Integer>();
        for (int i = 1; i <= 10; i++) {
            myArrayList.add(i);
        }

        for (int i = 1; i <= 10; i++) {
            int a = rand.nextInt(myArrayList.size());
            perm.add(myArrayList.get(a));
            myArrayList.remove(a);
        }
    }

    public final String toString() {
        return perm.toString();
    }
}

通过将 void nextPermutation() 方法更改为 ArrayList,在方法内部返回 perm,然后从字段中取出 ArrayList 并将其放置在 nextPermutation 方法中,我能够获得正确的输出.但是,对于赋值,我需要将 nextPermutation 设为无效,并且我还需要一个 toString 方法.

I was able to get the correct output by changing the void nextPermutation() method to ArrayList, returning perm inside the method, and taking out the ArrayList from the field and placing it in the nextPermutation method. However, for the assignment I need nextPermutation to be void and I also need a toString method.

所以,我的问题是,如何在 toString 中返回 perm 并为每个列表获得一个排列?

So, my question is, how do I return perm within toString and get one permutation per list?

推荐答案

要么你需要在每次打印之后和下次调用 nextPermutation() 之前清除 perm,或者有 nextPermutation() 创建并返回一个列表,该列表被打印然后丢弃.

Either you need to clear perm each time after printing it and before the next call to nextPermutation(), or have nextPermutation() create and return a list which gets printed and then thrown away.

如果您不断添加到同一个列表中,它只会越来越多.

If you keep adding to the same list, it's just going to grow and grow.

这篇关于在 toString 方法中返回 ArrayList 时,排列列表会重复自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆