List的子类:
(1)List的子类特点
ArrayList:
底层数据结构是数组,查询快,增删慢
线程不安全,效率高
Vector:
底层数据结构是数组,查询快,增删慢
线程安全,效率低
LinkedList:
底层数据结构是链表,查询慢,增删快
线程不安全,效率高
(2)ArrayList
A:没有特有功能需要学习
B:案例
a:ArrayList存储字符串并遍历
// 创建集合对象
ArrayList array = new ArrayList();
// 创建元素对象,并添加元素
array.add("hello");
array.add("world");
array.add("java");
// 遍历
Iterator it = array.iterator();
while (it.hasNext()) {
String s = (String) it.next();
System.out.println(s);
}
System.out.println("-----------");
for (int x = 0; x < array.size(); x++) {
String s = (String) array.get(x);
System.out.println(s);
}
b:ArrayList存储自定义对象并遍历
// 创建集合对象
ArrayList array = new ArrayList();
// 创建学生对象
Student s1 = new Student("武松", 30);
Student s2 = new Student("鲁智深", 40);
Student s3 = new Student("林冲", 36);
Student s4 = new Student("杨志", 38);
// 添加元素
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
// 遍历
Iterator it = array.iterator();
while (it.hasNext()) {
Student s = (Student) it.next();
System.out.println(s.getName() + "---" + s.getAge());
}
System.out.println("----------------");
for (int x = 0; x < array.size(); x++) {
// ClassCastException 注意,千万要搞清楚类型
// String s = (String) array.get(x);
// System.out.println(s);
Student s = (Student) array.get(x);
System.out.println(s.getName() + "---" + s.getAge());
(3)Vector
A:有特有功能
a:添加
public void addElement(E obj) -- add()
b:获取
public E elementAt(int index) -- get()
public Enumeration<E> elements() -- Iterator iterator()
boolean hasMoreElements() hasNext()
Object nextElement() next()
B:案例
Vector存储字符串并遍历
// 创建集合对象
Vector v = new Vector();
// 添加功能
v.addElement("hello");
v.addElement("world");
v.addElement("java");
// 遍历
for (int x = 0; x < v.size(); x++) {
String s = (String) v.elementAt(x);
System.out.println(s);
}
System.out.println("------------------");
Enumeration en = v.elements(); // 返回的是实现类的对象
while (en.hasMoreElements()) {
String s = (String) en.nextElement();
System.out.println(s);
}
(4)LinkedList
A:有特有功能
a:添加
public void addFirst(Object e)
public void addLast(Object e)
b:删除
public Object removeFirst()
public Object removeLast()
c:获取
public Object getFirst()
public Obejct getLast()
B:案例
a:LinkedList存储字符串并遍历
b:LinkedList存储自定义对象并遍历
(5)案例:
A:去除集合中的多个字符串的重复元素
import java.util.ArrayList;
import java.util.Iterator;
/*
* ArrayList去除集合中字符串的重复值(字符串的内容相同)
*
* 分析:
* A:创建集合对象
* B:添加多个字符串元素(包含内容相同的)
* C:创建新集合
* D:遍历旧集合,获取得到每一个元素
* E:拿这个元素到新集合去找,看有没有
* 有:不搭理它
* 没有:就添加到新集合
* F:遍历新集合
*/
public class ArrayListDemo {
public static void main(String[] args) {
// 创建集合对象
ArrayList array = new ArrayList();
// 添加多个字符串元素(包含内容相同的)
array.add("hello");
array.add("world");
array.add("java");
array.add("world");
array.add("java");
array.add("world");
array.add("world");
// 创建新集合
ArrayList newArray = new ArrayList();
// 遍历旧集合,获取得到每一个元素
Iterator it = array.iterator();
while (it.hasNext()) {
String s = (String) it.next();
// 拿这个元素到新集合去找,看有没有
if (!newArray.contains(s)) {
newArray.add(s);
}
}
// 遍历新集合
for (int x = 0; x < newArray.size(); x++) {
String s = (String) newArray.get(x);
System.out.println(s);
}
}
}
import java.util.ArrayList;
import java.util.Iterator;
/*
* 需求:ArrayList去除集合中字符串的重复值(字符串的内容相同)
* 要求:不能创建新的集合,就在以前的集合上做。
*/
public class ArrayListDemo2 {
public static void main(String[] args) {
// 创建集合对象
ArrayList array = new ArrayList();
// 添加多个字符串元素(包含内容相同的)
array.add("hello");
array.add("world");
array.add("java");
array.add("world");
array.add("java");
array.add("world");
array.add("world");
// 由选择排序思想引入
// 拿0索引的依次和后面的比较,有就把后的干掉
// 同理,拿1索引...
for (int x = 0; x < array.size() - 1; x++) {
for (int y = x + 1; y < array.size(); y++) {
if (array.get(x).equals(array.get(y))) {
array.remove(y);
y--;
}
}
}
// 遍历集合
Iterator it = array.iterator();
while (it.hasNext()) {
String s = (String) it.next();
System.out.println(s);
}
}
}
B:去除集合中的多个自定义对象的重复元素
import java.util.ArrayList;
import java.util.Iterator;
/*
* 需求:去除集合中自定义对象的重复值(对象的成员变量值都相同)
*
* 按照和字符串一样的操作,发现出问题了。
* 为什么呢?
* 思考哪里会出问题?
* 通过简单的分析,知道问题出现在了判断上。
* 而这个判断功能是集合自己提供的,所以我们如果想很清楚的知道它是如何判断的,就应该去看源码。
* contains()方法的底层依赖的是equals()方法。
* 而我们的学生类中没有equals()方法,这个时候,默认使用的是它父亲Object的equals()方法
* Object()的equals()默认比较的是地址值,所以,它们进去了。因为new的东西,地址值都不同。
* 按照我们自己的需求,比较成员变量的值,重写equals()即可。
* 自动生成即可。
*/
public class ArrayListDemo3 {
public static void main(String[] args) {
// 创建集合对象
ArrayList array = new ArrayList();
// 创建学生对象
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("林志玲", 40);
Student s3 = new Student("凤姐", 35);
Student s4 = new Student("芙蓉姐姐", 18);
Student s5 = new Student("翠花", 16);
Student s6 = new Student("林青霞", 27);
Student s7 = new Student("林青霞", 18);
// 添加元素
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
array.add(s5);
array.add(s6);
array.add(s7);
// 创建新集合
ArrayList newArray = new ArrayList();
// 遍历旧集合,获取得到每一个元素
Iterator it = array.iterator();
while (it.hasNext()) {
Student s = (Student) it.next();
// 拿这个元素到新集合去找,看有没有
if (!newArray.contains(s)) {
newArray.add(s);
}
}
// 遍历新集合
for (int x = 0; x < newArray.size(); x++) {
Student s = (Student) newArray.get(x);
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
C:用LinkedList模拟一个栈数据结构的集合类,并测试。
import java.util.LinkedList;
/**
* 自定义的栈集合
* A: LinkedList的特有添加功能addFirst() B:栈的特点先进后出
* @author
* @version V1.0
*/
public class MyStack {
private LinkedList link;
public MyStack() {
link = new LinkedList();
}
public void add(Object obj) {
link.addFirst(obj);
}
public Object get() {
// return link.getFirst();
return link.removeFirst();
}
public boolean isEmpty() {
return link.isEmpty();
}
}
/*
* MyStack的测试
*/
public class MyStackDemo {
public static void main(String[] args) {
// 创建集合对象
MyStack ms = new MyStack();
// 添加元素
ms.add("hello");
ms.add("world");
ms.add("java");
// System.out.println(ms.get());
// System.out.println(ms.get());
// System.out.println(ms.get());
// NoSuchElementException
// System.out.println(ms.get());
while(!ms.isEmpty()){
System.out.println(ms.get());
}
}
}