【问题标题】:How to work with ArrayList of ArrayLists of Integers?如何使用整数的 ArrayLists 的 ArrayList?
【发布时间】:2021-02-21 01:58:10
【问题描述】:

我正在尝试在 Java 中使用 ArrayLists of Integers 的 ArrayList,代码:

属性和构造函数

int V;
    ArrayList<ArrayList<Integer>>  gpond;
    ArrayList<Integer> vpond;
    
    GrPond(int v){ //Number of total nodes (arraylists in the main arraylist)
        V=v;
        gpond = new ArrayList<>();
        for(int i=0;i<V;i++){
            gpond.add(new ArrayList<Integer>(V));
        }
    }

我使用这种方法将整数的ArrayLists添加到主ArrayList(gpond)

 void VPond(int v, int w,int val){ // v=origin, w=destination, val=value.
        ArrayList<Integer> temp = new ArrayList<Integer>(V);
        for(int i=0;i<V;i++){
            temp.add(null);
        }
        temp.set(w, val); //I want to set in the given index a certain value
        gpond.add(v, temp); //Then, add that ArrayList with the value (val) 
                            //at the given index (w), to the main ArrayList.
    }

并尝试使用以下方式打印它:

void printGraph(){
        for(int i=0;i<V;i++){
            System.out.println("node "+i);
            if(gpond.get(i)!=null){  //Just want to print those who are occupied
                for(int j=0;j<V;j++){ //j<V, because each ArrayList<Integer> has maximum V values
                    if(gpond.get(i).get(j)!=null) //Just print the indexes with a given value
                        System.out.println("["+i+"]-> ["+j+"] | val: "+gpond.get(i).get(j));
                }
            }
        }
        
        for(ArrayList<Integer> e: gpond){ //Just want to test each ArrayList of the main ArrayList
            System.out.println("e: "+ e);
        }
    }

如果输入是,我希望它做什么,可以说:

v,w,val:
0 1 2
0 2 1
1 0 1 

e: [null, 2, null], [null, null, 1] //or something like this. e=origin, [index->value]
e: [1, null, null]

是打印

node 0
[0]->[1] | val: 2
[0]->[2] | val: 1
[1]->[0] | val: 1

改为打印:

node 0
[0]->[2] | val: 1
node 1
[1]->[0] | val: 1
node 2
[2]->[1] | val: 2

e: [null, null, 1]
e: [1, null, null]
e: [null, 2, null]

我已尝试在 temp 中为 add 更改 set 和/或 VPond 中的 gpond
我认为这是一个很小的错误,但我就是想不通

【问题讨论】:

  • VPond 中,您必须检查是否存在现有行 v,因为您会删除所有现有值,并且您不能总是将该行添加到 gpond,因为更高的行会移动,所以你必须对现有行使用 set

标签: java arraylist multidimensional-array integer


【解决方案1】:

您的问题是您总是在您的方法 VPond 中创建一个完整的新列表。所以你失去了你的旧价值观。您应该在GrPond 的构造函数中创建新的List,并在VPond 中设置新值。

GrPond(int v){ //Number of total nodes (arraylists in the main arraylist)
    V = v;
    gpond = new ArrayList<>();
    for(int i=0;i<V;i++){
        ArrayList<Integer> temp = new ArrayList<Integer>(V);
        for(int j=0;j<V;j++){
            temp.add(null);
        }
        gpond.add(temp);
    }
}

void VPond(int v, int w,int val){ // v=origin, w=destination, val=value.
    List<Integer> temp=gpond.get(v);
    temp.set(w, val); //I want to set in the given index a certain value
}

请考虑一下变量的名称。您使用 v、w、val、V、VPond、GrPond、i 和 j。如果您在编写它一周后触摸此代码,您将完全感到困惑(就像我一样)。你还写cmets来翻译你的变量名,为什么不直接用这些意思呢?

int sizeOfLists;
List<List<Integer>> gpond;
List<Integer> vpond;

GrPond(int listSize) {
    sizeOfLists = listSize;
    gpond = new ArrayList<>();
    for (int listIndex = 0; listIndex < sizeOfLists; listIndex++) {
        List<Integer> temp = new ArrayList<>(sizeOfLists);
        for (int indexInList = 0; indexInList < sizeOfLists; indexInList++) {
            temp.add(null);
        }
        gpond.add(temp);
    }
}

void VPond(int listIndex, int indexInList, int value) { listIndex=listIndex, indexInList=indexInList
    List<Integer> list = gpond.get(listIndex);
    list.set(indexInList, value);
}

【讨论】:

  • 我写的代码原来是其他语言的(而且变量是可以理解的),我试着把它翻译成“英文”,让它更具可读性,对不起!
  • 我通过在构造函数 "gpond.add(new ArrayList(Collections.nCopies(V,null)));" 中添加 for 循环来修复它(基本上你做了什么)。我感谢您的帮助。正如你所说,我更改了一些代码,以便摆脱之前使用的临时 ArrayList。
  • 对于我在您的代码中看到的内容,如果我首先设置一个 ArrayList 的 ArrayList 并将它们定义为 null(内部的),然后我从主 ArrayList 获取该 ArrayList 并将其更改为另一个值,它改变了主 ArrayList 中原始 ArrayList 中的值,对吧? (希望没有混淆)不知道。
猜你喜欢
  • 2013-11-14
  • 2011-10-16
  • 1970-01-01
  • 2012-11-19
  • 2013-03-24
  • 1970-01-01
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
相关资源
最近更新 更多