Detailed explanation of common syntax of Java array ArrayList
Fill in the foundation and record the common syntax of array ArrayList
1. Import
import java.util.ArrayList;
2. Define array list
ArrayList < class name > List = new ArrayList < class name > (); Cannot be a basic type, must be a class
3. Get collection size
size()
4. Store data
add(Object object); Join from subscript 0
add(int idx,Object object); Insert the object into the position with index idx, IDX < = list size();
When saving different objects, you need a single new. You can't add an array by changing the value. (memory involved)
5. Delete
remove(int idx); Delete the element with index idx and return the element. It can be received with variables or not
6. Empty
clear(); Empty array
7. Replacement
set(int idx,Object object); Replace the object element with the element originally indexed as IDX
8. Get the specified location element
Object get(int idx);
9. Air judgment
bool isEmpty(); Generally not, size () can be used to judge null
10. Determine whether there is an element
bool contains(Object object); Basically not. It can be solved by looking up the index of the element
11. Find the index of the element
int indexOf(Object object); If the element exists, the index is returned. Otherwise, it returns - 1. Judge whether the element is in the array by whether it is - 1 or not
12.. Sort array list
Import collections class;
import java.util.Collections;
(1) Default natural sorting, from small to large
Collections. sort(list); // You can't new out the object of collections. Use it directly
(2) Custom sorting
Import comparator class;
import java.util.Comparator;
When creating objects, you need to implement the abstract method compare() to implement custom sorting
package my_acm; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator;//自定义排序 //import java.lang.Integer;//lang包中的类不用导也能直接用 public class MyTest4 { public static void main(String [] args) { ArrayList<Point> list1 = new ArrayList<Point>(); ArrayList<Integer> list2 = new ArrayList<Integer>(); //ArrayList<int> list3 = new ArrayList<int>();//报错,基本数据类型不能 Comparator<Point> comparator = new Comparator<Point>() { public int compare(Point p1,Point p2) { if(p1.id!=p2.id) return p1.id-p2.id; else { if(p1.age!=p2.age) return p1.age-p2.age; else return 0;//不像C++,这里的if-else需要匹配 } } }; Point p1 = new Point(); p1.id=11;p1.age=21; list1.add(p1); Point p2 = new Point(); p2.id=9;p2.age=44; list1.add(p2); Point p3 = new Point(); p3.id=2;p3.age=68; list1.add(p3); /** 修改变量的值在加到数组里是不可行的,( p3.id=14;p3.age=23; 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 results:
i=0 id=11 age=21 i=1 id=9 age=44 i=2 id=2 age=68
After sorting by ID
i=0 id=2 age=68 i=1 id=9 age=44 i=2 id=11 age=21
The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.