/**
* 同步--安全
* 去银行办理业务
* 多个窗口 多线程同步。。
* 同步--安全 -- 效率低
* @author Angus
*
* List:
* |--ArrayList
* List 接口的大小可变数组的实现 ,注意,此实现不是同步的
* 查询快,增删慢,线程不安全,效率高
* |--Vector
* Vector 类可以实现可增长的对象数组 同步 ,几乎不用
* |--LinkedList
* List 接口的链接列表实现 ,此实现不是同步的
* 查询慢,增删快,线程不安全,效率高
*/
ArrayList
测试类 :学生的姓名和年龄相同即可去重
package list;
import java.util.ArrayList;
import java.util.Iterator;
/**
*
* @author Angus
*
* ArrayList
* 如果存储学生,去重
* 问题:
* 如何去重
* 需求:
* 学生的姓名和年龄相同即可去重
*/
public class ListDemo1 {
public static void main(String[] args) {
ArrayList array = new ArrayList();
Student s1 = new Student("hehe1", 11);
Student s2 = new Student("hehe2", 12);
Student s3 = new Student("hehe3", 13);
Student s4 = new Student("hehe4", 14);
Student s5 = new Student("hehe5", 15);
Student s6 = new Student("hehe5", 15);
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
array.add(s5);
array.add(s6);
ArrayList array2 = new ArrayList();
// 遍历
Iterator it = array.iterator();
while (it.hasNext()) {
Student s = (Student) it.next();
if (!array2.contains(s)) {
array2.add(s);
}
}
for (int i = 0; i < array2.size(); i++) {
Student s = (Student) array2.get(i);
System.out.println(s);
}
}
}
运行结果:
发现并没有去重,hehe5还是两个,我们根据代码发现,应该contains 可能有问题,跟进源码:
/**
* Returns <tt>true</tt> if this list contains the specified element.
* More formally, returns <tt>true</tt> if and only if this list contains
* at least one element <tt>e</tt> such that
* <tt>(o==null ? e==null : o.equals(e))</tt>.
*
* @param o element whose presence in this list is to be tested
* @return <tt>true</tt> if this list contains the specified element
*/
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
走的indexOf方法,继续跟进:
/**
* Returns the index of the first occurrence of the specified element
* in this list, or -1 if this list does not contain the element.
* More formally, returns the lowest index <tt>i</tt> such that
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
* or -1 if there is no such index.
*/
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
这次里面走的equals方法,而equals方法比较的是对象的地址值,每个Student都是new的对象,地址值不一样,所以没有去重。这样就需要重写equals方法来实现。
在学生类中重写equals方法。
package list;
/**
* 标准学生类
* @author Angus
*
*/
public class Student {
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = 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;
}
@Override
public String toString() {
return "Student [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;
Student other = (Student) 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;
}
}
之后运行结果去重完成:
Vector
了解就行,基本不用。。。
package list;
import java.util.Enumeration;
import java.util.Vector;
/**
* Vector
* @author Angus
* 特有功能:
* 添加:
* public void addElement(E obj)将指定的组件添加到此向量的末尾,将其大小增加 1。如果向量的大小比容量大,则增大其容量。
* 获取:
* public E elementAt(int index)返回指定索引处的组件。
* public Enumeration<E> elements()返回此向量的组件的枚举
* 长度:
* public int size()返回此向量中的组件数
*/
public class VectorDemo {
public static void main(String[] args) {
Vector v = new Vector();
//添加
v.addElement("hello");
v.addElement("world");
v.addElement("java");
System.out.println(v.elementAt(0));
System.out.println(v.elementAt(1));
System.out.println(v.elementAt(2));
//遍历
for(int x = 0; x<v.size();x++){
String s = (String) v.elementAt(x);
System.out.println(s);
}
System.out.println("-----------------------------");
//遍历 相当于 Iterator
Enumeration elements = v.elements();
while(elements.hasMoreElements()){
String s = (String) elements.nextElement();
System.out.println(s);
}
}
}
LinkedList
package list;
import java.util.LinkedList;
/**
* LinkedList
* @author Angus
* 特有功能:
* 添加:
* addFirst(E e) 将指定元素插入此列表的开头。
* void addLast(E e) 将指定元素添加到此列表的结尾。
* 获取:
* getFirst() 返回此列表的第一个元素。
* getLast() 返回此列表的最后一个元素。
* 删除:
* removeFirst() 移除并返回此列表的第一个元素。
* removeLast() 移除并返回此列表的最后一个元素。
*
*/
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList ll = new LinkedList();
ll.add("hello");
ll.add("world");
ll.add("java");
System.out.println(ll);
System.out.println("----------------");
ll.addFirst("hahh");
ll.addLast("jjjj");
System.out.println(ll);
System.out.println("--------------------");
System.out.println(ll.getFirst());
System.out.println(ll.getLast());
System.out.println("--------------------");
System.out.println(ll.removeFirst());
System.out.println(ll.removeLast());
}
}
结果:
用LinkedList模式栈的数据结构:
package list;
import java.util.Iterator;
import java.util.LinkedList;
/**
*
* @author Angus
* LinkedList模拟栈的数据结构
*
* 特点;
* 先进后出
* LinkedList模拟栈的数据结构:
* 实际意思,有一个LinkedList可以用,你需要自定义一个栈的集合
* 对外提供获取和添加功能
*
*/
public class LinkedListDemo2 {
public static void main(String[] args) {
LinkedList ll = new LinkedList();
ll.add("hello");
ll.add("world");
ll.add("java");
Iterator it = ll.iterator();
while(it.hasNext()){
String s = (String) it.next();
System.out.println(s); //本身就是先进后出模式。。。。
}
//以上方式错误。。。。。
MyTaskDemo ms = new MyTaskDemo();
ms.add("hello");
ms.add("world");
ms.add("java");
System.out.println(ms.get(0));
System.out.println(ms.get(1));
System.out.println(ms.get(2));
for (int i = 0; i < ms.sise(); i++) {
System.out.println(ms.get(i));
}
}
}
MyTastDemo
package list;
import java.util.LinkedList;
/**
* LinkedList模拟栈的数据结构
* @author Angus
*
*/
public class MyTaskDemo {
private LinkedList link;
public MyTaskDemo(){
link = new LinkedList();
}
public void add (Object obj){
link.addFirst(obj);
}
public Object get(int index){
return link.get(index);
}
public int sise(){
return link.size();
}
}