-1
我必须在Java 1.4中创建类似于结构的表格,并且我认为要使用字符串数组列表。尽管如此,在插入不同的值后,我的代码总是从列表的不同位置获取相同的值。看下面的代码和生成的输出。这个Java代码处理ArrayList有什么问题?
package various_tests;
import java.util.ArrayList;
public class workWithLists {
public static void main(String[] args) {
String[] idarTableRow= new String[3];
String[] line = new String[3];
ArrayList idarTable=new ArrayList();
// Create first row
idarTableRow[0]="A";
idarTableRow[1]="CATAF245";
// add row to table
idarTable.add(idarTableRow);
// Create second row
idarTableRow[0]="B";
idarTableRow[1]="CATAF123";
// add row to table
idarTable.add(idarTableRow);
//Print First row, column one and two
line = (String[]) idarTable.get(0);
System.out.print("Value at row 0: Column 1 is "+ line[0]+" ;Column 2 is "+ line[1]+"\n");
//Print second row, column one and two
line = (String[]) idarTable.get(1);
System.out.print("Value at row 1: Column 1 is "+ line[0]+" ;Column 2 is "+ line[1]+"\n");
}
}
和输出
Value at row 0: Column 1 is B ;Column 2 is CATAF123
Value at row 1: Column 1 is B ;Column 2 is CATAF123
我不明白为什么这只是露出插在列表的最后一个值,而不是不同的值在列表的位置0和1。 我在做什么错了?
您正在添加相同的数组两次。它会有你填写的最后一个值。 –