創(chuàng)建一個(gè)數(shù)組奕污。
String [ ]a=new String[5];
String [] b={“a”,”b”,”c”,“d”,“e”};
String [] c=new String[]{“a”,”b”,”c”,”d”,”e”};
1.打印數(shù)組
我們經(jīng)常使用for循環(huán)或者一些迭代器來(lái)打印出數(shù)組的所有元素挥唠,但我們也可以換個(gè)姿勢(shì)。
int array[] = {1,2,3,4,5};
System.out.println(array); //[I@1234be4e
String arrStr = Arrays.toString(array);
System.out.println(array); //[1,2,3,4,5];
2.創(chuàng)建ArrayList
String[] array = { “a”, “b”, “c”, “d”, “e” };
ArrayList<String> arrayList =
? ? ? ? ? ? ? ? ? ? ? ? new ArrayList<String>(Arrays.asList(array));
System.out.println(arrayList);
// [a, b, c, d, e]
3.檢查是否包含某個(gè)值
int array[] = {1,2,3,4,5};
boolean isContain= Arrays.asList(array).contains(5);
System.out.println(isContain);
// true
4.連接兩個(gè)數(shù)組
int[] array1 = { 1, 2, 3, 4, 5 };
int[] array2 = { 6, 7, 8, 9, 10 };
// Apache Commons Lang library
int[] combinedIntArray = ArrayUtils.addAll(array1, array2)
5.在一行聲明一個(gè)數(shù)組
method(new String[]{"a", "b", "c", "d", "e"});
6.數(shù)組倒置
int[] intArray = { 1, 2, 3, 4, 5 };
// Apache Commons Lang library
ArrayUtils.reverse(intArray);
System.out.println(Arrays.toString(intArray));
//[5, 4, 3, 2, 1]
7.刪除某個(gè)元素
int[] intArray = { 1, 2, 3, 4, 5 };
int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array
System.out.println(Arrays.toString(removed));
8.轉(zhuǎn)化為set
Set<String> set = new HashSet<String>(Arrays.asList(new String[]{"a", "b", "c", "d", "e"}));
System.out.println(set);
//[d, e, b, c, a]
9.將ArrayList轉(zhuǎn)化為Array
String[] stringArray = { "a", "b", "c", "d", "e" };
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));
String[] stringArr = new String[arrayList.size()];
arrayList.toArray(stringArr);
10.將數(shù)組元素組成一個(gè)字符串
// Apache common lang
String j = StringUtils.join(new String[] { "a", "b", "c" }, ", ");
System.out.println(j); //a, b, c,