练习1、
自己写的代码:
import java.util.ArrayList;
public class practice {public static void main(String[] args) {//定义一个集合ArrayList<String> list = new ArrayList<>();list.add("aaa");list.add("bbb");list.add("ccc");//System.out.println(list);String str = "[";for (int i = 0; i < list.size(); i++) {if( i== list.size()-1){str = str + list.get(i) ;}else{str= str +list.get(i)+",";}//str = str + list.get(i);}str = str +"]";System.out.println(str);}
}
代码运行结果:
视频代码:
练习2、
补充知识点:
基本数据类型的包装类:
自己写代码:
import java.util.ArrayList;
public class practice {public static void main(String[] args) {//定义一个集合ArrayList<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);//System.out.println(list);String str = "[";for (int i = 0; i < list.size(); i++) {if( i== list.size()-1){str = str + list.get(i) ;}else{str= str +list.get(i)+",";}//str = str + list.get(i);}str = str +"]";System.out.println(str);}
}
代码运行结果: