简体   繁体   中英

How to retrieve data from ArrayList consists of array objects

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?

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));
    }
}
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.

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM