当前位置: 代码迷 >> J2SE >> 如何循环向list插入数据
  详细解决方案

如何循环向list插入数据

热度:22   发布时间:2016-04-24 00:27:52.0
怎么循环向list插入数据
String[] cc = {"a","b","c"};
List<String> cca = null;
for(int i = 0; i<3; i++){
cca = new ArrayList<String>();
cca.add("z");
cca.add(cc[i]);
输出是[z,c];我想输出[z,a,b,c],该怎么改?

------解决方案--------------------
Java code
    String[] cc = {"a","b","c"};        List<String> cca = null;        cca = new ArrayList<String>();        cca.add("z");        for(int i = 0; i<3; i++){                    cca.add(cc[i]);        }        System.out.println(cca) ;
------解决方案--------------------
String[] cc = {"a","b","c"};
List<String> cca = new ArrayList<String>();
for(int i = 0; i<3; i++){
cca.add("z");
cca.add(cc[i]);
------解决方案--------------------
Java code
String[] cc = {"a","b","c"};        List<String> cca = new ArrayList<String>();        cca.add("z");        for(int i = 0; i<3; i++){            cca.add(cc[i]);        }
------解决方案--------------------
Java code
import java.util.ArrayList;import java.util.List;public class b {public static void main(String[] args) {    String[] cc = {"a","b","c"};    List<String> cca = new ArrayList<String>();    cca.add("z");    for (int i = 0; i < cc.length; i++) {        cca.add(cc[i]);    }    System.out.println(cca);}}
  相关解决方案