1、ArrayList类
    1)ArrayList类概述
        · 底层数据结构是数组,查询快,增删慢
        · 线程不安全,效率高
    2)ArrayList案例
        · 存储字符串并遍历
        · 存储自定义对象并遍历
 
2、Vecor类
    1)Vector类概述
       · 底层数据结构是数组,查询快,增删慢
       · 线程安全,效率低
    2)Vector类特有功能
        · public void addElement(E obj):添加功能
        · public Object elementAt(int index):返回指定索引处的组件
        · public Enumeration elements():返回此向量的所有组件
    3)Vector案例
        · 存储字符串并遍历
        · 存储自定义对象并遍历
 
3、LinkedList类
    1)LinkedList类概述        
  · 底层数据结构是链表,查询慢,增删快        
  · 线程不安全,效率高    
  2)LinkedList类特有功能        
  · public void addFirst(Object e)及addLast(Object e):在开头处或结尾处添加        
  · public Object getFirst()及getLast():获取第一个或最后一个元素        
  · public Object removeFirst()及public Object removeLast():移除第一个或最后一个元素并返回被删除的元素    
  3)LinkedList案例
      · 存储字符串并遍历

      · 存储自定义对象并遍历
 
例子1:
  1. package arraylistdemos;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. /**
  5. * Created by gao on 15-12-15.
  6. */
  7. /*
  8. * List的子类特点:
  9. * ArrayList:
  10. * 底层数据结构是数组,查询快,增删慢
  11. * 线程不安全,效率高
  12. * Vector:
  13. * 底层数据结构是数组,查询快,增删慢
  14. * 线程安全,效率低
  15. * LinkedList:
  16. * 底层数据结构是链表,查询慢,增删快
  17. * 线程不安全,效率高
  18. *
  19. * 案例:
  20. * 使用List的任何子类存储字符串或者存储自定义对象并遍历。
  21. *
  22. * ArrayList的使用。
  23. * 存储字符串并遍历
  24. */
  25. public class ArrayListDemo01 {
  26. public static void main(String[] args) {
  27. //创建集合对象
  28. ArrayList arrayList = new ArrayList();
  29. //创建元素对象,并添加元素
  30. arrayList.add("believe ");
  31. arrayList.add("in ");
  32. arrayList.add("yourself ");
  33. //遍历
  34. Iterator it = arrayList.iterator();
  35. while(it.hasNext()){
  36. String s = (String)it.next();
  37. System.out.println(s);
  38. }
  39. System.out.println("----------------------");
  40. for(int x = 0; x < arrayList.size(); x++){
  41. String s = (String)arrayList.get(x);
  42. System.out.println(s);
  43. }
  44. }
  45. }

例子2:

  1. package arraylistdemos;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. /**
  5. * Created by gao on 15-12-15.
  6. */
  7. public class ArrayListDemo02 {
  8. public static void main(String[] args) {
  9. //创建集合对象
  10. ArrayList arrayList = new ArrayList();
  11. //创建学生对象
  12. Student s1 = new Student("武松", 30);
  13. Student s2 = new Student("鲁智深", 40);
  14. Student s3 = new Student("林冲", 36);
  15. Student s4 = new Student("杨志", 38);
  16. //添加元素
  17. arrayList.add(s1);
  18. arrayList.add(s2);
  19. arrayList.add(s3);
  20. arrayList.add(s4);
  21. //遍历
  22. Iterator it = arrayList.iterator();
  23. while (it.hasNext()) {
  24. Student s = (Student) it.next();
  25. System.out.println(s.getName() + "---" + s.getAge());
  26. }
  27. System.out.println("------------");
  28. for (int x = 0; x < arrayList.size(); x++) {
  29. Student s = (Student) arrayList.get(x);
  30. System.out.println(s.getName() + "---" + s.getAge());
  31. }
  32. }
  33. }

   以上两个例子的添加和遍历方法对Vector和LinkedList都是可行的,下面讲解Vector特有的功能。

 
4、Vector的特有功能:
  1)添加功能
    public void addElement(Object obj) -- add()
  2)获取功能
    public Object elementAt(int index) -- get()
    public Enumeration elements() -- Iterator iterator()
    boolean hasMoreElements() -- hasNext()
    Object nextElement() -- next()
   3)JDK升级的原因:
     A:安全
     B:效率
     C:简化书写
 
