一、List把静态数组封装成动态数组。有序列表
二、ArrayList是常用的数组类,有添加,删除,修改多种方法
三、案列
ListDemo.java
package com.kouding;
import java.util.ArrayList;
import java.util.List;
public class ListDemo {
public static void main(String[] args) {
// List<泛型> 变量 = new ArrayList<>();
List<Dog> dogList = new ArrayList<>();
Dog dog1 = new Dog(1, "菲菲");
dogList.add(dog1);
dogList.add(dog1);
dogList.add(dog1);
dogList.add(dog1);
}
}
Dog.java
package com.kouding;
public class Dog {
private int age;
private String name;
public Dog(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Dog dog = (Dog) o;
if (age != dog.age) return false;
return name.equals(dog.name);
}
@Override
public String toString() {
return "Dog{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
@Override
public int hashCode() {
int result = age;
result = 31 * result + name.hashCode();
return result;
}
}
网友评论