Java- frame set (a)

  • A: The origin of the set of
    • Array length is fixed, the need to redefine elements of the array when added exceeds the length of the array, too cumbersome, provides us with internal java collection classes, any object can be stored, the length can be changed, with an increase of the element increase, with the reduction element is reduced
  • B: the difference between the array and a set of
    • 1 difference:
      • Arrays can either be stored basic data types, and may store the reference data type, basic data types are stored value, the stored reference data type is an address value
      • Only store a set of reference data types (objects) may be stored in the collection of basic data types, but when stored objects will become autoboxing
    • The difference between 2:
      • Length of the array is fixed and can not automatically increase
      • The collection length is variable, and may be increased according to the increase of the elements
  • C: When to use arrays and collections
    • 1, if the number of elements in the array are fixed recommended
      • 2, if the number of elements is not recommended to use a fixed set of
        `Here Insert Picture Description
boolean add(E e)
boolean remove(Object o)
void clear()
boolean contains(Object o)
boolean isEmpty()
int size()

To turn into a set of arrays, traversal can be achieved toArray set ()

Collection coll = new ArrayList();
coll.add(new Student("张三",23));		//Object obj = new Student("张三",23);
coll.add(new Student("李四",24));
coll.add(new Student("王五",25));
coll.add(new Student("赵六",26));

Object[] arr = coll.toArray();				//将集合转换成数组
for (int i = 0; i < arr.length; i++) {
	Student s = (Student)arr[i];			//强转成Student
	System.out.println(s.getName() + "," + s.getAge());
}
boolean addAll(Collection c)
boolean removeAll(Collection c)
boolean containsAll(Collection c)
boolean retainAll(Collection c)

Traversal of traversal iterator collection

Use iterator


Collection c = new ArrayList();
	c.add("a");
	c.add("b");
	c.add("c");
	c.add("d");
Iterator it = c.iterator();						//获取迭代器的引用
while(it.hasNext()) {							//集合中的迭代方法(遍历)
	System.out.println(it.next());
}
  • Collection and storage traversed by custom object iterator
  • Collection c = new ArrayList();
c.add(new Student("张三",23));
c.add(new Student("李四",24));
c.add(new Student("王五",25));
c.add(new Student("赵六",26));
c.add(new Student("赵六",26));

for(Iterator it = c.iterator();it.hasNext();) {
	Student s = (Student)it.next();						//向下转型
	System.out.println(s.getName() + "," + s.getAge());	//获取对象中的姓名和年龄
}
System.out.println("------------------------------");
Iterator it = c.iterator();								//获取迭代器
while(it.hasNext()) {									//判断集合中是否有元素
	//System.out.println(((Student)(it.next())).getName() + "," + ((Student)(it.next())).getAge());
	Student s = (Student)it.next();						//向下转型
	System.out.println(s.getName() + "," + s.getAge());	//获取对象中的姓名和年龄
}
  • Iterator principle

Iterators is set to traverse, and an internal storage structure of each set is different, and therefore each of a set of memory taken are not the same, then we have () and next () method is defined in each class hasNext , this is possible, but it makes the entire collection system is too cumbersome, the iterator is a method to extract an interface upward, and then within each class definitions themselves in an iterative manner, there are two benefits of doing so, a first specifies the traversal entire collection system are hasNext () and next () method, a second, internal codes underlying implementation

A:List集合的特有功能概述

- void add(int index,E element)
- E remove(int index)
- E get(int index)
- E set(int index,E element)

Use traverse incorporated by size () and get () method.

List list = new ArrayList();
	list.add(new Student("张三", 18));
	list.add(new Student("李四", 18));
	list.add(new Student("王五", 18));
	list.add(new Student("赵六", 18));
	for(int i = 0; i < list.size(); i++) {
	Student s = (Student)list.get(i);
	System.out.println(s.getName() + "," + s.getAge());
}

Concurrent modification causes of abnormal production of and solutions

Demand: I have a collection, what I would like to determine if there is anything "world" this element, if there is, I would add a "javaee" elements, please write code.

List list = new ArrayList();
	list.add("a");
	list.add("b");
	list.add("world");
	list.add("d");
	list.add("e");
/*Iterator it = list.iterator();
while(it.hasNext()) {
	String str = (String)it.next();
	if(str.equals("world")) {
		list.add("javaee");			//这里会抛出ConcurrentModificationException并发修改异常
	}
}*/
  • B: ConcurrentModificationException appear

    • Iterates over the set modify the set
  • C: Solution

    • a: is iterating elements iterator modifying element (the ListIterator unique features add)

    • b: traverse the elements of the collection, set modification element

ListIterator lit = list.listIterator();		
//如果想在遍历的过程中添加元素,可以用ListIterator中的add方法
    	while(lit.hasNext()) {
    		String str = (String)lit.next();
    		if(str.equals("world")) {
    			lit.add("javaee");	
    			//list.add("javaee");
    		}
    	}

vector

  • A: Vector Class Overview

  • B: Vector class-specific features

    • public void addElement(E obj)
    • public E elementAt(int index)
    • public Enumeration elements()
  • C: Case presentation

    • Vector iteration

      Vector v = new Vector (); // create the collection object, List subclass
      v.addElement ( "A");
      v.addElement ( "B");
      v.addElement ( "C");
      v.addElement ( " d ");

//Vector迭代
Enumeration en = v.elements();			//获取枚举
while(en.hasMoreElements()) {			//判断集合中是否有元素
	System.out.println(en.nextElement());//获取集合中的元素
}
  • A: Array
    • Modify the query fast too fast
    • Additions and deletions slow
  • B: list
    • Query slow, too slow modification
    • Additions and deletions fast

It features three subclasses of List

  • ArrayList:
    the underlying data structure is an array, query fast, slow additions and deletions.
    Thread-safe, high efficiency.
    Vector:
    the underlying data structure is an array, query fast, slow additions and deletions.
    Thread-safe, low efficiency.
    Vector relatively slow query ArrayList (thread-safe)
    Vector LinkedList relatively slow additions and deletions (array structure)
    LinkedList:
    the underlying data structure is the list, slow queries, additions and deletions fast.
    Thread-safe, high efficiency.

  • The difference between Vector and ArrayList

Vector is thread-safe, low efficiency
ArrayList is not thread-safe, high efficiency

Common: both arrays to achieve

The difference between ArrayList and LinkedList

ArrayList底层是数组结果,查询和修改快
	LinkedList底层是链表结构的,增和删比较快,查询和修改比较慢
共同点:都是线程不安全的

Guess you like

Origin blog.csdn.net/lgy54321/article/details/93302106