Generally speaking, the data structures in textbooks include arrays, single linked lists, stacks, trees, and graphs. The data structure I am referring to here is a question of how to represent an object. Sometimes, a single variable declaration is useless, such as int, String, double, or even one-dimensional arrays and two-dimensional arrays cannot fully express what you want to express, and it is too troublesome to define a class Class. At this time, you can consider using the Collections class in Java. Using the Collections class, you must declare import java.util.* in the file header;
1. Dynamic, ordered, variable sized one-dimensional array Vector and ArrayList
The Collections class includes dynamic, ordered, variable sized one-dimensional array Vector and ArrayList.
The only difference between Vector and ArrayList is: vector comes with threads mutually exclusive, and multiple threads will throw exceptions to read and write, while arraylist allows multiple threads to read and write, and the other parts are exactly the same. In other words, if a single thread is reading and writing, there is no difference between using Vector and ArrayList, but now programming is basically ArrayList, and using Vector is a bit non-mainstream.
1. The use of Vector is as follows:
public static void Vectortest() { // Vector<Double> means that this vector can only store double // Vector<String> means that this vector can only store String // Although Vector<Object> vector=new Vector<Object>(); is equivalent to Vector vector=new // Vector(); however, writing this in eclipse will warn you, indicating that your Vector is not standardized, () Vector<Object> vector = new Vector<Object>(); vector.add(1.6); vector.add(2.06); vector.add(1); System.out.println("Simple add means adding elements from the end: " + vector); System.out.println("size() can find the number of elements contained in vector: " + vector.size()); vector.remove(1); System.out.println("remove(1) means deleting the first element, because the count starts from 0, that is, the element 2.06: " + vector); vector.remove(vector.lastElement()); System.out.println("The vector that deletes the last element is: " + vector); vector.add(0, 1.8888); System.out.println("Add 1.8888 this element at the 0th position: " + vector); vector.set(0, "a"); System.out.println("Change the 0th position: " + vector); } If this method is called in the main function:
System.out.println("======== Test start of Vector data structure==========="); Vectortest(); System.out.println("======== Test end of Vector data structure=============); The operation results are as follows:
======= Test start of Vector data structure======
A simple add means adding elements from the end: [1.6, 2.06, 1]
size() can find the number of elements contained in vector: 3
remove(1) means to delete the first element. Since the count starts from 0, that is, the element 2.06: [1.6, 1]
The vector that deletes the last element is: [1.6]
Add the element 1.8888 at the 0th position: [1.8888, 1.6]
Change the element at the 0th position to a: [a, 1.6]
======== Test end of Vector data structure======
2. ArrayList
public static void ArrayListtest() { ArrayList<Double> arraylist = new ArrayList<Double>(); arraylist.add(1.0); arraylist.add(4.0); arraylist.add(5.0); arraylist.add(2.3); System.out.println("Simple add means adding elements from the end: " + arraylist); System.out.println("size() can find the number of elements contained: " + arraylist.size()); arraylist.remove(1); System.out.println("remove(1) means to delete the first element. Since the count starts from 0, that is, the element 4: " + arraylist); arraylist.remove(arraylist.size() - 1); System.out.println("The arraylist for deleting the last element is: " + arraylist); arraylist.add(0, 1.8888); System.out.println("Add 1.8888 element at the 0th position: " + arraylist); arraylist.set(0, 9.0); System.out.println("Change the element at the 0th position to a: " + arraylist); Collections.sort(arraylist); System.out.println("Sorting is supported if arraylist is not an abstract type" + arraylist); } Here you can see that the way ArrayList deletes the last element is different from Vector. The main reason is that ArrayList does not have the lastElement() method to remove the last element. Remove() can only be used to determine the position of the last element. If this method is called in the main function like this:
System.out.println("========= Test start of ArrayList data structure ========="); ArrayListtest(); System.out.println("======== Test end of ArrayList data structure =====================;); Then the following running results are obtained:
======= Test start of ArrayList data structure ======
A simple add means adding elements from the end: [1.0, 4.0, 5.0, 2.3]
size() can find the number of elements contained: 4
remove(1) means to delete the first element. Since the count starts from 0, that is, the element 4: [1.0, 5.0, 2.3]
The arraylist for deleting the last element is: [1.0, 5.0]
Add the element 1.8888 at the 0th position: [1.8888, 1.0, 5.0]
Change the element at the 0th position to a: [9.0, 1.0, 5.0]
If arraylist is not an abstract type, sorting is supported [1.0, 5.0, 9.0]
======== Test end of ArrayList data structure======
From the above two examples, we can see that Vector and ArrayList are more than an ordinary array, that is, the one-dimensional array taught in the textbook int array[] = { 8, 7, 100, 88, 6, 4, 5, 33, 7 }; it is much more powerful. You can insert elements at any position, or you can delete elements at a specified position without traversing the array. Of course, you still need to know how this array is traversed for you. In fact, ArrayList and ordinary one-dimensional arrays can be completely transformed, and ArrayList can also be used to sort the array directly without writing a bubble sort on the array. You can sort the array directly with Collections.sort(); and then use Collections.reverse(); to achieve inverse sorting. Of course, it is still the same sentence. For you, you still need to know how this array is sorted.
For example, the following method implements sorting and inverse sorting of one-dimensional array int array[] = { 8, 7, 100, 88, 6, 4, 5, 33, 7 }; first convert the array into ArrayList and then sort it with Collections.sort(); and Collections.reverse();, and finally convert the ArrayList content back to a one-dimensional array:
public static void arrayListSort() { int array[] = { 8, 7, 100, 88, 6, 4, 5, 33, 7 }; ArrayList<Integer> arraylist = new ArrayList<Integer>(); for (int i = 0; i < array.length; i++) System.out.print(array[i] + ","); for (int i = 0; i < array.length; i++) arraylist.add(array[i]); Collections.sort(arraylist); for (int i = 0; i < array.length; i++) arraylist.add(array[i]); Collections.sort(arraylist); for (int i = 0; i < array.length; i < array.length; i++) arraylist.add(array[i]); Collections.sort(arraylist); for (int i = 0; i < array.length; i < array.length; i++) arraylist.add(array[i]); Collections.sort(arraylist); for (int i = 0; i < array.length; i++) array[i] = arraylist.get(i); System.out.print("Sorted array: "); for (int i = 0; i < array.length; i++) System.out.print(array[i] + ","); Collections.reverse(arraylist); for (int i = 0; i < array.length; i++) array[i] = arraylist.get(i); System.out.print("Inversely sorted array: "); for (int i = 0; i < array.length; i++) System.out.print(array[i] + ","); //Destroy the arraylist after sorting. Arraylist = null; //This sentence suggests that Java recycle garbage immediately. Of course, whether this sentence is OK or not, Java will automatically remove garbage during the run process; } In the main function, the method is called like this:
System.out.println("==========Java array sort starts =========="); arrayListSort(); System.out.println("============= Java array sort ends ==========); You can get the following running results:
========Java array sorting starts======
8,7,100,88,6,4,5,33,7, sorted array: 4,5,6,7,7,8,33,88,100, inversely sorted array: 100,88,33,8,7,7,6,5,4,
========= Java array sorting ends======
In addition, the same is true for the previous "A Brief Introduction to the Use of List in Java" (click to open the link)
2. Collection HashSet
In addition, there is a collection HashSet, which is exactly the same as the mathematical collection concept. A set composed of one or more elements is called a set. HashSet has:
1. Determinism . The elements in the set must be deterministic. This is nonsense. It must be deterministic. Can I still put an uncertain thing in it?
2. Mutual opposite sex , the elements in the set are different. For example: set A={1, a}, then a cannot be equal to 1, that is, if you put two 1s into a HashSet, it will automatically become a 1
3. Disorder , there is no order of elements in the set. Therefore, HashSet must not perform sorting operations, for example, the following method:
public static void HashSettest() { HashSet<Object> hashset = new HashSet<Object>(); hashset.add(1); hashset.add(1); hashset.add(5); hashset.add(2.3); System.out.println("Simple add means adding elements from the end: " + hashset); System.out.println("size() can find the number of elements contained: " + hashset.size()); hashset.remove(1); System.out.println("remove(1) means deleting the element '1': " + hashset); hashset.remove("asd"); System.out.println("If there is no 'asd' element, remove does nothing: " + hashset); hashset.add(1.8888); System.out.println("Add 1.8888 element: " + hashset); }In the main function, call this method:
System.out.println("========= Test start of HashSet data structure ========="); HashSettest(); System.out.println("========= Test end of HashSet data structure ================); The results are as follows:
======= Test starts with HashSet data structure======
A simple add means adding elements from the end: [1, 5, 2.3]
size() can find the number of elements contained: 3
remove(1) means to delete the element '1': [5, 2.3]
Without the 'asd' element, remove does nothing: [5, 2.3]
Add the element 1.8888: [5, 1.8888, 2.3]
======== Test end of HashSet data structure======
HashSet has the add() method and the remove() method. The elements added to add() are in order. The results printed with System.out.println() may be in different orders. They cannot be the same as the Vector and ArrayList above. As long as the stored elements are not Object, Collections.sort(arraylist); can be used to sort them.
3. Dual HashMap
The usage method here is basically the same as the data above, and it is also very simple. It is to put the object into the map, and get can take away the objects in the map, but it is wrong to try to use the binary HashMap into a triple. If an object contains too many elements, you should consider using classes. Instead of being obsessed with Java's Collections class that is interfering between ordinary variables and Class classes.
For example, the following method shows the error operation of trying to change HashMap to triple:
public static void Maptest(){ System.out.println("============ Start of use of Map error==============); HashMap<String,String> map=new HashMap<String, String>(); HashMap<String,HashMap<String, String>> bigmap=new HashMap<String, HashMap<String, String>>(); map.put("key1","1"); map.put("key2","2"); bigmap.put("test1",map); map.clear(); map.put("key1","3"); map.put("key2","4"); bigmap.put("test2",map); System.out.println(bigmap); System.out.println(bigmap.get("test1").get("key1")); System.out.println(bigmap.get("test1").get("key2")); System.out.println(bigmap.get("test2").get("key1")); System.out.println(bigmap.get("test2").get("key2")); System.out.println("========== End of use of Map incorrectly================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================== Calling this code in the main function gives the following run result:
======== Test starts with Map data structure======
======== Start of use of Map error======
{test1={key2=4, key1=3}, test2={key2=4, key1=3}}
3
4
3
4
======= End of use of Map error======
========= Start of correct use of Map======
{key3=3, key2=2, key1=1}
======== End of correct use of Map======
======== Test end of Map data structure======
This program was originally intended to be very obvious, trying to construct a {test1,key1,1},{test1,key2,2},{test2,key3,3},{test2,key4,4}
However, every bigmap still has that map. Once you clear the map, the map in test1 will also be cleared. Someone tried to create multiple map1, map2,... Then you might as well use a simple class to look more clear, and you can write methods in the class in the future and be inherited
4. Notes
When creating a new Vector, Arraylist, HashSet, or HashMap, you can write it as:
Collection<String> a= new ArrayList<String>();
List<String> a= new Vector<String>();
Because there are ArrayList, Vector, Linkedlist, HashSet, and TreeSet that inherit from the Collection interface, and HashMap and Hashtable that inherit from the MAP interface, this is a problem of inheritance, polymorphism, encapsulation and other classes in Java. Of course, for your companions to see more clearly, don't play fancy naming here, write a clear ArrayList<Integer> arraylist = new ArrayList<Integer>(); No one dares to say that you don't understand classes in Java.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.