Arraylist练习题
作者:互联网
存储随机数字
1.生成6个1~33之间的随机整数,添加到集合,并遍历集合。
public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<>(); Random ra = new Random(); for (int i = 0; i <6; i++) { int i1 = ra.nextInt(33) + 1; list.add(i1); } for (int i = 0; i <list.size(); i++) { System.out.println(list.get(i)); } }
2.自定义4个学生对象,添加到集合,并遍历。
public static void main(String[] args) { ArrayList<Person> list = new ArrayList<>(); Person p1 = new Person("张三",11); Person p2 = new Person("李四",12); Person p3 = new Person("王五",19); Person p4 = new Person("赵六",22); list.add(p1); list.add(p2); list.add(p3); list.add(p4); for (int i = 0; i < list.size(); i++) { Person per = list.get(i); System.out.println("姓名:"+per.getName()+",年龄:"+per.getAge()); } }
搜索
复制
标签:练习题,Arraylist,list,per,Person,add,new,ArrayList 来源: https://www.cnblogs.com/pengtianyang/p/16423544.html