Catalog introduction
- 1. Array of objects
- 2. Collection
- 2.1 The origin of sets
- 2.2 The difference between an array and a collection
- 2.3 Collection inheritance system diagram
- 2.4 Overview of Collection Functions
- 2.5 Traversal of collections
- 2.6 The principle of iterators
- 3. Data structure
- 3.0 How to choose a suitable List collection
- 3.1 Arrays and linked lists of data structures
- 3.2 Stacks and Queues of Data Structures
- 4. Collection Summary
- 4.1 Collection Summary
1. Array of objects
- 1.1 Drawing object array memory map
- Requirement: Store the information [name, age] of the three students into an object array
- Plot the infield diagram as follows:
2. Collection
-
2.1 The origin of sets
- Object-oriented languages reflect things in the form of objects, so in order to facilitate the operation of multiple objects, Java provides a collection class.
-
2.2 The difference between an array and a collection
- (1): Length difference: the length of the array is fixed and the length of the collection is variable
- (2): The difference between storage data types: Arrays can store basic data types and reference data types; collections can only store reference data types
- (3): Content difference: Arrays can only store elements of the same data type, and sets can store elements of different types
-
2.3 Collection inheritance system diagram
-
2.4 Overview of Collection Functions
-
a: add function
- boolean add(Object obj): add an element
- boolean addAll(Collection c): add an element of a collection
-
b: delete function
- void clear(): remove all elements
- boolean remove(Object o): removes an element
- boolean removeAll(Collection c): removes an element of a collection (one or all)
-
c: Judgment function
- boolean contains(Object o): Determines whether the set contains the specified element
- boolean containsAll(Collection c): Determine whether the collection contains the specified collection element (one or all)
- boolean isEmpty(): Determine if the collection is empty
-
d: get function
- Iterator<E> iterator() (emphasis)
-
e: length function
- int size(): the number of elements
- Interview question: Does an array have a length() method? Does a string have a length() method? Does a collection have a length() method?
-
f: intersection function
- boolean retainAll(Collection c): elements that both collections have? Think about where the elements go, and what does the returned boolean mean?
-
g: Convert the collection to an array
- Object[] toArray()
-
2.5 Traversal of collections
-
2.5.1 Iterator traversal [less used]
public class CollectionDemo {
public static void main(String[] args) {
// Iterator iterator(): 使用迭代器进行遍历(重点*********)
// 创建Collection集合对象
Collection col = new ArrayList() ;
// 添加元素
col.add("张三") ;
col.add("李四") ;
col.add("王五") ;
col.add("赵六") ;
// 使用迭代器对其进行遍历
// 1. 获取迭代器对象
Iterator it = col.iterator() ; // 返回时实现了Iterator这个接口的子类对象
// 2. 调用迭代器对象中的方法完成遍历
// E next(): 获取元素的同时,将指针向后移动一位
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
- 2.5.2 Collection to array traversal
public class CollectionDemo {
public static void main(String[] args) {
// 1. 创建集合对象
Collection col = new ArrayList() ;
// 2. 添加元素
col.add("鸣人") ;
col.add("佐助") ;
col.add("佩恩") ;
// 3. 把集合转换成数组
// Object[] toArray()
Object[] objs = col.toArray() ;
// 4. 遍历数组
for(int x = 0 ; x < objs.length ; x++) {
// 向下转型
String s = (String)objs[x] ;
System.out.println(s.length());
}
}
}
- 2.6 The principle of iterators
3. Data structure
-
3.0 How to choose a suitable List collection
-
3.1 Arrays and linked lists of data structures
-
3.2 Stacks and Queues of Data Structures
4. Collection Summary
- 4.1 Collection Summary