Chapter 1 Collection Sets
1. Sets and arrays
What is the difference between sets and arrays?
A. Length
a. The length of the array is fixed and cannot be changed during operation.
b. The length of the collection is variable and can be added or deleted.
B. Contents
Arrays can store any type (basic data type and reference data type)
//int[] arr1 = new int[3];
// Student[] stuArr = new Student[3]; object array
b. Collections can only store object type data
// ArrayList < String > LIST1 = new ArrayList <>(); // string object
// ArrayList < Integer > List2 = new ArrayList <>(); // Stores Integer objects, automatically Boxed
Recommended usage:
Later courses, recommended use of collections
// Arrays are intended only to derive collections and to refer to the characteristics of data types (address values and default values)
2. Common methods of sets
1. increase
public abstract boolean add(E e);
2. Delete
public abstract boolean remove(Object o);
public abstract void clear();
3. change
public abstract Object[] toArray();
public abstract <T> T[] toArray(T[] a);
4. check
public abstract boolean contains(Object o);
public abstract boolean isEmpty();
public abstract int size();
3. Concurrent modification exception
1. Concurrent modification exception
Name: Concurrent ModificationException
Reason: In the process of traversing collections with iterators, adding and deleting collections data can lead to concurrent modification exceptions.
Solution: Do not randomly add or delete data during iterator traversal
2. Life Example - > "Modifying Needs"
"Decorate the house." Please help me decorate the house. Halfway through the decoration, suddenly I said I would change to XXX style.
"Frequently modify requirements in the process of doing projects"
4. Three traversal modes of sets
public static void main(String[] args) {
Collection<String> c = new ArrayList<>();
c.add("Dili Reba");
c.add("Gulinaza");
c.add("Malzahar");
c.add("Leather sofa");
System.out.println("======= [1]Mode 1:Array of rotation ====");
String[] arr = new String[c.size()];
c.toArray(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("======= [2]Mode 2:iterator ====");
Iterator<String> iter = c.iterator();
while (iter.hasNext()) {
String next = iter.next();
System.out.println(next);
}
System.out.println("======= [3]Mode 3:Enhance for ===");
for (String s : c) {
System.out.println(s);
}
}
Chapter II Generics
1. The Concept of Generics
1. Why use generics?
I know there is a data type here, but I don't know exactly what type it is, so I use generics instead.
2. For example:
ArrayList knows that he can store objects. But it's not clear what objects need to be stored.
As the author of the ArrayList class. When Josh Bloch designs classes, they are defined as generics, and the user (Java programmer) specifies the type.
3. What are the benefits of generics?
A. Benefits:
a. Raise runtime exceptions to compile time. // For example, the add method of ArrayList can be validated when storing data using generics
b. Avoid type conversion exceptions. // Type conversion exception, ClassCastException that occurs during downward transition
B. Disadvantages:
When I first started writing code, it was a bit of a hassle. // For example, when creating an object in ArrayList, you need to write an angle bracket and a specific type.
2. Generic Classes
Definition of generic class
public class Car<NIUBEA> {
private NIUBEA nb;
public NIUBEA getNb() {
return nb;
}
public void setNb(NIUBEA nb) {
this.nb = nb;
}
}
Use of generic classes
public class Test {
public static void main(String[] args) {
Car<String> c1 = new Car<>();
c1.setNb("Maserati");
String nb1 = c1.getNb();
System.out.println("nb1 = " + nb1);
System.out.println("===========================");
Car<Double> c2 = new Car<>();
c2.setNb(288.88);
Double nb2 = c2.getNb();
System.out.println("nb2 = " + nb2);
}
}
3. Generic Method
Definition of generic methods
public class DogBa {
public static <HEHE> void method(HEHE hehe){
System.out.println(hehe);
System.out.println(hehe.getClass());
}
}
Use of generic methods
public class Test {
public static void main(String[] args) {
DogBa.method("Hey,Can you see me??");
System.out.println("===========================");
DogBa.method(666);
System.out.println("===========================");
DogBa.method(3.14F);
}
}
4. Generic Interface
Definition of generic interfaces
public interface IAnimal<SUIBIAN> {
public abstract void eat(SUIBIAN sb);
}
Use of generic interfaces
public class Ma<SUIBIAN> implements IAnimal<SUIBIAN> {
@Override
public void eat(SUIBIAN sb) {
System.out.println(sb);
System.out.println(sb.getClass());
}
}
public class Lu implements IAnimal<String>{
@Override
public void eat(String sb) {
System.out.println(sb);
System.out.println(sb.getClass());
}
}
public class Test {
public static void main(String[] args) {
Ma<String> make = new Ma<>();
make.eat("pineapple");
System.out.println("-----");
Ma<Integer> madongmei = new Ma<>();
madongmei.eat(666);
System.out.println("==========");
Lu luhan = new Lu();
luhan.eat("Meal");
}
}
5. Generic wildcards
1. What is the wildcard?
When you don't know exactly what generics are, use generic wildcards?
Mainly used in methods. // I don't know what the specific generics in List are. Can I use them instead?
For example: public static void printList (List <?> list) {...}
2. The upper and lower limits of generics, Integer extends Number extends Object // / whether upper or lower limits, can be passed in by themselves
A. Upper limit: either son or self
public static void printList(List<? extends Number> list){ .... }
// Here you can pass in Number and its subclasses (NumberInteger)
B. Lower Limit: Father or self
public static void printList(List<? super Number> list){ .... }
// What you can pass in is Number and Number's parent class (Number Object)