简体   繁体   English

如何使用整数的 ArrayLists 的 ArrayList?

[英]How to work with ArrayList of ArrayLists of Integers?

I'm trying to use an ArrayList of ArrayLists of Integers in Java, code:我正在尝试在 Java 中使用整数 ArrayLists 的 ArrayList,代码:

Attributes and Constructor属性和构造函数

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));
        }
    }

Im using this method to add the ArrayLists of Integers to the main ArrayList (gpond)我使用这种方法将整数的 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.
    }

And trying to print it using:并尝试使用以下方法打印它:

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);
        }
    }

What I expect it to do if the input is, lets say:如果输入是,我希望它做什么,让我们说:

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]

is to print是打印

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

Instead its printing:相反,它的印刷:

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]

I've tried changing set for add in temp and/or gpond in VPond我已经尝试更改设置以在VPond添加临时和/或 gpond
I think it's a pretty little mistake but I just can't figure it out我认为这是一个很小的错误,但我就是想不通

Your problem is that you always create a complete new List in your method VPond.你的问题是你总是在你的方法 VPond 中创建一个完整的新列表。 So you loose your old values.所以你失去了旧的价值观。 You should create the new List in the constructor of GrPond and just set the new value in VPond .你应该在构造函数中创建新的列表GrPond和刚刚成立的新价值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
}

And please, think about the names of your variables.请考虑变量的名称。 You use v, w, val, V, VPond, GrPond, i and j.您使用 v、w、val、V、VPond、GrPond、i 和 j。 If you touch this code a week after you wrote it you will be completely confused (just as I was).如果您在编写代码一周后触摸此代码,您将完全困惑(就像我一样)。 You even write comments to translate your variable names, why don't you use these meanings directly?你甚至写注释来翻译你的变量名,你为什么不直接使用这些含义?

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);
}

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

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