Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!
Some objects in Java are calledContainer(Container). A container can contain multiple objects. Each object is called an element in a container. Containers are encapsulated by objects.Data Structure(Data structure ).
Container full of dreams
Different data structures have different ways of organizing elements, and they can also have different operations. The operational efficiency of the data structure varies according to the specific implementation. The same is true for containers in Java. We need to select the appropriate container to meet the changing needs.
(For more information about the data structure, refer to the paper: algorithms and data structures)
Array
Array)Is the most common data structure. An array is an ordered set of elements of the same type and has a fixed size (can accommodate a fixed number of elements ). Array can be based onSubscript (INDEX)Random Access element. In memory, arrays are usually sequential storage units.
JAVA supports the array data structure. We need to describe the type and size of each array. As follows:
Public Class Test { Public Static Void Main (string [] ARGs) {HUMAN [] persons = New Human [2 ];// Array size 2Persons [ 0] = New Human (160 ); Persons [ 1] = New Human (170 ); Int [] A = {1, 2, 3, 7, 9 };// Array size 5System. Out. println ([ 2 ]); String [] Names = {"Tom", "Jerry", "Luffy" };// Array size 3System. Out. println (Names [ 0 ]) ;}}
When specifying the type, add[]Is an array. UseNewSpecify the size of the array when creating a container.
We can useArray name [subscript]To call an element. We can initialize the elements of the array one by one, or use{}Initialize the array.
For non-basic types of arrays, such as human [], the array stores object references.
we can call system. arraycopy () method to effectively copy an array:
Public class test { Public static void main (string [] ARGs) { int [] afrom = {1, 2, 3, 7, 9 }; /// array size 5 int [] ATO = New int [3 ]; system. arraycopy (afrom, 1, ATO, 0, 3 ); system. out. println (ATO [ 1 ]) ;}
System. arraycopy,AfromFor the array to be copied,AtoFor the array to be copied,1It is the starting position of the element to be copied from afrom,0It is the starting position of the elements to be copied in ATO,3The total number of elements to be copied.
Collection
Table (list)AndSet)Is the two interfaces defined in Java. util ). Both interfaces are inherited from the collection interface. Through the implementation interface, we can obtain the corresponding container.
We used classes to describe the referenced types. In fact, we can also use interfaces to describe the referenced type.The object to which this type of reference points must implement this interface..
let's first use table (list) container. List is a set of elements in ordered . Therefore, subscript can be used to describe the position of an element. elements can be equal :
Import JAVA. util. * ; Public class test { Public static void main (string [] ARGs) {list
L1 =
New arraylist
(); l1.add ( "good"
); l1.add ( "bad"
);
l1.add (" shit ");
l1.remove (0);
system. out. println (l1.get (1); system. out. println (l1.size () ;}
When we define interfaces and create containers, we use<Class>To describe the types of elements in the container. We can only put the class and its class objects in the container.
The container references the list type, but the implementation of the container is the arraylist class. The interface and implementation are separated here. In fact, the same abstract data structure (ADT) can all have multiple implementation methods (for example, stacks can be implemented as arrays and linked lists ). Such separation allows us to select the ADT implementation method more freely.
We can define the <Object> type container. Because all classes in Java inherit from the object class, such containers can actually put any type of objects.
AboveProgramThe container is of the string type. We use
- Add ()Method to add new elements
- Get ()Method to obtain the elements in the container and pass an integer subscript as the parameter
- Remove ()You can delete the elements in the container by passing an integer subscript as the parameter. (There is another remove (), passing the element itself as a parameter)
- Size ()Return the total number of elements in the container.
List official documentation
Set)It is also a set of elements. CollectionEquivalent elements are not allowed., Set ElementNo order:
Import JAVA. util. * ; Public class test { Public static void main (string [] ARGs) {set
S1 =
New hashset
(); s1.add ( 4
); s1.add ( 5
); s1.add ( 4
);
s1.remove (5); system. out. println (S1); system. out. println (s1.size () ;}
The repeated element 4 is put into the container only once. Because the set is unorderedRemove ().
Set official documentation
Both list and set inherit from the collection interface. Collection represents the collection of objects. Many methods in the above list and set interfaces actually inherit from the collection, for example:
Add ("good ")Add Element
Size ()Total number of returned Elements
Contains ("bad ")Include element?
Remove ("good ")Delete Element
Official Collection documentation
There is also a collectionIterator (). This method encapsulates the collection containerIterator). The iterator is a collection of elements. It has the next () method, which is used to return an element each time until the elements in the iterator are exhausted.
Import Java. util .* ; Public Class Test { Public Static Void Main (string [] ARGs) {list <Integer> L1 = New Arraylist <integer> (); L1.add ( 4 ); L1.add ( 5 ); L1.add ( 2 ); Iterator I = L1.iterator (); While (I. hasnext () {system. Out. println (I. Next ());}}}
Iterator official documentation
Map
MAP isKey-value pairs. Each element in map is a key-value pair, that is,Key)CorrespondsObject Value). For map containers, we can use keys to find the corresponding objects.
Hash table is a common implementation method of map. For more information, seePaper discussion: hash table)
We need to declare the map key and value type. We will implement a hashmap below:
Import JAVA. util. * ; Public class test { Public static void main (string [] ARGs) {Map
m1 =
New hashmap
(); m1.put (" vamei ", 12
); m1.put ( "Jerry", 5
); m1.put ( "Tom", 18
); system. out. println (m1.get ( "vamei"
) ;}
In map, we usePut ()Method to add elements.Get ()To obtain the element.
MAP also provides the following method to return a collection:
Keyset () converts all keys to set
Values () converts all values to list
Summary
In Java, container interfaces and implementations are separated. This gives Java programmers greater freedom to choose, and of course it also increases programming difficulty.
The interface provides us with valid operations. In terms of effects, different implementations have the same effect. Of course, in different situations, the Implementation Details determine the operation efficiency.
Finally, the relationships between classes and interfaces we mentioned are as follows:
Welcome to continue reading the "Java quick tutorial" SeriesArticle