作业1:集合的嵌套遍历
package org.wests.HomeWork;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
/**
* 1:集合的嵌套遍历
* 需求:
*我们班有学生,每一个学生是不是一个对象。所以我们可以使用一个集合表示我们班级的学生。ArrayList*但是呢,我们旁边是不是还有班级,每个班级是不是也是一个ArrayList。
*而我现在有多个ArrayList。也要用集合存储,怎么办呢?
* @author 代虎
*
*/
public class HomeWork01 {
public static void main(String[] args) {
ListstuList = new ArrayList<>();
stuList.add(new Student("Apache", "Java"));
stuList.add(new Student("Commons", "Java"));
stuList.add(new Student("Github ", "Java"));
ListstuList2 = new ArrayList<>();
stuList2.add(new Student("张艺", "高三20班"));
stuList2.add(new Student("张玥", "高三20班"));
stuList2.add(new Student("贺州", "高三20班"));
@SuppressWarnings("rawtypes")
HashMapclassMap = new HashMap<>();
classMap.put(stuList, stuList.get(0).getClassname());
classMap.put(stuList2, stuList2.get(0).getClassname());
@SuppressWarnings("rawtypes")
Iterator it = classMap.keySet().iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
自定义的学生类:
package org.wests.HomeWork;
public class Student {
private String name;
private String Classname;
public Student() {
}
public Student(String name) {
super();
this.name = name;
}
public Student(String name, String classname) {
super();
this.name = name;
Classname = classname;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassname() {
return Classname;
}
public void setClassname(String classname) {
Classname = classname;
}
@Override
public String toString() {
return this.Classname + ":"+ this.name;
}
}
执行结果:
作业二:获取10个1-20之间的随机数,要求不能重复
package org.wests.HomeWork;
import java.util.HashSet;
import java.util.Set;
/**
* 2:获取10个1-20之间的随机数,要求不能重复
* @author 代虎
*
*/
public class HomeWork02 {
public static void main(String[] args) {
Setset = new HashSet<>();
for(int i=0;i<1000;i++) {
int number =(int)(Math.random()*20);
set.add(number);
Object[] array = set.toArray();
if(array.length>10-1) {
break;
}
}
System.out.println(set);
}
}
执行结果:
作业三:使用ArrayList集合存储自定义对象并遍历(三种方式去实现)
package org.wests.HomeWork;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
/**
* 3、使用ArrayList集合存储自定义对象并遍历(三种方式去实现)
* 分析思路: 1、建立自定义的对象Student
* 2、定义一个集合ArrayList
* 3、三种方法:A:普通for循环或者forEach语句
* B:Iterator/list.listIterator()
* C:转化为数组,遍历
*
* @author 代虎
*
*/
public class HomeWork03 {
/*
* A:普通for循环或者forEach语句
*/
public static void method01(Listlist) {
//for(int i=0;ivoid method02(Listlist) {
ListIteratorit = list.listIterator();
System.out.println("List的正向输出");
while(it.hasNext()){
System.out.println(it.next());
}
System.out.println("List的反向输出");
while(it.hasPrevious()) {
System.out.println(it.previous());
}
}
public static void method03(Listlist) {
Object[] arr = list.toArray();
for(int i=0;ilist = new ArrayList<>();
System.out.println("My favorite basketball star:");
list.add(new Student("尼克杨", "勇士"));
list.add(new Student("库里", "勇士"));
list.add(new Student("汤普森", "勇士"));
list.add(new Student("欧文", "凯尔特人"));
System.out.println("方法一");
method01(list);
System.out.println("方法二");
method02(list);
System.out.println("方法三");
method03(list);
}
}
执行结果: