java中使用Arraylist动态二维数组

发布于 2024-09-15 02:27:32 字数 1166 浏览 0 评论 0原文

我在使用 arraylist 创建动态二维数组时发现了一些问题,原始代码阅读起来很乏味,所以我在这里给出一个简单的代码,两种情况下问题都是相同的:

import java.util.*;

class test{
 public static void main(String args[]){
    Integer test[]=new Integer[3];

    ArrayList<Integer[]> al=new ArrayList<Integer[]>();

    int i,t;

     test[0]=1;
     test[1]=2;
     test[2]=3;


     al.add(test);
     test[0]=4;
     test[1]=5;
     test[2]=6;

  al.add(test);

     test[0]=7;
     test[1]=8;
     test[2]=9;

  al.add(test);



     test[0]=10;
     test[1]=11;
     test[2]=12;
  al.add(test);



     Integer table[][]=new Integer[al.size()][];
     table=al.toArray(table);

     for(i=0;i<=al.size()-1;i++){

     for(t=0;t<3;t++){
       System.out.print(" "+i+" "+t+" ");
       System.out.print(" "+table[i][t]+" ");}
     System.out.println();
 }   

   }
}

输出:

 0 0  10  0 1  11  0 2  12
  1 0  10  1 1  11  1 2  12
 2 0  10  2 1  11  2 2  12
 3 0  10  3 1  11  3 2  12

预期输出是

 0 0   1  0 1  2  0 2  3
 1 0   4  1 1  5  1 2  6
 2 0   7  2 1  8  2 2  9
 3 0  10  3 1  11  3 2  12

我不明白为什么最后一个元素覆盖所有其他元素。

I found some problem in creating dynamic 2d array with arraylist,The original code is tediously long to read so i am giving a simple code here,the problem is same in both the cases:

import java.util.*;

class test{
 public static void main(String args[]){
    Integer test[]=new Integer[3];

    ArrayList<Integer[]> al=new ArrayList<Integer[]>();

    int i,t;

     test[0]=1;
     test[1]=2;
     test[2]=3;


     al.add(test);
     test[0]=4;
     test[1]=5;
     test[2]=6;

  al.add(test);

     test[0]=7;
     test[1]=8;
     test[2]=9;

  al.add(test);



     test[0]=10;
     test[1]=11;
     test[2]=12;
  al.add(test);



     Integer table[][]=new Integer[al.size()][];
     table=al.toArray(table);

     for(i=0;i<=al.size()-1;i++){

     for(t=0;t<3;t++){
       System.out.print(" "+i+" "+t+" ");
       System.out.print(" "+table[i][t]+" ");}
     System.out.println();
 }   

   }
}

Output:

 0 0  10  0 1  11  0 2  12
  1 0  10  1 1  11  1 2  12
 2 0  10  2 1  11  2 2  12
 3 0  10  3 1  11  3 2  12

Output expected is

 0 0   1  0 1  2  0 2  3
 1 0   4  1 1  5  1 2  6
 2 0   7  2 1  8  2 2  9
 3 0  10  3 1  11  3 2  12

I don't under stand why the last element is over writting all other elements.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

书信已泛黄 2024-09-22 02:27:32

每次添加新行时都会初始化一个新的 Integer[]

也就是说,这样做:

Integer[] test = new Integer[3];
List<Integer[]> al = new ArrayList<Integer[]>();
int i,t;
test[0]=1;
test[1]=2;
test[2]=3;
al.add(test);
test = new Integer[3]; // Note this line
test[0]=4;
test[1]=5;
test[2]=6;
al.add(test);
test = new Integer[3]; // Note this line
test[0]=7;
test[1]=8;
test[2]=9;
al.add(test);
test = new Integer[3]; // Note this line
test[0]=10;
test[1]=11;
test[2]=12;
al.add(test);

或者更好的是,这样做:

List<Integer[]> al = new ArrayList<Integer[]>();
al.add(new Integer[]{1, 2, 3});
al.add(new Integer[]{4, 5, 6});
al.add(new Integer[]{7, 8, 9});
al.add(new Integer[]{10, 11, 12});

Initialize a new Integer[] every time you add a new row.

That is, do it like this:

Integer[] test = new Integer[3];
List<Integer[]> al = new ArrayList<Integer[]>();
int i,t;
test[0]=1;
test[1]=2;
test[2]=3;
al.add(test);
test = new Integer[3]; // Note this line
test[0]=4;
test[1]=5;
test[2]=6;
al.add(test);
test = new Integer[3]; // Note this line
test[0]=7;
test[1]=8;
test[2]=9;
al.add(test);
test = new Integer[3]; // Note this line
test[0]=10;
test[1]=11;
test[2]=12;
al.add(test);

Or better yet, do it this way:

List<Integer[]> al = new ArrayList<Integer[]>();
al.add(new Integer[]{1, 2, 3});
al.add(new Integer[]{4, 5, 6});
al.add(new Integer[]{7, 8, 9});
al.add(new Integer[]{10, 11, 12});
街道布景 2024-09-22 02:27:32

数组是对象。 ArrayList.add(E) 将给定 E 对象的引用添加到列表中;它不复制对象本身。

所以你应该这样做:

al.add(test);
test = new int[3];

这会创建一个新的数组对象,因此第二行数据被写入一个单独的数组中。

Arrays are objects. ArrayList.add(E) adds a reference to the given E object to the list; it doesn't copy the object itself.

So you should do:

al.add(test);
test = new int[3];

Which creates a new array object, so the second row of data is written into a separate array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文