//1.在指定的位置上添加一个元素
public void add(int index, E element);
//2.取得指定位置上的元素
public E get(int index);
//3.修改指定位置上的元素,返回原先的元素
public E set(int index, E element);
//4.为ListIterator接口实例化
public ListIterator<E> listIterator();
//5.删除指定位置上的元素,返回删除的那个元素的引用
public E remove(int index);
2.1 新的子类:ArrayList
[java] view plaincopy
public class ArrayList<E> extends AbstractList<E>
implements List<E>,RandomAccess,Cloneable,Serializable
2.1.1 范例:设置内容
[java] view plaincopy
import java.util.ArrayList;
import java.util.List;
public class ArrayListDemo01 {
public static void main(String[] args) {
List<String> all = new ArrayList<String>(); // 实例化List接口
all.add("hello"); // 向集合中增加内容
all.add("world"); // 向集合中增加内容
all.add("!!!"); // 向集合中增加内容
for (int x = 0; x < all.size(); x++) {
System.out.println(all.get(x)) ;
}
}
}
import java.util.HashSet;
import java.util.Set;
class Person{
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.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;
Person other = (Person) 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 String toString() {
return "姓名:" + this.name + ",年龄:" + this.age;
}
}
public class RepeatDemo {
public static void main(String[] args) {
Set<Person> all = new HashSet<Person>();
all.add(new Person("张三", 20));
all.add(new Person("李四", 20));
all.add(new Person("李四", 20));
all.add(new Person("王五", 19));
System.out.println(all);
}
}