引用:https://blog.csdn.net/T2080305/article/details/84651873
https://blog.csdn.net/T2080305/article/details/84651873
https://blog.csdn.net/rambler_designer/article/details/78144808
ArrayList
創建對象:
\[ArrayList<要存儲元素的數據類型> 變量名 = new ArrayList<要存儲元素的數據類型>(); \]
基本數據類型 對應的引用數據類型表示形式
byte Byte
short Short
Int Integer
long Long
float Float
double Double
char Character
boolean Boolean
從數組創建一個 ArrayList
String[] stringArray = { "a", "b", "c", "d", "e" };
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));
增加元素到鏈表中
boolean add(Element e) //增加指定元素到鏈表尾部.
boolean add(int index, Element e) //增加指定元素到鏈表指定位置.
boolean addAll(Collection<? extends E> c) //將指定collection中的所有元素插入到ArrayList中
boolean addAll(int index, Collection<? extends E> c) //從指定的位置開始,將指定collection 中的所有元素插入到ArrayList中
從鏈表中刪除元素
void clear() //從鏈表中刪除所有元素.
E remove(int index) //刪除鏈表中指定位置的元素.
boolean removeIf(Predicate<? super E> filter) //根據重寫Predicate類的test方法選擇刪除集合中的元素
boolean removeAll(Collection<?> c) //移除ArrayList中Collection所包含的所有元素
boolean remove(Object o) //移除ArrayList中首次出現的指定元素(如果存在則移除並返回true,否則返回false)
獲取鏈表中的元素
E get(int index) //獲取鏈表中指定位置處的元素.
Object[] toArray() //獲取一個數組,數組中所有元素是鏈表中的元素.(即將鏈表轉換為一個數組)
<T> T[] toArray(T[] a) //構造一個數組
List<E> subList(int fromIndex, int toIndex) //獲取從fromIndex到toIndex位置的元素
修改某個元素
E set(int index, E element) //將鏈表中指定位置上的元素替換成新元素。
搜索元素
boolean contains(Object o) //如果鏈表包含指定元素,返回true.
int indexOf(Object o) //返回元素在鏈表中**第一次**出現的位置,如果返回-1,表示鏈表中沒有這個元素。
int lastIndexOf(Object o) //返回元素在鏈表中**最后一次**出現的位置,如果返回-1,表示鏈表中沒有這個元素。
檢查鏈表是否為空
boolean isEmpty() //返回true表示鏈表中沒有任何元素. 判斷邏輯是size == 0
獲取鏈表大小
int size() //返回鏈表長度(鏈表包含元素的個數).
反轉鏈表/排序鏈表
Collections.reverse(arrayList);
//sort方法:最大的問題,沒辦法sort list部分,只能用array
Comparator c = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
// TODO Auto-generated method stub
if((int)o1<(int)o2)
return 1;
//注意!!返回值必須是一對相反數,否則無效。jdk1.7以后就是這樣。
//else return 0; //無效
else return -1;
}
};
Collections.sort(listStr);
list01.sort(c);//筆試時經常使用Java7,list並沒有sort方法,請使用Collections.sort()
Collections.sort(list02,c);
數組
聲明數組
String[] aArray = new String[5];
String[] bArray = {"a","b","c", "d", "e"};
String[] cArray = new String[]{"a","b","c","d","e"};
轉成String
String intArrayString = Arrays.toString(intArray);//不推薦,輸出[1, 2, 3, 4, 5]
char[] t = new char[]{'a','b','c'};
String s = new String(t);//輸出“abc”
合並數組
int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);
把數組中的元素用指定的分隔符連接起來
String j = StringUtils.join(new String[] { "a", "b", "c" }, ", ");//輸出a, b, c
反轉數組
ArrayUtils.reverse(intArray);
移除數組中的元素
int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array
復制
1.arraycopy(sourceArray,int index1,copyArray,index2,int length):從sourceArray的index1位置開始,后面length個元素,放到copyArray數組從index2的位置
int []a = {1,2,3,4,5};
int []b = {6,7,8,9,10};
System.arraycopy(a, 2, b, 3, 2);//b為6 7 8 3 4
2.copyOf(float []original,int newLength):從數組的第一個元素開始復制,復制長度為length,若長度超過數組原長,則超出元素為默認值0,返回一個數組
int []a = {11,22,33,44,55};
int []b = Arrays.copyOf(a, 7);//11 22 33 44 55 0 0
3.copyOfRange(double []original,int from,int to)
int []a = {55,33,44,22,11};
int []b = Arrays.copyOfRange(a, 1, 4);//[33, 44, 22]
sort
sort(doule a[]);//將數組按升序全排序
sort(doule a[],int start,int end);//從索引為start到索引為end-1的位置,升序排序
在數組中查找一個數
binarySearch(double [] a,double number);//返回要尋找的數number的索引,若沒查找到,則返回一個負數