機(jī)構(gòu)檔案
- 機(jī)構(gòu)級別:普通會員
- 信用等級:
資料認(rèn)證
未通過身份證認(rèn)證
未通過辦學(xué)許可認(rèn)證
- 學(xué)校瀏覽人次:次
- 加盟時(shí)間:2017年03月10日
新聞動(dòng)態(tài)
西安尚學(xué)堂:Java中Array的幾個(gè)簡單用法
發(fā)布者:西安尚學(xué)堂 發(fā)布時(shí)間:2017-05-04 來源:西安尚學(xué)堂
Java是一種面向?qū)ο蟮木幊陶Z言,具有跨平臺性,在軟件開發(fā)中應(yīng)用極為廣泛,我們都知道Array(數(shù)組)通常意義上講只是一個(gè)單純的線性序列,又基于Native,憑此它的效率歷來便號稱Java中最高。所以通常我們也都承認(rèn)Java中效率最高的存儲方式就是使用數(shù)組。在這里西安尚學(xué)堂小編給大家簡單的分析Java中Array的幾個(gè)簡單的使用方法。
1.創(chuàng)建/聲明一個(gè)數(shù)組
String[] aArray = new String[5]; String[] bArray = {"a","b","c", "d", "e"}; String[] cArray = new String[]{"a","b","c","d","e"};
2.Java中打印數(shù)組
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)建一個(gè)ArrayList
String [ ] stringArray = { "a" , "b" , "c" , "d" , "e" } ; ArrayList < String > arrayList = new ArrayList < String > ( Arrays . asList ( stringArray ) ) ; System . out . println ( arrayList ) ; // [A,B,C,D,E]
4.檢查數(shù)組中是否包含特定的值
String[] stringArray = { "a", "b", "c", "d", "e" }; boolean b = Arrays.asList(stringArray).contains("a"); System.out.println(b);
5.結(jié)合兩個(gè)數(shù)組
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.聲明一個(gè)數(shù)組的方法
method(new String[]{"a", "b", "c", "d", "e"});
7.加入所提供的數(shù)組中的元素連接成一個(gè)字符串
// 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. Array與List之間的轉(zhuǎn)換
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ù)組轉(zhuǎn)換成set
Set set = new HashSet(Arrays.asList(stringArray)); System.out.println(set); //[d, e, b, c, a]
10.數(shù)組反向輸出
int[] intArray = { 1, 2, 3, 4, 5 }; ArrayUtils.reverse(intArray); System.out.println(Arrays.toString(intArray)); //[5, 4, 3, 2, 1]
11.刪除數(shù)組元素
int[] intArray = { 1, 2, 3, 4, 5 }; int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array System.out.println(Arrays.toString(removed));
12.int轉(zhuǎn)換成byte數(shù)組
byte[] bytes = ByteBuffer.allocate(4).putInt(8).array(); for (byte t : bytes) { System.out.format("0x%x ", t);