ArrayList集合、Vector集合和LinkedList集合的学习

ArrayList是List接口中常用的一个子实现类

底层是数据结构式数组结构,

其特点有:

            查询快,增删慢

    从内存角度考虑:线程不安全的,不同步的,执行效率高。

    多线程:synchronized :同步的意思 解决线程安全问题
                     sychronized(锁对象){ 同步代码 
共享数据;
}

解决线程安全问题,通过同步可以解决,但是效率低了...

 Vector集合

底层是一种可增长对象数组,

其特点有:

                查询快,增删慢;

   从内存角度考虑:线程安全,同步,执行效率高。
 
  特有功能:
  public void addElement(Object obj)------->add(Object obj)
public Enumeration elements():返回此向量的枚举--->相当于:public Iterator iterator()
   boolean hasMoreElements()  --->boolean hasNext() ;

Object nextElement()   --->Object next() ;

LinkedList集合的特点:

  底层是一种链表实现,

其特点有:

                查询慢,增删快;

  从内存角度考虑:线程不安全,不同步,执行效率高。
 
特有功能:
  添加功能
  addFirst(Object e):将指定的元素插入到列表的开头
addLast(object e):将指定的元素添加到列表末尾
  获取功能:
  getFirst():获取列表第一个元素
  getLast():获取列表第二个元素
 
删除功能
  public Object removeFirst()移除并返回此列表的第一个元素。
  public Object removeLast();

 每个集合都有自己特有的方法,我们先看ArrayList的几种方法

(1)遍历功能:
1)一个是Collection的iterator()

  2)size()和get(int index)普通for循环

我们可以通过下面这段代码了解一下。

public static void main(String[] args) {
		
		//创建集合对象,记得导包Ctrl+Shift+O
		ArrayList list = new ArrayList() ;
		
		//添加元素
		list.add("you") ;
		list.add("are") ;
		list.add("beautiful") ;
		
		
		//遍历,有两种方式
		//获取迭代器,导包
		Iterator it = list.iterator() ;
		while(it.hasNext()) {
			String s = (String)it.next() ;
			System.out.println(s);
		}
		
		System.out.println("*******************");
		
		for(int x = 0 ; x < list.size() ; x ++) {
			String s1 = (String) list.get(x) ;
			System.out.println(s1);
		}
		
	}//you  are  beautiful

学习了上面的方法,我们再试着实现下面这个功能。

需求:ArrayList集合存储自定义对象并遍历

分析:(1)创建一个学生类;

         (2)在ArrayListDemo类中进行实现;

public class Student {
	
	private String name ;
	private int age ;
	//无参构造
	public Student() {
		super();
	}
        //有参构造
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
        //setxxgetxx方法
	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;
	}
	
	
public class ArrayListDemo2 {
	
	public static void main(String[] args) {
		
		//创建ArrayList集合对象
		ArrayList  array = new ArrayList();
		
		//创建学生对象
		Student s1 = new Student("小燕子", 18) ;
		Student s2 = new Student("尔康", 20) ;
		Student s3 = new Student("紫薇", 17) ;
		Student s4 = new Student("永琪", 21) ;
		
                //添加
		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 ++) {
			Student s1 = (Student)array.get(x) ;
			System.out.println(s1.getName()+"     "+s1.getAge());
		}
	}

接下来,我们通过很简单的一段代码学习Vector集合的添加及遍历元素的方法

public static void main(String[] args) {
		
		//创建Vector集合对象
		Vector v = new Vector() ;
		
		//添加元素
		v.addElement("you"); 
		v.addElement("are");
		v.addElement("beautiful");
		
		//遍历
		//获取Enumeration :向量的枚举
		Enumeration en = v.elements() ;
		while(en.hasMoreElements()) {
			String s = (String)en.nextElement() ;
			System.out.println(s);
		}
	}//you  are  beautiful

我们再学习LinkedList集合的几个重要方法,前面我已经提到它有添加功能,获取功能,删除功能

public static void main(String[] args) {
		
		//创建LinkedList集合对象
		LinkedList link = new LinkedList() ;
		
		//添加元素
		link.add("hello"); 
		link.add("world"); 
		link.add("java"); 
		System.out.println("link:"+link);
		
		//addFirst(Object e):将指定的元素插入到列表的开头
		link.addFirst("android");
		System.out.println("link:"+link);
		link.addLast("mysql");
		System.out.println(link);
		
		//getFirst():获取列表第一个元素
		System.out.println(link.getFirst());
		System.out.println(link.getLast());
		
		//public Object removeFirst()移除并返回此列表的第一个元素。 
		System.out.println("removeFirst():"+link.removeFirst());
		System.out.println(link);
		//public Object removeLast()
		System.out.println(link.removeLast());
		System.out.println(link);
	}//link:[hello, world, java]
         //link:[android, hello, world, java]
         //android, hello, world, java, mysql]
         //android
         //mysql
         // removeFirst():android
         // [hello, world, java, mysql]
         // mysql
         //[hello, world, java]

