Java數(shù)組的常用方法
1、聲明一個數(shù)組
? ? ? ?String[] aArray = new String[5];
String[] bArray = {“a”,“b”,“c”, “d”, “e”};
String[] cArray = new String[]{“a”,“b”,“c”,“d”,“e”};
2. 輸出一個數(shù)組
Java代碼
int[] intArray = { 1, 2, 3, 4, 5 };
String intArrayString = Arrays.toString(intArray);
// print directly will print reference value
System.out.println(intArray);
// [I@7150bd4d
System.out.println(intArrayString);
// [1, 2, 3, 4, 5]
3. 從一個數(shù)組創(chuàng)建數(shù)組列表
Java代碼
String[] stringArray = { “a”, “b”, “c”, “d”, “e” };
ArrayList arrayList = new ArrayList(Arrays.asList(stringArray));
System.out.println(arrayList);
// [a, b, c, d, e]
4. 檢查一個數(shù)組是否包含某個值
Java代碼
String[] stringArray = { “a”, “b”, “c”, “d”, “e” };
boolean b = Arrays.asList(stringArray).contains(“a”);
System.out.println(b);
// true
5. 連接兩個數(shù)組
Java代碼
int[] intArray = { 1, 2, 3, 4, 5 };
int[] intArray2 = { 6, 7, 8, 9, 10 };
// Apache Commons Lang library
int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);
6. 聲明一個內聯(lián)數(shù)組
Java代碼
method(new String[]{“a”, “b”, “c”, “d”, “e”});
7. 把提供的數(shù)組元素放入一個字符串
Java代碼
// containing the provided list of elements
// Apache common lang
String j = StringUtils.join(new String[] { “a”, “b”, “c” }, “, ”);
System.out.println(j);
// a, b, c
8. 將一個數(shù)據列表轉換為數(shù)組
Java代碼
String[] stringArray = { “a”, “b”, “c”, “d”, “e” };
ArrayList arrayList = new ArrayList(Arrays.asList(stringArray));
String[] stringArr = new String[arrayList.size()];
arrayList.toArray(stringArr);
for (String s : stringArr)
System.out.println(s);
9. 將一個數(shù)組轉換為集(set)
Java代碼
Set set = new HashSet(Arrays.asList(stringArray));
System.out.println(set);
//[d, e, b, c, a]
10. 逆向一個數(shù)組
Java代碼
int[] intArray = { 1, 2, 3, 4, 5 };
ArrayUtils.reverse(intArray);
System.out.println(Arrays.toString(intArray));
//[5, 4, 3, 2, 1]
11. 移除數(shù)組中的元素
Java代碼
int[] intArray = { 1, 2, 3, 4, 5 };
int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array
System.out.println(Arrays.toString(removed));
12. 將整數(shù)轉換為字節(jié)數(shù)組
Java代碼
byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();
for (byte t : bytes) {
System.out.format(“0x%x ”, t);
}
Java:數(shù)組工具類Arrays類的常用方法的用法及代碼
Arrays類是JDK提供的專門用于操作數(shù)組的工具類,位于java.util包中。
用Arrays類的方法操作數(shù)組,無需自己編碼。
Arrays類的常用方法:
1、boolean equals(array1,array2):比較兩個數(shù)組是否相等。
package com.jredu.ch06;
import java.util.Arrays;
public class Ch03 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] str1={“1”,“2”,“3”};
String[] str2={“1”,“2”,new String(“3”)};
System.out.println(Arrays.equals(str1, str2));//結果是:true
}
}
2、void sort(array):對數(shù)組array的元素進行升序排列
//給一個數(shù)組進行排序
int[] score ={79,65,93,64,88};
Arrays.sort(score);//給數(shù)組排序
//sort:作用是把一個數(shù)組按照有小到大進行排序
//str代表的也是一個引用地址
String str = Arrays.toString(score);
//print方法可以直接打印字符串的值
System.out.println(str);
//Arrays.toString(score):把數(shù)組轉變成字符串
3、String toString(array):把數(shù)組array轉換成一個字符串。
import java.util.Arrays;
public class Ch05 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//toString:把一個數(shù)組轉換成一個字符串
//定義 一個數(shù)組
int[] a={1,2,3};
System.out.println(a);//打印出的是hashcode碼
System.out.println(Arrays.toString(a));//打印出的是數(shù)組
}
}
4、void fill(array,val):把數(shù)組array所有元素都賦值為val。
//fill方法:把數(shù)組中的所有元素替換成一個值
int[] num={1,2,3};
//參數(shù)1:數(shù)組對象
//參數(shù)2:替換的值
Arrays.fill(num, 6);
System.out.println(Arrays.toString(num));//打印結果:[6, 6, 6]
5、int binarySearch(array,val):查詢元素值val在數(shù)組array中的下標
/binarySearch:通過二分法的方式找對應元素的下標
//使用前提:必須經過排序才可以使用
char[] a={‘a’,‘b’,‘c’,‘d’,‘e’};
int i = Arrays.binarySearch(a, ‘d’);
System.out.println(i);//結果是:3
char[] b={‘e’,‘a’,‘c’,‘b’,‘d’};
Arrays.sort(b);
int j=Arrays.binarySearch(b, ‘e’);
System.out.println(j);
6、copyof(array,length):把數(shù)組array復制成一個長度為length的新數(shù)組。
//copyOf:把一個原有的數(shù)組內容復制到一個新數(shù)組中
int[] a={1,2,3};
//參數(shù)1:原數(shù)組 參數(shù)2:新數(shù)組的長度
int[] b=Arrays.copyOf(a, a.length);
System.out.println(Arrays.toString(b));
//a和b的地址碼不同
《pre code_snippet_id=“2518384” snippet_file_name=“blog_20170804_5_2090124”》《/pre》
《pre》《/pre》
評論