日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java数组的十大方法

發布時間:2023/12/18 java 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java数组的十大方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Java數組的十大方法
以下是Java Array的前10種方法。他們是來自stackoverflow的投票最多的問題。

  • 0.聲明一個數組
String[] aArray = new String[5]; String[] bArray = {"a","b","c", "d", "e"}; String[] cArray = new String[]{"a","b","c","d","e"};
  • 1.用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]
  • 2.從數組中創建一個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]
  • 3.檢查數組是否包含某個值
String[] stringArray = { "a", "b", "c", "d", "e" }; boolean b = Arrays.asList(stringArray).contains("a"); System.out.println(b); // true
  • 4.連接兩個數組
int[] intArray = { 1, 2, 3, 4, 5 }; int[] intArray2 = { 6, 7, 8, 9, 10 }; // Apache Commons Lang library int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);
  • 5.內聯聲明一個數組
method(new String[]{"a", "b", "c", "d", "e"});
  • 6.將提供的數組的元素連接成單個字符串
// 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
  • 7.將ArrayList包含到數組中
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); for (String s : stringArr)System.out.println(s);
  • 8.將數組轉換為集合
Set<String> set = new HashSet<String>(Arrays.asList(stringArray)); System.out.println(set); //[d, e, b, c, a]
  • 9.反轉一個數組
int[] intArray = { 1, 2, 3, 4, 5 }; ArrayUtils.reverse(intArray); System.out.println(Arrays.toString(intArray)); //[5, 4, 3, 2, 1]
  • 10.刪除數組的元素
int[] intArray = { 1, 2, 3, 4, 5 }; int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array System.out.println(Arrays.toString(removed));
  • 再多一個 - 將int轉換為字節數組
byte[] bytes = ByteBuffer.allocate(4).putInt(8).array(); for (byte t : bytes) {System.out.format("0x%x ", t); }

轉:https://www.programcreek.com/2013/09/top-10-methods-for-java-arrays/

轉載于:https://www.cnblogs.com/hglibin/p/9001876.html

總結

以上是生活随笔為你收集整理的Java数组的十大方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。