package rew;
import java.util.List;
import java.util.ArrayList;
public class UseForeach {
public static void main(String[] args){
List<String>list=new ArrayList<String>(); //创建集合
list.add("abc");
list.add("def");
list.add("ghi");
list.add("klh");
list.add("qrs");
System.out.print("foreach遍历集合:\n\t"); //初始化集合
for(String string:list){
System.out.print(string);
}
System.out.println();
String[] strs=new String[list.size()]; //创建数组
list.toArray(strs);
System.out.print("foreach遍历数组:\n\t");
for(String string:strs){
System.out.print(string);
}
}
import java.util.List;
import java.util.ArrayList;
public class UseForeach {
public static void main(String[] args){
List<String>list=new ArrayList<String>(); //创建集合
list.add("abc");
list.add("def");
list.add("ghi");
list.add("klh");
list.add("qrs");
System.out.print("foreach遍历集合:\n\t"); //初始化集合
for(String string:list){
System.out.print(string);
}
System.out.println();
String[] strs=new String[list.size()]; //创建数组
list.toArray(strs);
System.out.print("foreach遍历数组:\n\t");
for(String string:strs){
System.out.print(string);
}
}
}
print和println的区别
print不换行,println换行