Today did the Medallia company's Java interview problem, found that the use of C + + after the Java feel unusually uncomfortable, especially the data structure in Java How to use the headache, and began to organize and practice the Java API about the use of data structures. Oracle's Java API provides explanations for each data structure but does not provide relevant examples, which is inconvenient because most of the time we read examples to learn how to use it, which is the most useful way I learn C + +.
Vector
Oracle API: "The Vector class implements a growable array of objects. Like a array, it contains components that can is accessed using an integer index. However, the size of a vector can grow or shrink as needed to accommodate adding and removing items after the vector have B Een created. "
Insert ()
Importjava.util.*; Public class Insert{ Public Static void Main(string[] arguments) {Vector<integer> Vector =NewVector<integer> ();//Insert for(inti =0; I <3; i = i+1) {Vector.add (i);//Append at the end} for(inti =0; I < vector.size (); i = i+1) {System.out.println (Vector.elementat (i));//Print 0 1 2} }}
Remove ()
Importjava.util.*; Public class Remove{ Public Static void Main(string[] arguments) {Vector<integer> Vector =NewVector<integer> ();//Insert for(inti =0; I <6; i = i+1) {Vector.add (i);//Append at the end}//RemoveVector.remove (3); for(inti =0; I < vector.size (); i = i+1) {System.out.print (Vector.elementat (i));//Print 0 1 2 4 5} System.out.println (); }}
Linked List
Oracle API: "doubly-linked list implementation of the list and Deque interfaces. Implements all optional The list operations, and permits all elements (including null). "
Importjava.util.*; Public class Demo{ Public Static void Main(string[] arguments) {Linkedlist<integer> LinkedList =NewLinkedlist<integer> (); Linkedlist.add (2);//Add 2 to the end of listLinkedlist.addfirst (1);//Add 1 to the head of listLinkedlist.addlast (4);//Add 4 to the end of listLinkedlist.add (2,3);//Insert 3 at index 2 for(inti =0; I < linkedlist.size (); i = i+1) {System.out.println (Linkedlist.get (i)); } System.out.println ();//RemoveLinkedlist.remove (Linkedlist.size ()-1); for(inti =0; I < linkedlist.size (); i = i+1) {System.out.println (Linkedlist.get (i));//Print 1 2 3} System.out.println ();//SearchSystem.out.println (Linkedlist.indexof (1) );//Print 0}}
Stack
Oracle API:
The Stack class represents a Last-in-first-out (LIFO) Stack of objects.
A more complete and consistent set of LIFO stacks operations is provided by the Deque interface and its implementations, WH Ich should is used in preference to this class. For example:
new ArrayDeque<Integer>();
Importjava.util.*; Public class Demo{ Public Static void Main(string[] arguments) {Stack stack =NewStack ();//deque<integer> stack = new arraydeque<integer> (); //Insert for(inti =0; I <3; i = i+1) {Stack.push (i); }//Delete while(!stack.isempty ()) {System.out.println (Stack.pop ());//Print 2 1 0} }}
Queue
Importjava.util.*; Public class Demo{ Public Static void Main(string[] arguments) {Queue<integer> Queue =NewLinkedlist<integer> ();//Insert for(inti =0; I <3; i = i+1) {Queue.add (i); }//Delete while(Queue.peek ()! =NULL) {System.out.println (Queue.poll ());//Print 0 1 2} }}
Map
Importjava.util.*; Public class Demo{ Public Static void Main(string[] arguments) {map<string, integer> Map =NewHashmap<string, integer> ();//InsertMap.put ("a",1); Map.put ("B",2); Map.put ("C",3); iterator< map.entry<string, integer> > iter = Map.entryset (). Iterator (); while(Iter.hasnext ()) {map.entry<string, integer> pair = Iter.next (); System.out.println (Pair.getkey () +" --"+ Pair.getvalue ()); }//RemoveSystem.out.println ("Now removing"+ Map.Remove ("B")); iter = Map.entryset (). iterator (); while(Iter.hasnext ()) {map.entry<string, integer> pair = Iter.next (); System.out.println (Pair.getkey () +" --"+ Pair.getvalue ()); }//SearchSystem.out.println (" The first element is"+ Map.get ("a")); }}
Data structure usage in Java (continuous update ...) )