输出结果如上注释

学了这么多,我们做一个小练习吧。

需求:
  模拟栈结构的特点,先进后出
  

  分析:我们需要自定义一个类,在这个类中使用LinkedList集合的特有功能进行操作

import java.util.LinkedList;

/**
 * 这是自定义的栈集合类
 * 	在这个MyStack中使用LinkedList的特有功能
 * 
 */
public class MyStack {
	//成员变量
	private LinkedList link ;
		
	/**
	 * 执行MyStack的无参构造实际是构造了个LinkedList对象
	 */
	public MyStack() {
		link = new LinkedList() ;
	}
	
	//添加元素
	public void add(Object obj) {
		link.addFirst(obj);
	}
	
	//获取元素
	public Object get() {
		return link.removeFirst() ;
	}
	
	
	//判断功能
	public boolean isEmpty() {
		return link.isEmpty();
	}
	
	
}

public class MyStackDemo {
	
	public static void main(String[] args) {
		
		//创建自定义的MyStack的栈集合的对象
		MyStack my = new MyStack() ;	//LinkedList list = new LinkedList();
		
		//添加元素
		my.add("hello");		//LinkedList ["java","world","hello"]
		my.add("world");
		my.add("java");
		
		while(!my.isEmpty()) {
			System.out.println(my.get());
		}
	}
}//java
 //world
 //hello

学了这么多,我们再看一个关于ArrayList集合的小练习

需求:给集合中添加重复的元素(字符串类型),将重复的元素从集合去除掉。

思路:
1)创建一个A集合,添加重复元素,
2)创建一个B集合,
3)遍历A集合中的元素获取到每一个元素,
A集合中判断B集合中是否包含A集合中的元素,
包含,不搭理
不包含,添加到B集合中

                4)遍历B集合。

public static void main(String[] args) {
		
		//创建一个集合
		ArrayList list = new ArrayList() ;
		
		//添加元素
		list.add("you") ;
		list.add("are") ;
		list.add("beautiful") ;
		list.add("you") ;
		list.add("are") ;
		list.add("so") ;
		list.add("beautiful") ;
		list.add("you") ;
		list.add("are") ;
		list.add("very") ;
		list.add("nice") ;
		list.add("great") ;
		
		//创建一个新的集合
		ArrayList newList = new ArrayList() ;
		
		//遍历旧集合,使用第一种遍历方式,如果现在忘记第二种遍历方式了,现在点上去复习一下哟~
		Iterator it = list.iterator() ;
		while(it.hasNext()) {
			String s = (String) it.next() ;
			
			//判断新集合中是否包含旧集合中的元素
			if(!newList.contains(s)) {
				newList.add(s) ;
			}
		}
		
		//遍历新集合
		Iterator it2 = newList.iterator() ;
		while(it2.hasNext()) {
			String s = (String) it2.next() ;
			System.out.println(s);
		}
	}   //you
            //are
            //beaytiful
            //so
            //beautiful
            //very
            //nice
            //great

这是其中一种方法,现在有这样的要求:不允许新建一个集合去完成,怎么办?

 
 
public static void main(String[] args) {
	ArrayList list= new ArrayList();
	list.add("hello");
	list.add("world");
	list.add("hello");
	list.add("java");
	list.add("hello");
	list.add("pythe");
	list.add("you");
	list.add("great");
	/**
		 * 引入选择排序,
		 * 用0索引对应的元素依次和后面索引对应的元素进行比较
		 * 	如果前面的元素和后面的元素重复了,把后面元的干掉
		 * 依次1索引....
		 */
	for(int x = 0;x<list.size()-1;x++) {
		for(int y=x+1;y<list.size();y++) {
			if(list.get(x).equals(list.get(y))) {
				list.remove(y);
				y--;
				}
			}
		
		}
			Iterator it=list.iterator();
				while(it.hasNext()) {
						String s = (String)it.next();
							System.out.println(s);
						}

	}//hello
        //world
        //java
        //pythe
        //you
        //great


好的,这一小节,我就先讲到这里。

猜你喜欢

转载自blog.csdn.net/luzhu1234/article/details/80227685