Java Collections Framework (in) - List collection

table of Contents

Collection Case (consolidation of knowledge)

A, List interfaces

List Case:

 List collection of unique features: 

 ListIterator members of the interface methods

 

List three common sub-set of interfaces:

1, ArrayList class overview and use

2, Vector class overview and use

3, LinkedList class overview and use

List collection exercise


Collection Case (consolidation of knowledge)

  1. Storage string and ergodic
  2. Store custom objects and traverse
  • Student(name,age)

1, the storage string and traverse

Requirements: storage string and ergodic.
  
 Analysis:
          A: Create collection object
          B: Create string object
          C: adding a String object to set
          D: through the collection
 

as follows:

package cn.wen_01;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionTest {
	public static void main(String[] args) {
		// 创建集合对象
		Collection c = new ArrayList();

		// 创建字符串对象
		// 把字符串对象添加到集合中
		c.add("小明");
		c.add("小何");
		c.add("小文");
		c.add("小马");
		c.add("小亮");

		// 遍历集合
		// 通过集合对象获取迭代器对象
		Iterator it = c.iterator();
		// 通过迭代器对象的hasNext()方法判断有没有元素
		while (it.hasNext()) {
			// 通过迭代器对象的next()方法获取元素
			String s = (String) it.next();
			System.out.println(s);
		}
	}
}

2, the storage and traversing custom objects

 Demand: custom object storage and traversing Student (name, age)
 
  Analysis:
          A: Create Student Class
          B: Create collection object
          C: Create Object Student
          D: add objects to the collection object Student
          E: through the collection
 

Student categories:

package cn.wen;

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;
	}

	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;
	}

}

Test categories:

package cn.wen;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionTest {
	public static void main(String[] args) {
		// 创建集合对象
		Collection c = new ArrayList();

		// 创建学生对象
		Student s1 = new Student("小明", 25);
		Student s2 = new Student("小王", 16);
		Student s3 = new Student("小亮", 20);
		Student s4 = new Student();
		s4.setName("小红");
		s4.setAge(26);

		// 把学生对象添加到集合对象中
		c.add(s1);
		c.add(s2);
		c.add(s3);
		c.add(s4);
		c.add(new Student("小文", 18)); // 匿名对象

		// 遍历集合
		Iterator it = c.iterator();
		while (it.hasNext()) {
			Student s = (Student) it.next();
			System.out.println(s.getName() + "---" + s.getAge());
		}
	}
}

A, List interfaces

Review of knowledge:

Collection Interface Interface List is the parent, and Set Queue interface methods defined in this interface for operating both set Set, also be used to operate a set of List and Queue.

List Interface Overview
  • Ordered Collection ( also known as a sequence ). This user interface can be accurately controlled to the insertion position of each list element. Users can access elements based integer index (position in the list), and search for elements in the list .
  • And set different lists typically allow duplicate elements .

List element ordered set represents a repeatable set, the set of each element has its corresponding sequence index. List allows repeated collection element, the collection can be accessed by the element at the specified index. List default settings set by the order of addition of the element index of the element, for example, to add the first element index is 0, the second element index is added L ......

List as a sub-interface Collection interface, of course, you can use the interface Co llection in all methods. And because List is an ordered collection, thus increasing the List collection in a number of ways to manipulate the index element in the collection.

Collection with a collection of school before learning the List

List Case  :

1, the storage string and traverse
2, the storage and traversing custom objects
 
package cn.wen_01;

import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

/*
 * 需求:List集合存储字符串并遍历。
 */
public class ListDemo {
	public static void main(String[] args) {
		// 创建集合对象
		List list = new ArrayList();

		// 创建字符串并添加字符串
		list.add("hello");
		list.add("world");
		list.add("java");

		// 遍历集合
		Iterator it = list.iterator();
		while (it.hasNext()) {
			String s = (String) it.next();
			System.out.println(s);
		}
	}
}

 

package cn.wen_02;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/*
 * List集合的特点:
 * 		有序(存储和取出的元素一致),可重复的。
 */
public class ListDemo2 {
	public static void main(String[] args) {
		// 创建集合对象
		List list = new ArrayList();

		// 存储元素
		list.add("hello");
		list.add("world");
		list.add("java");
		list.add("javaee");
		list.add("android");
		list.add("javaee");
		list.add("android");

		// 遍历集合
		Iterator it = list.iterator();
		while (it.hasNext()) {
			String s = (String) it.next();
			System.out.println(s);
		}
	}
}

 List collection of unique features: 


 A: Adding functionality
          void add (int index, Object element ): additive element in the specified location
 B: acquisition function
         Object get (int index): Get the specified position of the element
 C: list iterator
          ListIterator listIterator (): List set specific iteration is
 D: delete function
          Object remove (int index): delete the index element, return the deleted elements
 E: editing
         Object set (int index, Object element ): modify elements according to the index, return the modified elements
 

  • As part of the function test code:
package cn.wen_03;

import java.util.ArrayList;
import java.util.List;

public class ListDemo {
	public static void main(String[] args) {
		// 创建集合对象
		List list = new ArrayList();

		// 添加元素
		list.add("hello");
		list.add("world");
		list.add("java");

		// void add(int index,Object element):在指定位置添加元素
		// list.add(1, "android");//没有问题
		// IndexOutOfBoundsException
		// list.add(11, "javaee");//有问题(异常)
		// list.add(3, "javaee"); //没有问题
		// list.add(4, "javaee"); //有问题

		// Object get(int index):获取指定位置的元素
		// System.out.println("get:" + list.get(1));
		// IndexOutOfBoundsException
		// System.out.println("get:" + list.get(11));

		// Object remove(int index):根据索引删除元素,返回被删除的元素
		// System.out.println("remove:" + list.remove(1));
		// IndexOutOfBoundsException
		// System.out.println("remove:" + list.remove(11));

		// Object set(int index,Object element):根据索引修改元素,返回被修饰的元素
		System.out.println("set:" + list.set(1, "javaee"));

		System.out.println("list:" + list);
	}
}
  • List traverse unique set of features:

           size () and get () method in combination with

Code testing as follows:

package cn.wen_04;

import java.util.ArrayList;
import java.util.List;

/*
 * List集合的特有遍历功能:
 * 		size()和get()方法结合使用
 */
public class ListDemo2 {
	public static void main(String[] args) {
		// 创建集合对象
		List list = new ArrayList();

		// 添加元素
		list.add("hello");
		list.add("world");
		list.add("java");

		// Object get(int index):获取指定位置的元素
		// System.out.println(list.get(0));
		// System.out.println(list.get(1));
		// System.out.println(list.get(2));
		// IndexOutOfBoundsException
		// System.out.println(list.get(3));

		// 用循环改进
		// for (int x = 0; x < 3; x++) {
		// System.out.println(list.get(x));
		// }
		// 如果元素过多,数起来就比较麻烦,所以我们使用集合的一个长度功能:size()
		// 最终的遍历方式就是:size()和get()
		for (int x = 0; x < list.size(); x++) {
			// System.out.println(list.get(x));

			String s = (String) list.get(x);
			System.out.println(s);
		}
	}
}

Small exercises

Student categories:

package cn.wen;

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;
	}

	// 成员方法
	// getXxx()/setXxx()
	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;
	}
}

Test categories:

package cn.wen;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/*
 * 存储自定义对象并遍历,用普通for循环。(size()和get()结合)
 */
public class ListDemo3 {
	public static void main(String[] args) {
		// 创建集合对象
		List list = new ArrayList();

		// 创建学生对象
		Student s1 = new Student("小东", 18);
		Student s2 = new Student("小明", 88);
		Student s3 = new Student("小红", 38);

		// 把学生添加到集合中
		list.add(s1);
		list.add(s2);
		list.add(s3);

		// 遍历
		// 迭代器遍历
		Iterator it = list.iterator();
		while (it.hasNext()) {
			Student s = (Student) it.next();
			System.out.println(s.getName() + "---" + s.getAge());
		}
		System.out.println("--------");

		// 普通for循环
		for (int x = 0; x < list.size(); x++) {
			Student s = (Student) list.get(x);
			System.out.println(s.getName() + "---" + s.getAge());
		}
	}
}

 

 ListIterator members of the interface methods

  • boolean hasPrevious()
  • E previous()
  • ConcurrentModificationException
phenomenon
the reason
solution
 

Code testing as follows: 

package cn.wen;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/*
 * 列表迭代器:
 * 		ListIterator listIterator():List集合特有的迭代器
 * 		该迭代器继承了Iterator迭代器,所以,就可以直接使用hasNext()和next()方法。
 * 
 * 特有功能:
 * 		Object previous():获取上一个元素
 * 		boolean hasPrevious():判断是否有元素
 * 
 * 		注意:ListIterator可以实现逆向遍历,但是必须先正向遍历,才能逆向遍历,所以一般无意义,不使用。
 */
public class ListIteratorDemo {
	public static void main(String[] args) {
		// 创建List集合对象
		List list = new ArrayList();
		list.add("hello");
		list.add("world");
		list.add("java");

		// ListIterator listIterator()
		ListIterator lit = list.listIterator(); // 子类对象
		// while (lit.hasNext()) {
		// String s = (String) lit.next();
		// System.out.println(s);
		// }
		// System.out.println("-----------------");
		
		// System.out.println(lit.previous());
		// System.out.println(lit.previous());
		// System.out.println(lit.previous());
		// NoSuchElementException
		// System.out.println(lit.previous());

		while (lit.hasPrevious()) {
			String s = (String) lit.previous();
			System.out.println(s);
		}
		System.out.println("-----------------");

		// 迭代器
		Iterator it = list.iterator();
		while (it.hasNext()) {
			String s = (String) it.next();
			System.out.println(s);
		}
		System.out.println("-----------------");

	}
}
  •  Note small problem:
package cn.itcast_04;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/*
 * 问题?
 * 		我有一个集合,如下,请问,我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现。
 * 
 * ConcurrentModificationException:当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常。 
 * 产生的原因:
 * 		迭代器是依赖于集合而存在的,在判断成功后,集合的中新添加了元素,而迭代器却不知道,所以就报错了,这个错叫并发修改异常。
 * 		其实这个问题描述的是:迭代器遍历元素的时候,通过集合是不能修改元素的。
 * 如何解决呢?
 * 		A:迭代器迭代元素,迭代器修改元素
 * 			元素是跟在刚才迭代的元素后面的。
 * 		B:集合遍历元素,集合修改元素(普通for)
 * 			元素在最后添加的。
 */
public class ListIteratorDemo2 {
	public static void main(String[] args) {
		// 创建List集合对象
		List list = new ArrayList();
		// 添加元素
		list.add("hello");
		list.add("world");
		list.add("java");

		// 迭代器遍历
		// Iterator it = list.iterator();
		// while (it.hasNext()) {
		// String s = (String) it.next();
		// if ("world".equals(s)) {
		// list.add("javaee");
		// }
		// }

		// 方式1:迭代器迭代元素,迭代器修改元素
		// 而Iterator迭代器却没有添加功能,所以我们使用其子接口ListIterator
		// ListIterator lit = list.listIterator();
		// while (lit.hasNext()) {
		// String s = (String) lit.next();
		// if ("world".equals(s)) {
		// lit.add("javaee");
		// }
		// }

		// 方式2:集合遍历元素,集合修改元素(普通for)
		for (int x = 0; x < list.size(); x++) {
			String s = (String) list.get(x);
			if ("world".equals(s)) {
				list.add("javaee");
			}
		}

		System.out.println("list:" + list);
	}
}

 

List three common sub-set of interfaces:

 

 

1, ArrayList class overview and use

  • ArrayList class overview
Underlying data structure is an array, the query fast, slow additions
Thread-safe, high efficiency
  • ArrayList Case
