1

My arraylist contains String array objects. How Can I get all values? My code is below,

String cordinates1c,cordinates2l,cordinates2m;                   
String[] array1={cordinates1c,cordinates2l,cordinates2m};
String[] array2={cordinates1c,cordinates2l,cordinates2m};

ArrayList alist=new ArrayList();

alist.add(array1);

alist.add(array2);

    //retreieving

for(int i=0;i<alist.size();i++)

System.out.println("arrayList="+alist.get(i));

if I try to retrieve like above it gives,

07-12 12:42:09.977: INFO/System.out(743): arrayList=[[Ljava.lang.String;@43e11b28]

How to do that?

6 Answers 6

3

Arrays should be printed with the help of Arrays.toString() or Arrays.deepToString().

import java.util.Arrays;
public class Test {
    public static void main(String[] args) throws Exception {
        String[][] a = {{"a", "b", "c"}, {"d", "e"}};
        System.out.println(Arrays.deepToString(a));
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

or deepTOString in case of multi dimensional arrays.
1
for(int i=0;i<alist.size();i++) {

    for (String a : alist.get(i)) {
        System.out.println(a);
    }
}

You have to iterate over the array of strings, too.

1 Comment

If jsonAraay contains this alist, then how to retreive data?
1

You can iterate over your List and then use Arrays.toString() or Arrays.deepToString() to print array contents

 for (String[] eachArray : alist) {
     System.out.println("arrayList=" + Arrays.deepToString(eachArray));
 }

Comments

1
ArrayList<String[]> l;
for (String[] a : l) {
    for (String s : a) {
        System.out.println(s);
    }
}

1 Comment

If jsonAraay contains this alist, then how to retreive data?
0

Hoping that you are trying to print the string in array1

ArrayList<String> alist= (ArrayList<String>) Arrays.asList(array1);

Now print the data from alist.

also have a look into alist.addAll(collection)

But following snippet will add array1 and array2 object to your ArrayList, So retrieval you will get an array object

alist.add(array1);

alist.add(array2);

Comments

0

Are you looking for something like this ?

String cordinates1c = "1", cordinates2l = "2", cordinates2m = "3"; 
String[] array1 = {cordinates1c, cordinates2l, cordinates2m};      
String[] array2 = {cordinates1c, cordinates2l, cordinates2m};      

List<String []> alist=new ArrayList<String []>();                  
alist.add(array1);                                                 
alist.add(array2);                                                 
for (String[] strings : alist) {                                   
    System.out.println(StringUtils.join(strings));                 
}  

Output: 123 123

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.