Catching basis, record what common syntax array of ArrayList
1. Import
import java.util.ArrayList;
2. Define the array list
ArrayList <class name> list = new ArrayList <class name> (); can not be a basic type, must be a class
3. Get set size
size()
4. Data stored
add (Object object); start from index 0 was added
add (int idx, Object object); inserting the object position index idx, idx <= list.size ();
Different deposited into a single object to new, not by changing the value after addition of the array. (Involved in memory)
5. Delete
remove (int idx); delete the index idx of the element, the element returns, to receive may be variable, or not received
6. Empty
Clear (); empty array
7. Replace
set (int idx, Object object); the original object element and the element index idx is replaced
8. Get the specified location element
Object get(int idx);
9. sentenced empty
bool isEmpty (); generally it does not, size () may be used to empty judgment
10. determines whether an element
bool contains (Object object); the basic need, can be solved by finding the index of the element
11. Find the index element
int indexOf (Object object); if the element is present, the index is returned, otherwise -1, -1 is not determined by the elements in the array is not
12. The sorting array list
Introducing Collections class;
import java.util.Collections;
(1) default natural order, from small to large
Collections.sort (list); // not a new Collections object directly
(2) custom ordering
Comparator introducing the like;
java.util.Comparator import;
When creating an object needs to implement an abstract method compare (), implement custom sorting
Package my_acm; Import of java.util.ArrayList; Import java.util.Collections; Import java.util.Comparator; // custom sorting // Import java.lang.Integer; // lang classes in the package can not be directly turned using public class MyTest4 { public static void main (String [] args) { the ArrayList <Point> = List1 new new the ArrayList <Point> (); the ArrayList <Integer> = List2 new new the ArrayList <Integer> (); // the ArrayList <int> the ArrayList = new new list3 <int> (); //Error, the basic data types can not Comparator <Point> Comparator = new new Comparator <Point> () { public int Compare (Point P1, P2 Point) { IF (p1.id! = P2.id) return p1.id- p2.id ; the else { IF (p1.age =! p2.age) return p1.age- p2.age; the else return 0; // like C ++, where the need to match if-else } } }; Point P1 = new newPoint (); p1.id =. 11; = 21 is p1.age ; list1.add (P1); Point P2 = new new Point (); p2.id =. 9; = 44 is p2.age ; list1.add (P2); P3 Point = new new Point (); p3.id = 2; 68 = p3.age ; list1.add (P3); / ** values applied to change the variable in the array is not feasible, ( p3.id = 14 ; = 23 is p3.age; list1.add (P3); * / for ( int I = 0; I <list1.size (); I ++ ) { System.out.println ( "I =" + I + "ID =" + list1.get (i) .id + " age =" +list1.get(i).age); } Collections.sort(list1,comparator); System.out.println("按id排序后"); for(int i=0;i<list1.size();i++) { Point x = new Point(); x=list1.get(i); System.out.println( "i="+i+" id="+x.id+" age="+x.age ); } } } class Point{ int id; int age; }
Output:
i=0 id=11 age=21
i=1 id=9 age=44
i=2 id=2 age=68
按id排序后
i=0 id=2 age=68
i=1 id=9 age=44
i=2 id=11 age=21