1, Arrays and collections
1. Overview of data stored in sets and arrays:
Sets and arrays are structures that store multiple data, referred to as java containers for short.
Note: storage at this time mainly refers to storage at the memory level, and does not involve persistent storage (. txt,.jpg,.avi, in the database)
2. Characteristics of array storage:
>Once initialized, its len gt h is determined.
>Once an array is defined, the type of its elements is determined. For example: String[] arr;int[] arr1;
3. Disadvantages of array storage:
>Once initialized, its len gt h cannot be modified.
>The methods provided in the array are very limited, which is very inconvenient for operations such as adding, deleting and inserting data. At the same time, the efficiency is not high.
>The requirement to obtain the number of actual elements in the array. There are no ready-made properties or methods available in the array
>The characteristics of array data storage: orderly and repeatable. For disordered and unrepeatable needs, they cannot be met.
4. Advantages of collective storage:
Solve the disadvantages of array data storage.
2, Collection interface
1. Single instance set framework structure
|-----Collection interface; Singleton collection, which is used to store objects one by one
*List interface; Store ordered and repeatable data. Dynamic array
* |----ArrayList,LinkedList,Vector
*|--- Set interface: a "Set" that stores ordered and non repeatable data
* |----HashSet,LinkedHashSet,TreeSet
Corresponding diagram:
2. Common methods of collection interface:
@Test public void test1(){ Collection coll = new ArrayList(); //add(Object e):Will element e Add to collection coll in coll.add("AA"); coll.add("BB"); coll.add(123); coll.add(new Date()); //size(): Gets the number of elements added System.out.println(coll.size()); //addAll()(Collection coll1): take coll1 The elements in the collection are added to the current collection Collection coll1 = new ArrayList(); coll1.add(456); coll1.add("cc"); coll.addAll(coll1); System.out.println(coll.size()); System.out.println(coll); //clear():Empty collection elements coll.clear(); //isEmpty():Judge whether the current collection is empty System.out.println(coll.isEmpty()); }
@Test public void test1(){ Collection coll = new ArrayList(); coll.add(123); coll.add(456); coll.add(new String("Tom")); coll.add(false); // Person p = new Person("Jerry",13); // coll.add(p); coll.add(new Person("Jerry",13)); //contains(Object obj): Judge whether the current collection contains obj //When we judge, we call obj Class of the object equals(). boolean contains = coll.contains(123); System.out.println(contains); System.out.println(coll.contains(new String("Tom"))); // System.out.println(coll.contains(p)); System.out.println(coll.contains(new Person("Jerry", 13))); //2.containsAll(Collection coll1): Judge formal parameters coll1 Whether all elements in exist in the current collection. Collection coll1 = Arrays.asList(123,456); System.out.println(coll.containsAll(coll1)); } @Test public void test2(){ //3.remove(Object obj):Remove from current collection obj Element. Collection coll = new ArrayList(); coll.add(123); coll.add(456); coll.add(new String("Tom")); coll.add(new Person("Jerry",13)); coll.add(false); coll.remove(123); System.out.println(coll); coll.remove(new Person("Jerry",13)); System.out.println(coll); //4.removeAll(Collection coll1): Subtraction: remove from current set coll1 All elements in Collection coll1 = Arrays.asList(123,4567); coll.removeAll(coll1); System.out.println(coll); } @Test public void test3(){ Collection coll = new ArrayList(); coll.add(123); coll.add(456); coll.add(new String("Tom")); coll.add(new Person("Jerry",13)); coll.add(false); //5.retainAll(Collection coll1):Intersection: get the current set and coll1 The intersection of sets and returns it to the current set // Collection coll1 = Arrays.asList(123,456,789); // coll.retainAll(coll1); // System.out.println(coll); //equals(Object obj):To return true,The elements of the current set and the formal parameter set are required to be the same Collection coll1 = new ArrayList(); coll1.add(123); coll1.add(456); coll1.add(new String("Tom")); coll1.add(new Person("Jerry",13)); coll1.add(false); System.out.println(coll.equals(coll1)); } @Test public void test4(){ Collection coll = new ArrayList(); coll.add(123); coll.add(456); coll.add(new String("Tom")); coll.add(new Person("Jerry",13)); coll.add(false); //hashCode(): Returns the hash value of the current object System.out.println(coll.hashCode()); //8.aggregate ---->Array: toArray(): Object[] arr = coll.toArray(); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } //Expanding: arrays ---->aggregate:call Array Static method of class asList() List<String> list = Arrays.asList(new String[]{"aa", "bb", "cc"}); System.out.println(list); //9.iterator(): return Iterator Interface for traversing collection elements. Put on IteratorTest.java Medium test }
3. Conversion between collection and array
@Test public void test4(){ Collection coll = new ArrayList(); coll.add(123); coll.add(456); coll.add(new String("Tom")); coll.add(new Person("Jerry",13)); coll.add(false); //hashCode(): Returns the hash value of the current object System.out.println(coll.hashCode()); //8.aggregate ---->Array: toArray(): Object[] arr = coll.toArray(); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } //Expanding: arrays ---->aggregate:call Array Static method of class asList() List<String> list = Arrays.asList(new String[]{"aa", "bb", "cc"}); System.out.println(list); //9.iterator(): return Iterator Interface for traversing collection elements. Put on IteratorTest.java Medium test }
4. Use the Collection collection to store objects. The class of the object is required to meet the following requirements:
When adding data obj to the object of the implementation class of the Collection interface, the class of obj is required to override equals().
3, Iterator interface and foreach loop
1. There are two ways to traverse the Collection:
① Use Iterator ② foreach loop (or enhance for loop)
2.java. Iterator interface defined under utils package: iterator
2.1 Description:
The Iterator object is called an Iterator (a kind of design pattern) and is mainly used to traverse the elements in the Collection.
GOF defines iterator pattern as providing a way to access each element in a container object
Element without exposing the internal details of the object. The iterator pattern is born for containers.
2.2 function:
Traversing Collection elements
2.3 how to obtain an instance:
coll.iterator() returns an iterator instance
2.4 code implementation of traversal:
@Test public void test1(){ Collection coll = new ArrayList(); coll.add(123); coll.add(456); coll.add(new String("Tom")); coll.add(new Person("Jerry",13)); coll.add(false); Iterator iterator = coll.iterator();//Method 2: not recommended // for (int i = 0; i < coll.size(); i++) { // System.out.println(iterator.next()); // } //Method 3: recommendation //hasNext():Determine whether there is another element while (iterator.hasNext()){ //next():①Pointer down ②Returns the element at the collection position after moving down System.out.println(iterator.next()); } }
2.5 illustration
2.6 use of remove()
//test Iterator Medium remove() //If not already called next()Or in the last call next Method has been called remove method, //Re call remove Metropolis Daily IllegalStateException
//Internally defined remove(),You can delete the elements in the collection when traversing. This method is different from collection direct call remove()@Test public void test3(){ Collection coll = new ArrayList(); coll.add(123); coll.add(456); coll.add(new String("Tom")); coll.add(new Person("Jerry",13)); coll.add(false); Iterator iterator = coll.iterator(); //Delete from collection“ Tom" while (iterator.hasNext()){ Object obj = iterator.next(); if ("Tom".equals(obj)){ iterator.remove(); } } //Traversal set iterator = coll.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } }
3.jdk5.0 new feature -- enhanced for loop: (foreach loop)
3.1 example of traversing a set:
@Test public void test1(){ Collection coll = new ArrayList(); coll.add(123); coll.add(456); coll.add(new String("Tom")); coll.add(new Person("Jerry",13)); coll.add(false); //for(Type local variable of collection element : Collection object) // for (Object obj : coll){ System.out.println(obj); } }
3.2 Description:
The iterator is still called internally
3.3 example of traversing array:
@Test public void test2(){ int[] arr = new int[]{1,2,3,4,5,6}; //for(Type of array element local variable: array object) for (int i : arr){ System.out.println(i); } }