从数组创建ArrayList

时间:2014-01-10 04:54:55

标签: java arraylist

我想从下面的某些数组中创建3个ArrayLists:

([x,y])  ([x1,y1],[x2,y2],[x3,y3])  ([m1,n1],[m2,n2],[m3,n3],[m4,n4],[m5,n5])

以下是与此部分相关的代码:

    public static ArrayList<Double[]>[] hexpos = (ArrayList<Double[]>[])new ArrayList[3];


      for (int i = 0; i < 3; i++)
        hexpos[ i ] = new ArrayList<Double[]> ();

// ==================== initial arraylist1

     Double[] t = new Double[2]; 
     t[0]=x;
     t[1]=y;
     hexpos[0].add(t);
     System.out.println("xtest"+ hexpos[0].get(0)[0]);
     System.out.println("ytest"+ hexpos[0].get(0)[1]);

// ========================初始arraylist2

     int count=1;      
     t = new Double[2]; 
     t[0]=x;
     t[1]=y-(2*r);
     hexpos[count].add(t);

    t = new Double[2]; 
    t[0]=x+(3*s/2);
    t[1]=y-r;
    hexpos[count].add(t);

    t = new Double[2]; 
    t[0]=x+(3*s/2);
    t[1]=y+r;
    hexpos[count].add(t);
    count++;

// ========================初始arraylist3

    t = new Double[2]; 
    t[0]=x;
    t[1]=y-(4*r);
    hexpos[count].add(t);

    t = new Double[2]; 
    t[0]=x+(3*s/2);
    t[1]=y-(3*r);
    hexpos[count].add(t);


    t = new Double[2]; 
    t[0]=x+(3*s);
    t[1]=y-(2*r);
    hexpos[count].add(t);


    t = new Double[2]; 
    t[0]=x+(3*s);
    t[1]=y;
    hexpos[count].add(t);

我打印出第一个arraylist作为例子: 输出是: xtest 400 ytest 400 xtest 400 ytest 400 xtest 400 ytest 400 问题是它为什么重复3次!!!

1 个答案:

答案 0 :(得分:3)

尝试Arrays.asList

String arr1[] = {"111","poo","cfggg"};
String arr2[] = {"vfv","poc","cdegg"};

ArrayList<String> arrayList1 = new ArrayList<String>();
arrayList1.addAll(Arrays.asList(arr1));

ArrayList<String> arrayList2 = new ArrayList<String>();
arrayList2.addAll(Arrays.asList(arr2));

System.out.println(arrayList1);
System.out.println(arrayList2);