A generic
Benefits of generics:
1 increased code security
2 eliminates the hassle of forced conversion (down conversion)
Let's go through the examples to see what generics look like
ArrayList<String> list = new ArrayList<String>();
Generics are the types of elements saved in the declaration collection. You can see that the elements saved above are strings, and the following strings can also be omitted
ArrayList<String> list = new ArrayList<>();
Default and declare generics are the same when not written later
generic interface
? extends E
Among them? Is subclass E is parent
Downward restriction only allows subclasses or grandchildren, etc
int... num is equivalent to an array
Let's see int Example of num
public static void print1(int a ,int ... num) {
for (int i = 0; i < num.length; i++) {
System.out.println(num[i]);
}
}
public static void fun2() {
int[] array = new int[] {1,2,3,4,5,6};
print1(3,array);
print1(1,3,5,7,8,99,1000);
}
String[] strings = {"wanglong","wangning"};
List<String> asList = Arrays.asList(strings);
ListIterator<String> listIterator = asList.listIterator();
while (listIterator.hasNext()) {
System.out.println(listIterator.next());
What's the point of not adding or removing the above code?
The point is that you can use other methods in the set
Three ways to delete
Three methods of traversal
1 General traversal
ArrayList<String> strings = new ArrayList<>();
strings.add("a");
strings.add("b");
strings.add("b");
strings.add("c");
strings.add("d");
// No iteration traversal
// Delete b if it exists in the set
for (int i = 0; i < strings.size(); i++) {
if (strings.get(i).equals("b")) {
// i -- Mark, call back the corner mark once after each deletion
strings.remove(i--);
}
System.out.println(strings.get(i));
}
}
Iterator traversal
ArrayList<String> strings = new ArrayList<>();
strings.add("a");
strings.add("b");
strings.add("c");
strings.add("d");
Iterator<String> iterator = strings.iterator();
while (iterator.hasNext()) {
String next = iterator.next();
if (next.equals("b")) {
// Let iterator delete
// If you want to add an element, you need to use the unique iterator in the List ListIterator
iterator.remove();
}
}
System.out.println(strings);
3 enhanced for loop traversal
ArrayList<String> strings = new ArrayList<>();
strings.add("a");
strings.add("b");
strings.add("c");
strings.add("d");
// ergodic
// After the colon is the container you want to traverse
// Before bubbling, you need to traverse every element in the same period
// The underlying layer of the enhanced for loop is implemented with iterators
// Generally speaking, Zhiyong doesn't want to delete printing
for (String string : strings) {
System.out.println(string);
}
Nesting of two sets
Requirements: a java discipline has two classes, two students in each class
Represented by sets
There are two small collections in the subject
Every little assembly is filled with students
ArrayList<ArrayList<Students>> subjects = new ArrayList<>();
// Create a small collection
ArrayList<Students> classes1 = new ArrayList<>();
// Put the students in a small assembly
classes1.add(new Students("Be doomed", 80));
classes1.add(new Students("Ning Ning", 79));
// Create class 2
ArrayList<Students> classes2 = new ArrayList<>();
classes1.add(new Students("Stars", 73));
classes1.add(new Students("Culvert", 84));
// Put the class into the subject
subjects.add(classes1);
subjects.add(classes2);
// Traversal printing
// Find out the class in a circle
for (ArrayList<Students> classes : subjects) {
// Second level circulation to find out the students in the class
for (Students students : classes) {
System.out.print(students);
}
System.out.println();
}