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],该怎么改?
14 个解决方案
#1
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 = null;
for(int i = 0; i<3; i++){
cca = new ArrayList<String>();//你这样子相当于每次循环都新建一个ArrayList,所以最后 cca的内容只有z和c
cca.add("z");
cca.add(cc[i]);
#2
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]);
List<String> cca = new ArrayList<String>();
for(int i = 0; i<3; i++){
cca.add("z");
cca.add(cc[i]);
#3
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]);
}
#4
++
#5
String[] cc = {"a","b","c"};
List<String> cca = new ArrayList<String>();
for(int i = 0; i<cc.length; i++){
cca.add("z");
cca.add(cc[i]);
}
这样
List<String> cca = new ArrayList<String>();
for(int i = 0; i<cc.length; i++){
cca.add("z");
cca.add(cc[i]);
}
这样
#6
谢谢大家了,我开始喜欢这个美丽的论坛了~~~
#7
1楼正解,5楼你说的那样的话输出[z,a,z,b,z,c]了。别误人子弟。看清楚再说。
#8
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);
}
}
#9
LZ,你逻辑有点错误,每次循环都是新建一个list,添加入z,a[i],所以最后一次就是z,a[2],我想的是先把z在循环外加入list,然后再循环加入数组
import java.util.*;
public class aaaaaaaaaa {
public static void main(String[] args) {
List<String> arr = new ArrayList<String>();
String[] a = {"a","b","c"};
arr.add("z");
for(int i=0;i<a.length;i++){
arr.add(a[i]);
}
Iterator it = arr.iterator();
while(it.hasNext()){
String str = (String) it.next();
System.out.print(str+" ");
}
}
}
import java.util.*;
public class aaaaaaaaaa {
public static void main(String[] args) {
List<String> arr = new ArrayList<String>();
String[] a = {"a","b","c"};
arr.add("z");
for(int i=0;i<a.length;i++){
arr.add(a[i]);
}
Iterator it = arr.iterator();
while(it.hasNext()){
String str = (String) it.next();
System.out.print(str+" ");
}
}
}
#10
+1
#11
String[] cc = {"a","b","c"};
String[] str=new String[4];
str[0]="z";
str[1]=cc[0];
str[2]=cc[1];
str[3]=cc[2];
for (String string : str) {
System.out.println(string);
}
方法很多
#12
#13
这样好一点
String[] cc = {"a","b","c"};
String[] str=new String[4];
str[0]="z";
for(int i=0;i<cc.length;i++){
str[i+1]=cc[i];
}
for (String string : str) {
System.out.println(string);
}
#14
#1
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 = null;
for(int i = 0; i<3; i++){
cca = new ArrayList<String>();//你这样子相当于每次循环都新建一个ArrayList,所以最后 cca的内容只有z和c
cca.add("z");
cca.add(cc[i]);
#2
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]);
List<String> cca = new ArrayList<String>();
for(int i = 0; i<3; i++){
cca.add("z");
cca.add(cc[i]);
#3
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]);
}
#4
++
#5
String[] cc = {"a","b","c"};
List<String> cca = new ArrayList<String>();
for(int i = 0; i<cc.length; i++){
cca.add("z");
cca.add(cc[i]);
}
这样
List<String> cca = new ArrayList<String>();
for(int i = 0; i<cc.length; i++){
cca.add("z");
cca.add(cc[i]);
}
这样
#6
谢谢大家了,我开始喜欢这个美丽的论坛了~~~
#7
1楼正解,5楼你说的那样的话输出[z,a,z,b,z,c]了。别误人子弟。看清楚再说。
#8
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);
}
}
#9
LZ,你逻辑有点错误,每次循环都是新建一个list,添加入z,a[i],所以最后一次就是z,a[2],我想的是先把z在循环外加入list,然后再循环加入数组
import java.util.*;
public class aaaaaaaaaa {
public static void main(String[] args) {
List<String> arr = new ArrayList<String>();
String[] a = {"a","b","c"};
arr.add("z");
for(int i=0;i<a.length;i++){
arr.add(a[i]);
}
Iterator it = arr.iterator();
while(it.hasNext()){
String str = (String) it.next();
System.out.print(str+" ");
}
}
}
import java.util.*;
public class aaaaaaaaaa {
public static void main(String[] args) {
List<String> arr = new ArrayList<String>();
String[] a = {"a","b","c"};
arr.add("z");
for(int i=0;i<a.length;i++){
arr.add(a[i]);
}
Iterator it = arr.iterator();
while(it.hasNext()){
String str = (String) it.next();
System.out.print(str+" ");
}
}
}
#10
+1
#11
String[] cc = {"a","b","c"};
String[] str=new String[4];
str[0]="z";
str[1]=cc[0];
str[2]=cc[1];
str[3]=cc[2];
for (String string : str) {
System.out.println(string);
}
方法很多
#12
#13
这样好一点
String[] cc = {"a","b","c"};
String[] str=new String[4];
str[0]="z";
for(int i=0;i<cc.length;i++){
str[i+1]=cc[i];
}
for (String string : str) {
System.out.println(string);
}