Java 如何使用整数数组列表的数组列表?
我试图在Java中使用整数数组列表的数组列表,代码:Java 如何使用整数数组列表的数组列表?,java,arraylist,multidimensional-array,integer,Java,Arraylist,Multidimensional Array,Integer,我试图在Java中使用整数数组列表的数组列表,代码: 属性和构造函数 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 Ar
属性和构造函数
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));
}
}
是印刷吗
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]
我已尝试在VPond中更改外接程序temp和/或gpond的设置我认为这是一个相当小的错误,但我就是想不出来你的问题是你总是在方法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
}
GrPond(int v){//总节点数(主arraylist中的arraylist)
V=V;
gpond=newarraylist();
对于(int i=0;i您的问题是,您总是在方法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
}
GrPond(int v){//总节点数(主arraylist中的arraylist)
V=V;
gpond=newarraylist();
对于(int i=0;iinVPond
您必须检查是否存在一个现有的行v,因为您将删除所有现有值,并且您不能总是将该行添加到gpond,因为更高的行会被移动,所以您必须为现有行使用set在VPond
您必须检查是否存在一个现有的行v,因为您将删除所有现有的行v值,并且您不能总是将行添加到gpond,因为更高的行会被移位,所以您必须对现有行使用set。我编写的代码最初是用其他语言编写的(变量是可以理解的),我尝试将其翻译为“英语”以使其更具可读性,抱歉!我通过在构造函数中添加for循环来修复它“gpond.add(newarraylist(Collections.nCopies(V,null));”(基本上是您所做的)。我感谢您的帮助。我对代码做了一些更改,以便可以像您所说的那样摆脱我以前使用的临时ArrayList。我在您的代码中看到,如果我首先设置一个ArrayList并将它们定义为null(内部的数组列表),然后我从主ArrayList获取ArrayList并将其更改为另一个值,它从主ArrayList更改了原始ArrayList中的值,对吗?(希望不会混淆)我不知道。我编写的代码最初是用其他语言编写的(变量是可以理解的),我尝试将其翻译为“英语”为了让它更可读,对不起!我通过在构造函数“gpond.add(newarraylist(Collections.nCopies(V,null));”中添加for循环修复了它(基本上就是您所做的)。我感谢您的帮助。我对代码做了一点更改,这样我也可以像您所说的那样摆脱以前使用的临时ArrayList。对于我在您的代码中看到的情况,如果我首先设置ArrayList的ArrayList并将其定义为null(内部),然后我从主ArrayList获取ArrayList并将其更改为另一个值,它从主ArrayList更改原始ArrayList中的值,对吗?(希望不会混淆)我不知道。
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]
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
}
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);
}