Location>code7788 >text

ITERATOR and array built -in methods and common errors in the Java collection

Popularity:457 ℃/2025-02-01 23:24:17

Delete a certain error in MAP

package part;
 Import;
 Import;
 public class java01 {
     Public Static Void Main (String [] args) {{
         // Why use the packaging type integer here instead of int
         HashMap <string, Integer> MAP = New HashMap ();
         ("a", 1);
         ("B", 2);
         ("C", 3);
         // Get the collection of all key names, and the return type is SET type
         Set <strong> keys = ();
         // Enhanced form for cycle
         for (string keyname: keys) {
             // If the key name is equal, delete it, causing the program to report an error.  I will report an error
             ifyname == "b") {{
               // (keyName);
                 ("n", 11); // Newly added an error
                 (Keyname, 200); // OK, but we better use iterators to operate
             }
         }
     }
 }

Why use the packaging type integer here instead of int

Because: the key and values ​​of the HashMap must be an object type, which cannot be the basic data type.
Java provides the functions of automatic loading (INT to Integer) and INTEGER to int
Inty

Will I report an error for Haha?

When we are in the middle of the cycle, not only are deleted, but new additions will also report an error.
Because: When you use For-Each to cycle the key set of HashMap
The bottom layer of the for-ele is implemented through Iterator
Iterator will check whether the set is modified (through a modcount variable) to determine
If the collection is modified (exemplary and delete elements), the ConcurrentModificationException will be thrown out.

Why will you not report an error when you traverse the last item?

package part;
 Import;
 Import;

 public class java01 {
     Public Static Void Main (String [] args) {{
         // The key and values ​​of HashMap must be an object type, and it cannot be the basic data type.  Inty
         HashMap <string, Integer> MAP = New HashMap ();
         ("a", 1);
         ("B", 2);
         ("C", 3);
         // Get the collection of all key names, and the return type is SET type
         Set <strong> keys = ();
         // Enhanced form for cycle
         for (string keyname: keys) {
             // When the last item is traversed, it will not report an error when deleting
             If (keyName == "c") {{
                 (Keyname);
             }
         }
     }
 }

Explanation: Why not report the last one to delete the error?

The trigger mechanism of this heel (CONCURRENTMODIFINEEPTION)
(ConcurrentModificationException) is to detect whether the set is modified by the Modcount variable.
When traversing the collection, Iterator will check whether the Modcount is consistent with the expected value. If it is not consistent (that is, the set is modified), it will throw an exception.
When you delete the last item, the Iterator may have completed the traversal, so it will not trigger the Modcount check.
So I won't report an error.

Modify the data, it is best to use the iterator to process it

package part;


 Import;
 Import;
 Import;

 public class java01 {
     Public Static Void Main (String [] args) {{
         // Why use the packaging type integer here instead of int
         HashMap <string, Integer> MAP = New HashMap ();
         ("a", 1);
         ("B", 2);
         ("C", 3);
         Set <strong> keys = ();
         // iterators
         Iterator <string> it = ();
         // Hasnext method is used to whether there is a way to exist the next data
         While (())
             // Get the next data
             String key = ();
             // Delete the key name is B
             if ("b" .equals (key)) {
                 ();
             }
             // 1 null 3
             ((key);
         }
         // {a = 1, c = 3}
         (MAP);
     }
 }

Use the Removeif () method (Java 8+) iteration

HashMap <string, Integer> Map = New HashMap <> ();
 ("a", 1);
 ("B", 2);
 ("C", 3);

 Set <strong> keys = ();
 (Keyname-> ("C"); // Use Removeif
 (MAP); // Output: {a = 1, b = 2}

Can I delete other items using an iterator?

That is to say: Can we delete the data of B when we are in cycle A?
No.
Because: can only delete the current cycle. Besides: can only delete the current cycle of this item

Convert the array into string ()

package part;

 Import;
 public class java01 {
     Public Static Void Main (String [] args) {{
         // State and initialize an INT array
         int [] is = {1, 2, 3, 4, 5};
         // Convert to string [1, 2, 3, 4, 5]
         String str = (is);
         (STR);
         // [i@28d93b30 is the memory address of HashCode
         (is);
     }
 }

Convert the array into a collection and sequence

package part;

 Import;
 Import;

 public class java01 {
     Public Static Void Main (String [] args) {{
         // Convert the array into a collection
         List <integer> list = (1, 2, 3, 4, 5);
         int [] arr = {4,1, -3,10};
         // The default is a promoter haha, which will affect the original array of the array, which is different from JS
         (ARR);
         // [-3, 1, 4, 10]
         ((ARR));
     }
 }

Two -point finding method, finding the position after sorting

package part;

 Import;
 Import;

 public class java01 {
     Public Static Void Main (String [] args) {{
         int [] arr = {4,1, -3,10};
         (ARR);
         // [-3, 1, 4, 10]
         ((ARR));
         // Query 4
         int index = (ARR, 4);
         // 2
         (index);
     }
 }

2 number group items comparison

package part;
 Import;
 Import;

 public class java01 {
     Public Static Void Main (String [] args) {{
         int [] arr1 = {4,1, -3,10};
         int [] arr2 = {4,1,10, -3};
         // Will compare whether the two arrays are equal, and it will be compared in one -to -one.  The 10 of the 2 and the 10 of the 2nd item 2 are not equal, and return to FLASE
         ((ARR1, ARR2)); // False
     }
 }
package part;

 Import;
 Import;

 public class java01 {
     Public Static Void Main (String [] args) {{
         int [] arr1 = {4, 1, -3, 10};
         int [] arr2 = {4, 1, 10, -3};
         // ARR1, 0, 2 means that from the ARR1 array, start from 0 and take the top 2.  ARR2, 0, 2 means that from the ARR2 array, start from 0, take the top 2
         // Pay special attention to: introduced in JDK 9.  If you use JDK 8 or earlier, this method will report an error.
         ((ARR1, 0, 2, ARR2, 0, 2)); // Output: true
     }
 }

ArrayList sets the default duration 10, and you must set the container size greater than or equal to 0. If it is negative, report an error

package part;

 Import;
 public class java01 {
     Public Static Void Main (String [] args) {{
         Arraylist <string> list2 = New ArrayList <string> (0);
         (list2); // []

        Arraylist <string> list1 = New ArrayList <string> (-1);
        (list1); // illegal parameter abnormalities
     }
 }

ArrayList's access range is [0, length -1]

package part;
 Import;
 Import;
 Import;

 public class java01 {
     Public Static Void Main (String [] args) {{
         Arraylist list = new arrayList ();
         ("A").
         ("B").
         ("C").
         // The range of access is [0, length -1]
         (2);
         // Indexoutofboundsexception index exceeds exceptions
         (3);
     }
 }

The length of linkedList is 0, using get (0) and getfirst access to report errors

package part;

 Import;

 public class java01 {
     Public Static Void Main (String [] args) {{
        LinkedList <string> list = new linkedlist <string> ();
        // Error report
        ((0));
        // List's length is 0. Now you get the first item. This operation is similar to the index of crossing the world.  The same will also report an error
         //
        (());
     }
 }

HashMap is deleted during cycle, and new data will report an error

package part;
 Import;
 Import;

 public class java01 {
     Public Static Void Main (String [] args) {{
         HashMap Map = New HashMap ();
         ("a", 1);
         ("B", 2);
         ("C", 3);
         // Hashmap Once the cycle starts, if you delete it, new data will be added, and an error will occur
         for (object key ()) {
             if ((b ")) {
                // (key); delete the data newspaper error
                // ("d", 4); new data newspaper error
                 (key, 4); // Modify the data will not report an error.
             }
         }
         // We can use iterators to solve such problems.
         (MAP);
     }
 }

end

Ready to start learning Java.
On the fifth day of studying today, I will publish articles every day, and I will get rolled up.
Ask your friends to supervise me, Oli gives