例子3:
  1. package vectordemos;
  2. import java.util.Enumeration;
  3. import java.util.Vector;
  4. /**
  5. * Created by gao on 15-12-15.
  6. */
  7. public class VectorDemo01 {
  8. public static void main(String[] args) {
  9. //创建集合对象
  10. Vector v = new Vector();
  11. //添加功能
  12. v.addElement("hello");
  13. v.addElement("world");
  14. v.addElement("java");
  15. //遍历
  16. for(int x = 0; x < v.size(); x++){
  17. String s = (String) v.elementAt(x);
  18. System.out.println(s);
  19. }
  20. System.out.println("-------------");
  21. Enumeration e = v.elements();
  22. while(e.hasMoreElements()){
  23. String s = (String) e.nextElement();
  24. System.out.println(s);
  25. }
  26. }
  27. }
5、LinkedList的特有功能:
        A:添加功能
            public void addFirst(Object e)
            public void addLast(Object e)
        B:获取功能
            public Object getFirst()
            public Obejct getLast()
        C:删除功能
            public Object removeFirst()
            public Object removeLast()
 
例子4:

  1. package linklistdemos;
  2. import java.util.LinkedList;
  3. /**
  4. * Created by gao on 15-12-15.
  5. */
  6. public class LinkListDemo01 {
  7. public static void main(String[] args) {
  8. // 创建集合对象
  9. LinkedList link = new LinkedList();
  10. // 添加元素
  11. link.add("hello");
  12. link.add("world");
  13. link.add("java");
  14. // public void addFirst(Object e)
  15. link.addFirst("javaee");
  16. // public void addLast(Object e)
  17. link.addLast("android");
  18. // public Object getFirst()
  19. System.out.println("getFirst:"+link.getFirst());
  20. // public Obejct getLast()
  21. System.out.println("getLast:"+link.getLast());
  22. System.out.println("link:"+link);
  23. // public Object removeFirst()
  24. System.out.println("removeFirst:"+link.removeFirst());
  25. // public Object removeLast()
  26. System.out.println("removeLast:"+link.removeLast());
  27. // 输出对象名
  28. System.out.println("link:"+link);
  29. }
  30. }

输出结果:

getFirst:javaee
getLast:android
link:[javaee, hello, world, java, android]
removeFirst:javaee
removeLast:android
link:[hello, world, java]
 
6、实例应用:ArrayList去除集合中字符串的重复值(字符串的内容相同)
方式一:创建新的集合
  1. package exercisedemos;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. /**
  5. * Created by gao on 15-12-15.
  6. */
  7. /*
  8. * ArrayList去除集合中字符串的重复值(字符串的内容相同)
  9. *
  10. * 分析:
  11. * A:创建集合对象
  12. * B:添加多个字符串元素(包含内容相同的)
  13. * C:创建新集合
  14. * D:遍历旧集合,获取得到每一个元素
  15. * E:拿这个元素到新集合去找,看有没有
  16. * 有:不搭理它
  17. * 没有:就添加到新集合
  18. * F:遍历新集合
  19. */
  20. public class Exercise01 {
  21. public static void main(String[] args) {
  22. // 创建集合对象
  23. ArrayList array = new ArrayList();
  24. // 添加多个字符串元素(包含内容相同的)
  25. array.add("hello");
  26. array.add("world");
  27. array.add("java");
  28. array.add("world");
  29. array.add("java");
  30. array.add("world");
  31. array.add("world");
  32. array.add("world");
  33. array.add("world");
  34. array.add("java");
  35. array.add("world");
  36. // 创建新集合
  37. ArrayList newArray = new ArrayList();
  38. // 遍历旧集合,获取得到每一个元素
  39. Iterator it = array.iterator();
  40. while(it.hasNext()){
  41. String s = (String) it.next();
  42. if(!newArray.contains(s)){
  43. newArray.add(s);
  44. }
  45. }
  46. Iterator it2 = newArray.iterator();
  47. while (it2.hasNext()){
  48. String s = (String) it2.next();
  49. System.out.println(s);
  50. }
  51. }
  52. }

方式二:不能创建新的集合

  1. package exercisedemos;
  2. import java.util.ArrayList;
  3. /**
  4. * Created by gao on 15-12-15.
  5. */
  6. /*
  7. * 需求:ArrayList去除集合中字符串的重复值(字符串的内容相同)
  8. * 要求:不能创建新的集合,就在以前的集合上做。
  9. */
  10. public class Exercise02 {
  11. public static void main(String[] args) {
  12. // 创建集合对象
  13. ArrayList array = new ArrayList();
  14. // 添加多个字符串元素(包含内容相同的)
  15. array.add("hello");
  16. array.add("world");
  17. array.add("java");
  18. array.add("world");
  19. array.add("java");
  20. array.add("world");
  21. array.add("world");
  22. array.add("world");
  23. array.add("world");
  24. array.add("java");
  25. array.add("world");
  26. // 由选择排序思想引入,我们就可以通过这种思想做这个题目
  27. // 拿0索引的依次和后面的比较,有就把后的干掉
  28. // 同理,拿1索引...
  29. for (int x = 0; x < array.size() - 1; x++) {
  30. for (int y = x + 1; y < array.size(); y++) {
  31. if (array.get(x).equals(array.get(y))) {
  32. array.remove(y);
  33. y--; //注意删除后会有一个替位
  34. }
  35. }
  36. }
  37. for (int x = 0; x < array.size(); x++) {
  38. String s = (String) array.get(x);
  39. System.out.println(s);
  40. }
  41. }
  42. }
