开发者

Storing data from string Array to String ArrayList

开发者 https://www.devze.com 2023-02-15 15:47 出处:网络
I have String add_data[] = new String[6]; with datas at 0,1,2,3,4,5 indexes. Also I hav开发者_JAVA技巧e

I have

String add_data[] = new String[6];

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

Also I hav开发者_JAVA技巧e

 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>.


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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消