This is the 8th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021.
Traversal is arguably the most common operation on a data structure. I said that the basic operation is “add, delete, change, check”, but traversal is to find out who we want to add, delete, change, check. In general, I’m going to do it with some if judgment.
So in preparing this article, I had a long pause in deleting. I can’t understand why Iterator + remove is used when iterating and deleting. Later I wrote an example and put it at the end of the article. It doesn’t matter if you don’t use it, it’s just weird to write it. Criticisms and corrections are welcome.
Format:
for/while
Loops, the most common kind, are taken one by one, requiring an order of the container;for
Enhanced version: Enhanced for loop;Iterator for, while
Create a new Iterator, and then write it in a for or while loop, using hasNext to determine whether to do the next iteration, create a temporary object, and print.
Traverse the List
Initialize an ArrayList:
List<String> ids = new ArrayList<>();
ids.add("007");
ids.add("008");
ids.add("009");
Copy the code
- To iterate over the first type, since the list is ordered, you can use an index to iterate over it:
// List traverses 1
System.out.println("Ids traversal for1");
for (int i=0; i<ids.size(); i++) {
System.out.println(ids.get(i));
}
Copy the code
- The second method, using the enhanced for loop, is written differently and has nothing to do with the index:
// List traverses 2
System.out.println("Ids traversal for2");
for (String temp: ids) {
System.out.println(temp);
}
Copy the code
- Third, define an iterator and write it inside a for or while:
// List Iterator
System.out.println("Ids traversal Iterator1");
for (Iterator iterator = ids.listIterator(); iterator.hasNext();) {
String temp = (String) iterator.next();
System.out.println(temp);
}
// List iterates through the while version of Iterator
System.out.println("Ids traversal Iterator2");
Iterator<String> iterator = ids.iterator();
while (iterator.hasNext()) {
Object temp = iterator.next();
System.out.println(temp);
}
Copy the code
The results are as follows:
Traverse the Set
The Set is out of order, so there’s no index to use. Here’s a quick review of the differences between HashSet and TreeSet, which correspond to two traversal methods:
- The first uses enhanced for loops, which use HashSet and are a bit more unordered:
// Initialize a HashSet
Set<Integer> ages = new HashSet<>();
ages.add(8);
ages.add(88);
ages.add(36);
// Set traverses 1
for (Integer i: ages) {
System.out.println(i);
}
Copy the code
Running results:
- The second type, again using for but using Iterator:
Set<Integer> ages2 = new TreeSet<>();
ages2.add(8);
ages2.add(88);
ages2.add(36);
// Set iterates over Iterator
for (Iterator iterator2 = ages2.iterator(); iterator2.hasNext();) {
Integer temp = (Integer) iterator2.next();
System.out.println(temp);
}
Copy the code
The result is 8, 36, 88. It’s already in order of size.
Traverse Map
The essence of traversing a Map is traversing a Set.
Initialize a class table and use HashMap to store it:
// Initialize a map
Map<String, String> courses = new HashMap<>();
courses.put("1001"."English");
courses.put("1003"."Novel Reading");
courses.put("1002"."History");
System.out.println("Initialized Map:" + courses.toString());
Copy the code
- The first way to do it is to take theta
KeySet
And then use this Key to get the corresponding values one by one:
KeySet <String> keySet = courses.keyset (); for (String courseId: keySet) { System.out.println(courseId + ": " + courses.get(courseId)); }Copy the code
Instead of defining the keySet outside, we could say “for” instead:
for (String courseID: courses.keySet()) {
}
Copy the code
- Second, go get this
EntrySet
And then use each Entry directlygetKey
和getValue
:
EntrySet = entrySet = entrySet
System.out.println("Use EntrySet to traverse:");
Set<Map.Entry<String,String>> kvs = courses.entrySet();
for (Iterator iterator2 = kvs.iterator(); iterator2.hasNext();) {
Map.Entry e = (Map.Entry) iterator2.next();
System.out.println(e.getKey() + ":" + e.getValue());
}
Copy the code
The Map traversal code runs as follows:
Will Iterator be used to delete iterators?
Delete after traversal:
System.out.println("Delete when you're done, that's a little weird.");
int n = ids.size();
for (int i=0; i<n; i++) {
System.out.println(ids.get(0));
System.out.println(ids.toString());
ids.remove(0);
}
Copy the code
Let’s just use the initialized example above. I just saved the size at the beginning. Otherwise the size will change as the list removes an element. Each time, print the element to be deleted, and then type the current list state. Running results:
Recommended version written:
Iterator: Iterator: Iterator: Iterator: Iterator: Iterator: Iterator
//
System.out.println(ids.toString());
ids.add("wait remove 1");
ids.add("wait remove 2");
ids.add("wait remove 3");
System.out.println(ids.toString());
System.out.println("Recommended way to write:");
Iterator iterator1 = ids.iterator();
while(iterator1.hasNext()) {
String obj = (String) iterator1.next();
System.out.println(obj);
if (obj.contains("1")) {
iterator1.remove();
}
}
System.out.println("end"+ids.toString());
Copy the code
Running results:
It’s better to write it down to deepen your understanding. Move on to the next point! One unit –