7、实例应用:去除集合中自定义对象的重复值(对象的成员变量值都相同)
Student类:(重写equals方法)
  1. package exercisedemos;
  2. /**
  3. * Created by gao on 15-12-9.
  4. */
  5. public class Student {
  6. private String name;
  7. private int age;
  8. public Student() {
  9. }
  10. public Student(String name, int age) {
  11. this.name = name;
  12. this.age = age;
  13. }
  14. public String getName() {
  15. return name;
  16. }
  17. public void setName(String name) {
  18. this.name = name;
  19. }
  20. public int getAge() {
  21. return age;
  22. }
  23. public void setAge(int age) {
  24. this.age = age;
  25. }
  26. @Override
  27. public boolean equals(Object o) {
  28. if (this == o) return true;
  29. if (!(o instanceof Student)) return false;
  30. Student student = (Student) o;
  31. if (age != student.age) return false;
  32. if (!name.equals(student.name)) return false;
  33. return true;
  34. }
  35. }

测试类:

  1. package exercisedemos;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. /**
  5. * Created by gao on 15-12-15.
  6. */
  7. /*
  8. * 需求:去除集合中自定义对象的重复值(对象的成员变量值都相同)
  9. *
  10. * 我们按照和字符串一样的操作,发现出问题了。
  11. * 为什么呢?
  12. * 我们必须思考哪里会出问题?
  13. * 通过简单的分析,我们知道问题出现在了判断上。
  14. * 而这个判断功能是集合自己提供的,所以我们如果想很清楚的知道它是如何判断的,就应该去看源码。
  15. * contains()方法的底层依赖的是equals()方法。
  16. * 而我们的学生类中没有equals()方法,这个时候,默认使用的是它父亲Object的equals()方法
  17. * Object()的equals()默认比较的是地址值,所以,它们进去了。因为new的东西,地址值都不同。
  18. * 按照我们自己的需求,比较成员变量的值,重写equals()即可。
  19. * 自动生成即可。
  20. */
  21. public class Exercise03 {
  22. public static void main(String[] args) {
  23. // 创建集合对象
  24. ArrayList array = new ArrayList();
  25. // 创建学生对象
  26. Student s1 = new Student("林青霞", 27);
  27. Student s2 = new Student("林志玲", 40);
  28. Student s3 = new Student("凤姐", 35);
  29. Student s4 = new Student("芙蓉姐姐", 18);
  30. Student s5 = new Student("翠花", 16);
  31. Student s6 = new Student("林青霞", 27);
  32. Student s7 = new Student("林青霞", 18);
  33. // 添加元素
  34. array.add(s1);
  35. array.add(s2);
  36. array.add(s3);
  37. array.add(s4);
  38. array.add(s5);
  39. array.add(s6);
  40. array.add(s7);
  41. // 创建新集合
  42. ArrayList newArray = new ArrayList();
  43. Iterator it = array.iterator();
  44. while (it.hasNext()) {
  45. Student s = (Student) it.next();
  46. if (!newArray.contains(s)) {
  47. newArray.add(s);
  48. }
  49. }
  50. for (int x = 0; x < newArray.size(); x++) {
  51. Student s = (Student)newArray.get(x);
  52. System.out.println(s.getName()+"---"+s.getAge());
  53. }
  54. }
  55. }
输出结果:
林青霞---27
林志玲---40
凤姐---35
芙蓉姐姐---18
翠花---16
林青霞---18
 
8、实例应用:请用LinkedList模拟栈数据结构的集合,并测试
自定义Stack类:
  1. package exercisedemos;
  2. /**
  3. * Created by gao on 15-12-15.
  4. */
  5. import java.util.LinkedList;
  6. /**
  7. * 自定义的栈集合
  8. */
  9. public class MyStack {
  10. private LinkedList link;
  11. public MyStack(){
  12. link = new LinkedList();
  13. }
  14. public void add(Object obj){
  15. link.addFirst(obj);
  16. }
  17. //弹出并删除
  18. public Object get(){
  19. // return link.getFirst();
  20. return link.removeFirst();
  21. }
  22. public boolean isEmpty(){
  23. return link.isEmpty();
  24. }
  25. }

