java - elements of arraylist duplicated -


i have created arraylist , added elements (string array) in while loop. use following add elements:

templist.add(recordarray); //- recordarray string[]  //arraylist<string[]> templist = new arraylist<string[]>();// declared in activity before oncreate method 

if check array within while loop using following code:

astringarray = templist.get(index); log.i(tag,"astringarray[0] = " + astringarray[3]); index++; 

i correct string each of 3 array elements added arraylist.

the problem when try using same code outside of while loop, same string displayed each of 3 iterations.

so sum up, in while loop following:

1st iteration -  astringarray[3] - displays "100350 2nd iteration -  astringarray[3] - displays "100750 3rd iteration -  astringarray[3] - displays "100800 

outside of while loop following:

1st iteration -  astringarray[3] - displays "100800 2nd iteration -  astringarray[3] - displays "100800 3rd iteration -  astringarray[3] - displays "100800 

i've searched on answer can't find one. hope here can help.

much appreciated

clive

i suspect you're adding same string array each time go through loop. should create new string array each time.

don't forget list contains references. guess code looks this:

arraylist<string[]> templist = new arraylist<string[]>(); string[] recordarray = new string[4];  (int = 0; < 10; i++) {     recordarray[0] = "a" + i;     recordarray[1] = "b" + i;     recordarray[2] = "c" + i;     recordarray[3] = "d" + i;     templist.add(recordarray); } 

that ends arraylist of 10 identical references. instead, want this:

arraylist<string[]> templist = new arraylist<string[]>();  (int = 0; < 10; i++) {     string[] recordarray = new string[4];     recordarray[0] = "a" + i;     recordarray[1] = "b" + i;     recordarray[2] = "c" + i;     recordarray[3] = "d" + i;     templist.add(recordarray); } 

that way have references 10 different arrays in list.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -