刘鑫同学
2018-11-13 04:01:50浏览 1595
Student.java
public class Student {
private String id;
private String password;
private String name;
public Student(String id, String password, String name) {
super();
this.id = id;
this.password = password;
this.name = name;
}
public String getId() {
return id;
}
public String getPassword() {
return password;
}
public String getName() {
return name;
}
}
Main.java
import Student;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Initial {
public static void listForEach(List<Student> ls) {
for(Student temp: ls) {
System.out.println(temp.getId()+temp.getName());
}
}
public static void setForEach(Set<Student> st) {
for(Student temp: st) {
System.out.println(temp.getId() + temp.getName());
}
}
public static void mapForEach(Map<String,Student> mp) {
Set<String> keySet = mp.keySet();
for (String key: keySet) {
Student temp = mp.get(key);
if(temp != null) {
System.out.println(temp.getId() + temp.getName());
}
}
}
public static void main(String[] args) {
Student[] studentArray = {
new Student("1","123456","Tom"),
new Student("2","123456","Harry"),
new Student("3","123456","William")};
Student s1 = studentArray[0];
Student s2 = studentArray[1];
Student s3 = studentArray[2];
List<Student> studentList = new ArrayList<Student>();
studentList.add(s1);
studentList.addAll(0, Arrays.asList(Arrays.copyOfRange(studentArray, 1, 3)));
Student temp = studentList.get(0);
System.out.println(temp.getName() + " 地址:" + temp);
listForEach(studentList);
Set<Student> studentSet = new HashSet<Student>();
studentSet.addAll(Arrays.asList(studentArray));
setForEach(studentSet);
Map<String,Student> studentMap = new HashMap<String, Student>();
studentMap.put(s1.getId(), s1);
studentMap.put(s2.getId(), s2);
studentMap.put(s3.getId(), s3);
Student temp2 = studentMap.get("2");
System.out.println(temp2.getName() + " 地址:" + temp2);
mapForEach(studentMap);
}
}