简体   繁体   English

如何在Java的ArrayList中存储很多数组?

[英]How can I store a lot of arrays in an ArrayList in Java?

I have a function that through recursion prints every combination of numbers of an input array. 我有一个函数,可以通过递归打印输入数组的数字的每个组合。

public class comb {
    public static void main(String[] args)

        List<int[]> list = new ArrayList<int[]>();


        int[] test = {1,2,3,4};
        combinations(test,2,0,new int[2]);

        listToString(list);
    }


    static void combinations(int[] arr, int n, int pos, int[] result){
        if(n == 0){
            System.out.println(Arrays.toString(result));
            return;
        }
        for(int i = pos; i <= arr.length - n; i++){
            result[result.length - n] = arr[i];
            combinations(arr,n-1,pos+1,result);
        }
    }

Outputs 产出

[1, 2]
[1, 3]
[1, 4]
[2, 2]
[2, 3]
[2, 4]
[3, 2]
[3, 3]
[3, 4]

What should I do if instead of having each array printed, I want it added to a list? 如果我希望将其添加到列表中而不是打印每个数组,该怎么办? I can't just add result to the list because it is constantly changing. 我不能只将结果添加到列表中,因为它一直在变化。

There's multiple ways you might achieve it... 您可以通过多种方式实现它...

You could... 你可以...

Make List<int[]> list = new ArrayList<int[]>(); 使List<int[]> list = new ArrayList<int[]>(); a static global static全局

public class comb {
    static List<int[]> list = new ArrayList<int[]>();
    public static void main(String[] args) {

Then you could just add the values to the ArrayList ... 然后,您可以将值添加到ArrayList

static void combinations(int[] arr, int n, int pos, int[] result) {
    if (n == 0) {
        list.add(result);
        return;
    }

But this is generally a bad idea, is sloppy programming and leads to bad habits 但这通常是一个坏主意,是草率的编程并导致不良习惯

You could... 你可以...

Pass a reference of the List to the combinations method... List的引用传递给combinations方法...

static void combinations(int[] arr, int n, int pos, int[] result, List<int[]> list) {
    if (n == 0) {
        list.add(Arrays.copyOf(result, result.length));
        return;
    }

This is better, as it makes the API re-usable and capable of been called multiple times within the same instance of the program without otherwise destroying what was in the List , like the previous example 这样做更好,因为它使该API可重复使用,并且能够在程序的同一实例中多次调用,而又不会破坏List ,就像前面的示例一样

You could... 你可以...

Have the combinations method return a List ... combinations方法返回一个List ...

static List<int[]> combinations(int[] arr, int n, int pos, int[] result) {
    List<int[]> list = new ArrayList<>(25);
    if (n == 0) {
        list.add(Arrays.copyOf(result, result.length));
    } else {
        for (int i = pos; i <= arr.length - n; i++) {
            result[result.length - n] = arr[i];
            list.addAll(combinations(arr, n - 1, pos + 1, result));
        }
    }
    return list;
}

This is kind of cool in the fact that you don't need to care about how the List was created, only that it will return all combinations of the arr to your specifications. 这很酷,因为您无需关心List创建方式,只需将List所有arr组合返回到您的规范即可。 It's probably not as efficient as the previous example, as you are creating a new List on each recursion, but does hide the implementation details 它可能不如上一个示例有效,因为您在每个递归上都创建了一个新的List ,但确实隐藏了实现细节

You can copy the array, and add the copy to the list. 您可以复制阵列,然后将副本添加到列表中。

static void combinations(int[] arr, int n, int pos, int[] result, List<int[]> allResults){
    if(n == 0){
        allResults.add(Arrays.copyOf(result));
        return;
    }
    // etc...

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

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