测试类:

  1. package exercisedemos;
  2. /**
  3. * Created by gao on 15-12-15.
  4. */
  5. /*
  6. * MyStack的测试
  7. */
  8. public class Exercise04 {
  9. public static void main(String[] args) {
  10. MyStack ms = new MyStack();
  11. ms.add("hello");
  12. ms.add("world");
  13. ms.add("java");
  14. while (!ms.isEmpty()){
  15. System.out.println(ms.get());
  16. }
  17. }
  18. }
输出结果:
java
world
hello
 
 
 

Java API —— ArrayList类 & Vector类 & LinkList类的更多相关文章

  1. 详解Java中ArrayList、Vector、LinkedList三者的异同点(转)

    本文转自http://my.oschina.net/zzw922cn/blog/491631 一.ArrayList ArrayList是一个可以处理变长数组的类型,这里不局限于“数”组,ArrayL ...

  2. 详解Java中ArrayList、Vector、LinkedList三者的异同点

    转载:https://my.oschina.net/zzw922cn/blog/491631 一.ArrayList ArrayList是一个可以处理变长数组的类型,这里不局限于"数&quo ...

  3. java 中 ArrayList LinkedList Vector 三者的异同点

    1.ArrayList和Vector都是基于数组实现的,所以查询速度很快,增加和删除(非最后一个节点)速度慢: Vector是线程安全的,ArrayList不是. 2.LinkedList 是一个双向 ...

  4. 比较Java数组,ArrayList,LinkedList,Vector 性能比较

    public class PerformanceTester { public static final int TIMES=100000; public static abstract class ...

  5. ArrayList 、Vector、 LinkList

    public class TestList {     public static void init(List list)     {         if(list!=null)          ...

  6. Java——(五)Collection之List集合、ArrayList和Vector实现类

    ------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 一.List集合 List集合代表一个元素有序.客重复的集合,集合中每个元素都有其对应的顺序索引 ...

  7. 集合(一)Collection、List、ArrayList和Vector

    一.Collection 集合存放在java.util包中,可以看作是集成好的数据结构,供你调用,十分方便,集合经常拿来和数组对比,其实我觉得没啥可比性,不过还是简单来看看它们的区别: 1.数组长度固 ...

  8. ArrayList、Vector、LinkedList的区别联系?

    1.ArrayList.Vector.LinkedList类都是java.util包中,均为可伸缩数组. 2.ArrayList和Vector底层都是数组实现的,所以,索引数据快,删除.插入数据慢. ...

  9. ArrayList、Vector、LinkedList的特点和区别

    ArrayList.Vector.LinkedList类均在java.util包中,均为可伸缩数组. 1)ArrayList和Vector都是基于存储元素的Object[] array来实现的,它们会 ...

随机推荐

  1. C# 反射学习总结

    C#中的反射可以使得程序集和类型(类.结构.委托.接口和枚举)以及类型中的成员(方法.字段.属性.事件.参数.构造函数等)都成为变量在编程中动态调用.

  2. MVC初学 - The type or namespace name 'DbContext' could not be found

    问题: The type or namespace name 'DbContext' could not be found (are you missing a using directive or ...

  3. 微软职位内部推荐-Sr SDE for Win Apps Ecosystem

    微软近期Open的职位: Job posting title: Senior Software Design Engineer Location: China, Beijing Level: 63 D ...

  4. 【转载】使用Axure制作App原型怎样设置尺寸?

    使用Axure制作App原型怎样设置尺寸? 原文地址:http://www.axure.us/2172/ 本文由原型库网站投稿,转载请注明出处. 最近有几位小伙伴儿都提出同样一个疑问:想用Axure设 ...

  5. inputstream与其他格式的转换

    1.InputStream 转换成InputSource . InputStream inputStream = request.getInputStream(); InputSource input ...

  6. VBS基础篇 - 条件语句

    经常地,当我们编写代码时,我们需要根据不同的判断执行不同操作,我们可以使用条件语句完成这个工作. If...Then...Else 在下面的情况中,您可以使用 If...Then...Else 语句: ...

  7. android 设置半透明

    对于Button和ImageButton 还有一些View 设置半透明或者透明都是通过 android:background="#b0000000" 这是就是半透明 android ...

  8. JavaScript string array 数组

    Array类可以如下定义: var aValues = new Array(); 如果预先知道数组的长度,可以用参数传递长度 var aValues = new Array(20); -------- ...

  9. WinForm中Component Class、User Control及Custom Control的区别和使用建议

    reference: http://blog.csdn.net/redstonehe/article/details/1536549 .NET Framework 为您提供了开发和实现新控件的能力.除 ...

  10. 3157: 国王奇遇记 & 3516: 国王奇遇记加强版 - BZOJ

    果然我数学不行啊,题解君: http://www.cnblogs.com/zhuohan123/p/3726933.html const h=; var fac,facinv,powm,s:..]of ...