Storage string and ergodic
Store custom objects and traverse

 

1), and traverse the string storage

package cn.wen_01;

import java.util.ArrayList;
import java.util.Iterator;

/*
 * List的子类特点:
 * 		ArrayList:
 * 			底层数据结构是数组,查询快,增删慢
 * 			线程不安全,效率高
 * 		Vector:
 * 			底层数据结构是数组,查询快,增删慢
 * 			线程安全,效率低
 * 		LinkedList:
 * 			 底层数据结构是链表,查询慢,增删快
 * 			线程不安全,效率高
 * 
 * 案例:
 * 		使用List的任何子类存储字符串或者存储自定义对象并遍历。
 * 
 * ArrayList的使用。	
 * 		存储字符串并遍历
 */
public class ArrayListDemo {
	public static void main(String[] args) {
		// 创建集合对象
		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);
		}
	}
}

2), the storage and traversing custom objects

 Test categories:

package cn.wen_02;

import java.util.ArrayList;
import java.util.Iterator;

/*
 * ArrayList存储自定义对象并遍历
 */
public class ArrayListDemo2 {
	public static void main(String[] args) {
		// 创建集合对象
		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());
		}
	}
}

Student categories:

package cn.wen_02;

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;
	}

	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;
	}

}

2, Vector class overview and use

Vector class overview
Underlying data structure is an array, the query fast, slow additions
Thread-safe, low efficiency
Vector class-specific features
public void addElement (E obj )
public E elementAt ( int index )
public Enumeration elements ()
 
package cn.wen_03;

import java.util.Enumeration;
import java.util.Vector;

/*
 * 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()
 * 
 * JDK升级的原因:
 * 		A:安全
 * 		B:效率
 * 		C:简化书写
 */
public class VectorDemo {
	public static void main(String[] args) {
		// 创建集合对象
		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);
		}
	}
}

3, LinkedList class overview and use

LinkedList class overview
The underlying data structure is a linked list, query slow, fast additions and deletions
Thread-safe, high efficiency
LinkedList class -specific features
public void addFirst (E e) addLast (E e )
public E getFirst () getLast ()
public E removeFirst () public E removeLast ()
 
package cn.wen_03;

import java.util.LinkedList;

/*
 * 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()
 */
public class LinkedListDemo {
	public static void main(String[] args) {
		// 创建集合对象
		LinkedList link = new LinkedList();

		// 添加元素
		link.add("hello");
		link.add("world");
		link.add("java");

		// public void addFirst(Object e)
		// link.addFirst("javaee");
		// public void addLast(Object e)
		// link.addLast("android");

		// public Object getFirst()
		// System.out.println("getFirst:" + link.getFirst());
		// public Obejct getLast()
		// System.out.println("getLast:" + link.getLast());

		// public Object removeFirst()
		System.out.println("removeFirst:" + link.removeFirst());
		// public Object removeLast()
		System.out.println("removeLast:" + link.removeLast());

		// 输出对象名
		System.out.println("link:" + link);
	}
}

List collection exercise

  • ArrayList
Deduplication value set in the string ( the same contents of the string )
Removing duplicate values in the set of customized object ( member variables of the object are the same value )
  • LinkedList
Please LinkedList set of simulated stack data structure, and tested

 

1), removing duplicate values set in the string ( the same contents of the string )

package cn.wen_01;

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");
		array.add("world");
		array.add("world");
		array.add("java");
		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);
		}
	}
}

2) removing duplicate custom objects set values ( member variables of the object are the same value )

package cn.wen_02;

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");
		array.add("world");
		array.add("world");
		array.add("java");
		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);
		}
	}
}
 

3), use LinkedList set of simulated stack data structure, and tested 

package cn.wen_03;

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());
		}
	}
}
package cn.wen_03;

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;
	}

	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 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;
	}

}

 

 

 

Published 91 original articles · won praise 16 · views 1172

Guess you like

Origin blog.csdn.net/hewenqing1/article/details/103866614