|
Article Directory
1 Collection Interface
2 List interface
3 Iterator
4 Vertor
5 ArrayList
6 LinkedList
|
Main part:
(1) Collection
in Java there is a digital wrapper class (Inteager), a character wrapper class (String). There are also object wrapper classes----used to load objects (usually a class of objects). The advantage is that using methods and properties is easy to manage object.
This packaging class is called a container. Just like our water cups, the results have a certain difference. There are also many types of containers due to the different data structures. Their parent class is the collection interface, which has many methods.
These are the methods common to the container, such as add (), remove (); Collection can be divided into two major categories. This article describes the list
Collection
|_list
|_set
characteristics of the container
(1) Only objects can be stored, not basic data types
(2) length is not fixed---is more flexible than array
(3) have their own data structures
(2) List:
On the surface, "list", which is also an interface, whose subclasses are the container (collection) of the list system class
The list interface method is almost the same as collection, which is the feature of the list system: Access to the list by index. For example: Add (int index,object o); remove (int index);//ect
List----|_vector
|_arraylist
|_linkedlist
|_ Other
(3) Iterator-----iterators
role: used to access objects in the collection, but also an interface . There are only 3 ways, very important. Hasnext (), Next (), remove ();
Note this: each container has an inner class that implements the iterator . and control gets the entity object through iterator ().
Listiterator is the iterator sub-interface, the same reason, here is the way to access the element through index. Can be crud, the effect is very fierce.
I'll focus on how to use it later.
(4) Vector
Having said so many interfaces, finally came a concrete container.
Vector data Structures----array types----slower access (CRUD)---replaced by ArrayList
Eliminated, not much to say.
Look at the code:
public static void Main (string[] args) { vector v=new vector (); There are two ways to add an object---String---. V.add ("ABC1"); V.addelement ("ABC2"); V.add ("ABC3"); Iterate with iterators. Access object for (Iterator it=v.iterator (); It.hasnext ();) { System.out.println (It.next ()); } }
(5)ArrayList
Data Structure-- Array
Features: Fast access, slow query, and vector similar, so instead of vector
Usage:
public static void Main (string[] args) { List list=new ArrayList (); List.add ("ABC1"); List.add ("ABC2"); List.add ("ABC3"); Iterator it=list.iterator (); while (It.hasnext ()) { Object obj=it.next (); if (Obj.equals ("ABC2")) List.add ("abc0");//exception occurred
} }
An exception appears above and cannot be traversed in an iterator to add an object to the container. At this point the listiterator function, using this iterator add (e E) There is no problem
public static void Main (string[] args) { List list=new ArrayList (); List.add ("ABC1"); List.add ("ABC2"); List.add ("ABC3"); Listiterator it=list.listiterator (); while (It.hasnext ()) { Object obj=it.next (); if (Obj.equals ("ABC2")) It.add ("abc0"); } SYSTEM.OUT.PRINTLN (list); [ABC1, ABC2, ABC0, ABC3] }
To be blunt is to rewrite the list inside the method + some of their own methods.
(6) LinkedList
---List of data structures
Method Rich:
GetFirst ();
Get (index);
GetLast ();
Removefirst ();
Set (int index, E Element);//Replace
such as
Code: Using the LinkedList simulation stack-----FIFO
Package Cn.com.list;import Java.util.linkedlist;public class Listdemo {public static void main (string[] args) {//using Lin Kedlist mimics stacks and queues. Create stacks, Mystack stack=new mystack (); Into the Stack Stack.add ("ABC1"); Stack.add ("ABC2"); Stack.add ("ABC3"); Out Stack while (Stack.hasnext ()) {System.out.println (Stack.get ()); } System.out.println ("____________________________"); Create a queue similar to the above
}//Stack public static class Mystack{private LinkedList list=null;public mystack () {list=new linkedlist ();} Public Boolean Add (Object o) {list.addfirst (o); return true; } public Object get () {return List.removefirst (); Public Boolean Hasnext () {if (List.size () ==0) return false; else return true; }}//Queue public static class queue{private LinkedList list=null, public queue () {list=new linkedlist (), Public Boolea n Add (Object obj) {list.addfirst (obj); return true; Public Object get () {return list.removelast ()}, Public Boolean Hasnext () {if (List.size () ==0) return false; else return true; } }}
List of the three main members of the class introduction, the next section about set system
Note: Simple summary, more functions look at the API
The first day of the Java collections framework