30天学会JAVA—练习题(2021韩顺平)——Day14
List接口
public class A09 {
@SuppressWarnings("all")
public static void main(String[] args){
List a = new ArrayList();
a.add("hello1");
a.add("hello2");
a.add("hello3");
a.add("hello4");
a.add("hello5");
a.add("hello6");
a.add("hello7");
a.add("hello8");
a.add("hello9");
a.add("hello10");
a.add("hello11");
a.add(2, "一只懒鱼儿");
a.get(5);
a.remove(6);
a.set(7, "今日已完");
//迭代器遍历
// Iterator iterator = a.iterator();
// while((iterator.hasNext())){
// System.out.println(iterator.next());
// }
//增强for循环
for(Object obj: a){
//为什么List和ArrayList就不可以呢
System.out.println(obj);
}
//普通for循环
// for(int i = 0; i < a.size(); i++){
// System.out.println(a.get(i));
// }
}
}
class Book{
private String name;
private int price;
private String author;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Book(String name, int price, String author) {
super();
this.name = name;
this.price = price;
this.author = author;
}
@Override
public String toString() {
return "Book [name=" + name + ", price=" + price + ", author=" + author + "]";
}
}
public class A01 {
@SuppressWarnings("all")
public static void main(String[] args) {
//ArrayList
ArrayList arr = new ArrayList();
arr.add(new Book("红楼梦", 80, "曹雪芹"));
arr.add(new Book("java从入门到放弃", 60, "不知名者"));
arr.add(new Book("开始", 12, "AA"));
//LinkedList
// LinkedList arr = new LinkedList();
// arr.add(new Book("红楼梦", 80, "曹雪芹"));
// arr.add(new Book("java从入门到放弃", 60, "不知名者"));
// arr.add(new Book("开始", 12, "AA"));
//
// //Vector
// Vector arr = new Vector();
// arr.add(new Book("红楼梦", 80, "曹雪芹"));
// arr.add(new Book("java从入门到放弃", 60, "不知名者"));
// arr.add(new Book("开始", 12, "AA"));
//遍历
System.out.println("===原始list========");
for(Object o : arr){
System.out.println(o);
}
//冒泡排序
sort(arr);
System.out.println("===排序后list========");
for(Object o : arr){
System.out.println(o);
}
}
//对ArrayList进行排序
public static void sort(ArrayList list){
int listSize = list.size();
for(int i = 0; i < listSize -1 ;i++){
for(int j = 0; j < listSize -1 -i; j++) {
//取出对象Book 向下转型
Book book1 = (Book)list.get(j);
Book book2 = (Book)list.get(j+1);
if(book1.getPrice() > book2.getPrice()){
//用list的方法交换就好了
list.set(j, book2);
list.set(j+1, book1);
}
}
}
}
}
想要哪个值相同,勾选即可
public class A02 {
public static void main(String[] args) {
HashSet hashSet = new HashSet();
hashSet.add(new Employee("Ada", 24));
hashSet.add(new Employee("Jack", 32));
hashSet.add(new Employee("Ada", 24));
//放入了三个对象,因为hash值不同
System.out.println(hashSet);
}
}
class Employee{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Employee(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Employee [name=" + name + ", age=" + age + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
public class A03 {
@SuppressWarnings("all")
public static void main(String[] args) {
HashSet hashSet = new HashSet();
hashSet.add(new Employee("ada", 1000, new MyDate(1998,11,9)));
hashSet.add(new Employee("ada", 1000, new MyDate(1999,11,9)));
hashSet.add(new Employee("ada", 1000, new MyDate(1998,11,9)));
hashSet.add(new Employee("tom", 1000, new MyDate(1998,11,9)));
System.out.println(hashSet);
}
}
class Employee{
private String name;
private int sal;
private MyDate birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
public Employee(String name, int sal, MyDate birthday) {
super();
this.name = name;
this.sal = sal;
this.birthday = birthday;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((birthday == null) ? 0 : birthday.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (birthday == null) {
if (other.birthday != null)
return false;
} else if (!birthday.equals(other.birthday))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "Employee [name=" + name + ", sal=" + sal + ", birthday=" + birthday + "]" + "\n";
}
}
class MyDate{
int year;
int month;
int day;
public MyDate(int year, int month, int day) {
super();
this.year = year;
this.month = month;
this.day = day;
}
@Override
public String toString() {
return "MyDate [year=" + year + ", month=" + month + ", day=" + day + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + day;
result = prime * result + month;
result = prime * result + year;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MyDate other = (MyDate) obj;
if (day != other.day)
return false;
if (month != other.month)
return false;
if (year != other.year)
return false;
return true;
}
}
public class A02 {
public static void main(String[] args) {
LinkedHashSet linkedHashSet = new LinkedHashSet();
linkedHashSet.add(new Car("奥拓", 1000));
linkedHashSet.add(new Car("奥迪", 3000000));
linkedHashSet.add(new Car("法拉利", 30000000));
linkedHashSet.add(new Car("奥迪", 3000000));
linkedHashSet.add(new Car("保时捷", 700000000));
linkedHashSet.add(new Car("奥迪", 3000000));
System.out.println(linkedHashSet);
}
}
class Car{
private String name;
private double price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Car(String name, double price) {
super();
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "[name=" + name + ", price=" + price + "]" + "\n";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
long temp;
temp = Double.doubleToLongBits(price);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Car other = (Car) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
return false;
return true;
}
}
public class A04 {
@SuppressWarnings("all")
public static void main(String[] args) {
Map hashM = new HashMap();
hashM.put("001", new Employee("ada", 19000,"001"));
hashM.put("002", new Employee("jack", 16000,"002"));
hashM.put("003", new Employee("lucy", 18500,"003"));
//使用keySet(增强for循环)
Set keySet = hashM.keySet();
for(Object key : keySet){
//获取value 并向下转型
if(hashM.get(key) instanceof Employee){
Employee emp = (Employee) hashM.get(key);
if(emp.getSalary() > 18000){
System.out.println(emp);
}
}
}
//使用EntrySet(迭代器)
Set entrySet = hashM.entrySet();
Iterator iterator = entrySet.iterator();
while(iterator.hasNext()){
//向下转型为Map.Entry
Map.Entry entry = (Map.Entry) iterator.next();
Employee emp = (Employee) entry.getValue();
//通过entry取得key 和 value
if(emp.getSalary() > 18000){
System.out.println(emp);
}
}
}
}
class Employee{
private String name;
private int salary;
private String id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Employee(String name, int salary, String id) {
super();
this.name = name;
this.salary = salary;
this.id = id;
}
@Override
public String toString() {
return "\n"+"[name=" + name + ", salary=" + salary + ", id=" + id + "]";
}
}