简体   繁体   中英

Storing data from string Array to String ArrayList

I have

String add_data[] = new String[6];

with datas at 0,1,2,3,4,5 indexes.

Also I have

 ArrayList<String> data = new ArrayList<String>();

now I need to put the values in all the indexes in add_data at data[0].

How can i do this? please guide me.

Eg. add_data[0]="a"; add_data[0]="b";

data[0] should have "ab"

use Arrays.asList(array) method to copy array to List. In your case - String[] to List<String> .

I'm not sure to understand what you want to do, but try this :

String temp = "";
for(String s : add_data) {
    temp += s;
}
data.put(temp);
String result = "";
for (int i = 0; i < add_data.length: i++) {
  result += add_data[i];
}
data.add(result);

You need:

  1. Catenate all Strings from your array
  2. Put this at 0 index in your ArrayList

Something like this:

String addData[] = new String[6];
ArrayList<String> data = new ArrayList<String>();    
addData[0] = "a";
addData[1] = "b";

// 1. catenate all strings
String str = "";
for (String s : addData) {
    str += (s != null)?s:"";
}

// 2. put it into 0 index in your arraylist     
data.add(0, str);
System.out.println(data.get(0));

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