Generally speaking, the data structure of the textbook includes array, single linked list, stack, tree, graph. The data structure I am referring to here is a question of how to represent an object, and sometimes a single variable declaration can be overwhelming, 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 much trouble to define class classes. You might consider using the collections class in Java. Using the collections class, you must declare the import java.util.* in the file header;
One, dynamic, orderly, variable size vector and ArrayList of one-dimensional arrays
The Collections Class includes a dynamic, ordered, variable-dimensional one-dimensional array of vectors and ArrayList.
The only difference between vectors and ArrayList is that the vector is thread-exclusive, and multiple threads throw an exception to their reads and writes, while ArrayList allows multiple threads to read and write, and the other parts are identical, in other words, if the single thread is reading and writing, There is no difference between using vectors and ArrayList, but now programming is mostly ArrayList, using vectors is a bit non-mainstream.
1. Vector is used as follows:
public static void Vectortest () {
//vector<double> indicates that the Vector can only hold Double
//vector<string> Indicates that the vector can only exist string
//Although vector<object> vector=new vector<object> (), equivalent to vector vector=new
// Vector (); however, in Eclipse, this is written to warn you that this vector is not canonical, ╮(╯▽╰)╭
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 the vector: "+ vector.size ());
Vector.remove (1);
System.out.println ("Remove" (1) means to delete the 1th element, since the count begins at 0, that is, the 2.06 element: "+ vector");
Vector.remove (Vector.lastelement ());
System.out.println ("Delete the vector of the last element:" + vector);
Vector.add (0, 1.8888);
System.out.println ("Add 1.8888 of this element in the No. 0 position:" + vector);
Vector.set (0, "a");
SYSTEM.OUT.PRINTLN ("Change the No. 0 position this element to a:" + vector);
}
This section of the method is called if the main function calls:
SYSTEM.OUT.PRINTLN ("The test of the ======vector data structure begins to be = =");
Vectortest ();
SYSTEM.OUT.PRINTLN ("Test end of ======VECTOR data structure = = =");
The results of the operation are as follows:
======vector data structure Test start = =
Simple add means adding elements from the end: [1.6, 2.06, 1]
Size () can find the number of elements contained in a vector: 3
Remove (1) means to delete the 1th element, since the count begins at 0, which is the element of 2.06: [1.6, 1]
The vector of the last element is deleted: [1.6]
Add 1.8888 to this element in position No. 0: [1.8888, 1.6]
Change the No. 0 position this element to A:[a, 1.6]
======vector data structure Test end = =
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" adds an element 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 1th element, since the count begins at 0, that is, the 4 element: "+ ArrayList);
Arraylist.remove (Arraylist.size ()-1);
System.out.println ("Delete the ArrayList of the last element as:" + ArrayList);
Arraylist.add (0, 1.8888);
System.out.println ("Add 1.8888 of this element in the No. 0 position:" + ArrayList);
Arraylist.set (0, 9.0);
SYSTEM.OUT.PRINTLN ("Change the No. 0 position this element to a:" + ArrayList);
Collections.sort (ArrayList);
System.out.println ("Sort" + ArrayList is supported if ArrayList is not an abstract type);
Here you can see that ArrayList deletes the last element in a way that is different from the vector, mainly ArrayList there is no lastelement () This method to remove the last element remove (), only arraylist.size ()- To determine the position of the last element. If this method is called in this way in the main function:
SYSTEM.OUT.PRINTLN ("The test of the ======arraylist data structure begins to be = =");
Arraylisttest ();
SYSTEM.OUT.PRINTLN ("Test end of ======ARRAYLIST data structure = = =");
You get the following running results:
======arraylist data structure Test start = =
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 the deletion of the 1th element, since the count begins at 0, which is the 4 element: [1.0, 5.0, 2.3]
Delete the arraylist of the last element as: [1.0, 5.0]
Add 1.8888 to this element in position No. 0: [1.8888, 1.0, 5.0]
Change the No. 0 position this element to a:[9.0, 1.0, 5.0]
If ArrayList is not an abstract type, the sort is supported [1.0, 5.0, 9.0]
======arraylist data structure Test end = =
From the above two examples, vector and ArrayList can be seen compared to an ordinary array, that is, the textbook is taught on the one-dimensional array int array[] = {8, 7, 100, 88, 6, 4, 5, 33, 7}; powerful many, you can insert elements anywhere, you can not use the The enumeration group is able to delete the elements of the specified location in a single method, but for your exams you still have to know how to traverse the array. In fact, ArrayList and the normal one-dimensional array can be fully implemented, and the use of ArrayList can also be able to sort the array directly, without having to write a bubble sort of array, and so on, directly with Collections.sort (), the array can be sorted, And then using Collections.reverse (), you can achieve reverse ordering, of course, for your exams you still have to know how the array is sorted.
For example, the following method implements the sorting and inverse ordering of the one-dimensional array int array[] = {8, 7, 100, 88, 6, 4, 5, 33, 7}, and first converts the array into ArrayList and then Collections.sort () with collections. Reverse (), sorting, and finally converting ArrayList content back to one-dimensional arrays:
public static void Arraylistsort () {
int array[] = {8, 7, M, 6, 4, 5, 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++)
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 ("Inverse sorted array:");
for (int i = 0; i < array.length i++)
System.out.print (Array[i] + ",");
After sorting the ArrayList is destroyed
arraylist = null;
This is suggested that Java immediately recycling garbage, of course, there is no line, Java in the process of running will automatically remove garbage
System.GC ();
}
This method is called in the main function:
System.out.println ("======java array sort start = = =");
Arraylistsort ();
System.out.println ("======java array sort end = =");
You will be able to get the following running results:
======java array sort start = =
8,7,100,88,6,4,5,33,7, sorted array: 4,5,6,7,7,8,33,88,100, inverse sorted array: 100,88,33,8,7,7,6,5,4,
======java array sort End = =
In addition, previously said the "Java list in the use of a simple introduction" (Click to open the link) is the same reason
Ii. Collection of HashSet
In addition, the set Hashset,hashset is identical to the mathematical set concept. The composition of one or more elements is called a collection. HashSet has:
1. Certainty , the elements of the set must be certain, this is nonsense, must be determined, can I still put an indefinite thing inside?
2. The elements of the set are different from each other. For example: Set A={1,a}, then a cannot equal 1, that is, if you put two 1 into hashset, it automatically becomes a 1
3. Disorder , the elements in the collection are not successively divided. Therefore, HashSet is also not allowed to sort operations
For example, a method like the following:
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" adds an element 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 to delete the element" 1 ":" + hashset);
Hashset.remove ("ASD");
SYSTEM.OUT.PRINTLN ("If there is no ' ASD ' this element removes nothing:" + hashset);
Hashset.add (1.8888);
System.out.println ("Add 1.8888 this element:" + hashset);
}
In the main function, call this method:
SYSTEM.OUT.PRINTLN ("The test of the ======hashset data structure begins to be = =");
Hashsettest ();
SYSTEM.OUT.PRINTLN ("Test end of ======HASHSET data structure = = =");
The results are as follows:
======hashset data structure Test start = =
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 of ' 1 ': [5, 2.3]
If there is no ' ASD ' this element then remove does nothing: [5, 2.3]
Add 1.8888 to this element: [5, 1.8888, 2.3]
======HASHSET data structure Test end = =
HashSet has the Add () method and the Remove () method, add () The elements are not in order, each time with System.out.println () printed results may be different, and not to the above vector and ArrayList, You can use Collections.sort (ArrayList) as long as the saved elements are not object;
Three or two Yuan group HashMap
The method used here is basically the same as the data above, also very simple, is put method to the object into the map, and get can take the object in the map, but trying to use the two-tuple hashmap into ternary group is wrong, if an object contains too many elements, then you should consider the class. Instead of being obsessed with Java mediations is the collections class between ordinary variables and class classes.
For example, the following method shows the error of attempting to change HashMap to a triple group:
public static void Maptest () {System.out.println ("======map incorrect use started = = =)";
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 ("======map wrong use over = =");
System.out.println ("======map correct use start = =");
Map.clear ();
Bigmap=null;
Map.put ("Key1", "1");
Map.put ("Key2", "2");
Map.put ("Key3", "3");
SYSTEM.OUT.PRINTLN (map);
System.out.println ("======map correct use of the end = ="); }
In the main function call this code, get the following run result:
======MAP data structure Test start = = = ======map wrong use start = = = {test1={key2=4, key1=3}, test2={key2=4, key1=3}
3
4
3
4
===== =map incorrect use of end = = =
======map correct use start = = =
{key3=3, key2=2, key1=1}
======map correct use end = = = = =
&NBSP;======MAP data structure Test end = = =
This procedure was intended to be obvious, trying to construct a {test1,key1,1},{test1,key2,2},{ TEST2,KEY3,3},{TEST2,KEY4,4}&NBSP
However, every bigmap is still the same map, once you empty the map, the original Test1 map is also emptied, Someone is trying to create multiple MAP1,MAP2,... Then you might as well use a simple class to look more explicit, and you can write the method later in the class, inherited
Iv. remarks
in the creation of a new vector,arraylist good, hashset or HashMap, can be written in full:
collection<string> a= new arraylist<string> ();
list<string> a= new vector<string> ();
because of the ArrayList, Vector, LinkedList, HashSet, TreeSet inherited from the collection interface, there are HashMap, Hashtable, which inherit from the map interface. This is the Java class inheritance, polymorphism, encapsulation and so on, of course, for your partner to see more clearly, here do not play what fancy naming, 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 the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.