List 接口
ArrayList 和 LinkedList 区别
ArrayList : 数组实现
数组实现 特点 : 查询快 增删慢(参照与LinkedList)
查询 : 直接使用角标查询
增删 : 需要把要添加的元素的位置 后面的元素 全部移动一位
LinkedList : 链表实现
链表实现 特点 : 查询慢 增删快(参照与ArrayList)
查询 : 判断离头进还是尾近 然后从头或尾 一个一个查找 找到为止
增删 : 将要插入的元素的地址保存 后面的元素 不用进行任何的改动
public static void fun1() {
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add(4, "x");
System.out.println(list);
Object object = list.get(2);
System.out.println(object);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
list.set(4, "z");
System.out.println(list);
Object remove = list.remove(3);
System.out.println(remove);
System.out.println(list);
}
/**
* 并发修改异常
*/
public static void fun3() {
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
if (iterator.next().equals("b")) {
list.add("oh~yes");
}
}
System.out.println(list);
}
public static void fun4() {
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
ListIterator listIterator = list.listIterator();
while (listIterator.hasNext()) {
if (listIterator.next().equals("b")) {
listIterator.add("Oh~Yes");
}
}
System.out.println(list);
}
public static void fun5() {
Vector vector = new Vector();
vector.addElement("a");
vector.addElement("b");
vector.addElement("c");
vector.addElement("d");
System.out.println(vector);
Enumeration elements = vector.elements();
while (elements.hasMoreElements()) {
System.out.println(elements.nextElement());
}
}
public static void fun6() {
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
ListIterator listIterator = list.listIterator();
while (listIterator.hasNext()) {
System.out.println(listIterator.next());
}
while (listIterator.hasPrevious()) {
System.out.println(listIterator.previous());
}
}
// linkedList
LinkedList linkedList = new LinkedList()
// 使用addfrist方法添加
linkedList.addLast("a")
linkedList.addLast("b")
linkedList.addLast("c")
System.out.println(linkedList)
// 获取头尾元素
System.out.println(linkedList.getFirst())
System.out.println(linkedList.getLast())
}
public static void fun1() {
LinkedList list = new LinkedList();
list.addLast("a");
list.addLast("b");
list.addLast("c");
list.addLast("d");
System.out.println(list);
while (!list.isEmpty()) {
Object removeLast = list.removeLast();
System.out.println(removeLast);
System.out.println(list);
}
System.out.println(list);
}
/**
* ArrayList的去重
*/
public static void fun2() {
ArrayList arrayList = new ArrayList();
arrayList.add("a");
arrayList.add("a");
arrayList.add("a");
arrayList.add("b");
arrayList.add("b");
arrayList.add("b");
arrayList.add("c");
arrayList.add("c");
arrayList.add("c");
ArrayList arrayList2 = new ArrayList();
Iterator iterator = arrayList.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
if (!arrayList2.contains(next)) {
arrayList2.add(next);
}
}
System.out.println(arrayList2);
}
ArrayList list = new ArrayList();
list.add(new Student("鹏鹏", 18));
list.add(new Student("鹏鹏", 18));
list.add(new Student("水水", 17));
list.add(new Student("水水", 17));
list.add(new Student("茜茜", 16));
list.add(new Student("茜茜", 16));
ArrayList newlist = new ArrayList();
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
Student student = (Student)next;
if (!newlist.contains(student)) {
newlist.add(student);
}
}
for (Object object : newlist) {
Student student = (Student)object;
System.out.println(student);
}
}