练习1
package QM_Fx;
import java.util.ArrayList;
public class test{
public static void main(String[] args) {
//1.创建一个集合
ArrayList<String> list=new ArrayList<>();
//2.添加元素
list.add("点赞了吗");
list.add("投币了吗");
list.add("收藏了吗");
list.add("转发了吗");
System.out.print("[");
//3.遍历元素
for (int i = 0; i < list.size(); i++) {
if(i==list.size()-1) {
System.out.print(list.get(i));
}else{
System.out.print(list.get(i)+",");
}
}
System.out.println("]");
}
}
/*
[点赞了吗,投币了吗,收藏了吗,转发了吗]
*/
练习2
用集合储存数组要将数据进行包装类
代码
import java.util.ArrayList;
public class test{
public static void main(String[] args) {
//1.创建一个集合
ArrayList<Integer> list=new ArrayList<>();
//2.添加元素
list.add(1);
list.add(2);
list.add(3);
list.add(4);
System.out.print("[");
//3.遍历元素
for (int i = 0; i < list.size(); i++) {
if(i==list.size()-1) {
System.out.print(list.get(i));
}else{
System.out.print(list.get(i)+",");
}
}
System.out.println("]");
}
}
/*
[1,2,3,4]
*/
char得用单引号
import java.util.ArrayList;
public class test{
public static void main(String[] args) {
//1.创建一个集合
ArrayList<Character> list=new ArrayList<>();
//2.添加元素
list.add('a');
list.add('a');
list.add('a');
list.add('a');
System.out.print("[");
//3.遍历元素
for (int i = 0; i < list.size(); i++) {
if(i==list.size()-1) {
System.out.print(list.get(i));
}else{
System.out.print(list.get(i)+",");
}
}
System.out.println("]");
}
}
/*
[a,a,a,a]
*/