Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I want to create 3 ArrayLists from some arrays like below:

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

Here is the code related to this part:

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

//======================== initial 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++;

//======================== initial 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);

I just print out the first arraylist as an example: The output is: xtest 400 ytest 400 xtest 400 ytest 400 xtest 400 ytest 400 The problem is that why it repeats 3times!!!

share|improve this question
    
What is the size you have? what is the output that you expect? what is the output that you are getting? – neoprez Jan 10 '14 at 5:00
    
What do you mean it "calls the first members 3 times"? Are you sure? Can you show us the context in which you're using it? – David Wallace Jan 10 '14 at 5:11
    
Thx for the kind reply, I have added more details of code and output to the post! – user3180204 Jan 10 '14 at 7:26
    
what is the value of x, y, s and r ? – Yagnesh Agola Jan 10 '14 at 8:19
    
put your whole code to analyse your problem ? – Yagnesh Agola Jan 10 '14 at 8:23

1 Answer 1

Try 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);
share|improve this answer
    
Thank you for the reply, but it is possible to create them as array of arraylist!!? – user3180204 Jan 10 '14 at 7:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.