Array: 常用Java操作

來源:互聯網
上載者:User

標籤:

1. 定義一個Java數組

1 String[] aArray = new String[5];2 String[] bArray = {"a","b","c", "d", "e"};3 String[] cArray = new String[]{"a","b","c","d","e"};

第一種是定義了一個數組,並且指定了數組的長度,我們這裡稱它為動態定義。

第二種和第三種在分配記憶體空間的同時還初始化了值。

 

2. 列印Java數組中的元素

int[] intArray = { 1, 2, 3, 4, 5 };String intArrayString = Arrays.toString(intArray);// print directly will print reference valueSystem.out.println(intArray);// [[email protected]System.out.println(intArrayString);// [1, 2, 3, 4, 5]

這裡的重點是說明了Java中數組的引用和值得區別,第三行直接列印intArray,輸出的是亂碼,因為intArray僅僅是一個地址引用。第4行輸出的則是真正的數組值,因為它經過了Arrays.toString()的轉化。對Java初學者來說,引用和值仍需重視。

 

3. 從Array中建立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]

為什麼要將Array轉換成ArrayList呢?可能是因為ArrayList是動態鏈表,我們可以更方便地對ArrayList進行增刪改,我們並不需要迴圈Array將每一個元素加入到ArrayList中,用以上的代碼即可簡單實現轉換。

 

4. 檢查數組中是否包含某一個值

String[] stringArray = { "a", "b", "c", "d", "e" };boolean b = Arrays.asList(stringArray).contains("a");System.out.println(b);// true

先使用Arrays.asList()將Array轉換成List<String>,這樣就可以用動態鏈表的contains函數來判斷元素是否包含在鏈表中。

 

5. 串連兩個數組

int[] intArray = { 1, 2, 3, 4, 5 };int[] intArray2 = { 6, 7, 8, 9, 10 };// Apache Commons Lang libraryint[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);

ArrayUtils是Apache提供的數組處理類庫,其addAll方法可以很方便地將兩個數組串連成一個數組。

 

6. 聲明一個數組內鏈(?)

method(new String[]{"a", "b", "c", "d", "e"});


7. 將數組中的元素以字串的形式輸出

// containing the provided list of elements// Apache common langString j = StringUtils.join(new String[] { "a", "b", "c" }, ", ");System.out.println(j);// a, b, c

同樣利用StringUtils中的join方法,可以將數組中的元素以一個字串的形式輸出。

 

8.  將Array轉化成Set集合

Set<String> set = new HashSet<String>(Arrays.asList(stringArray));System.out.println(set);//[d, e, b, c, a]

在Java中使用Set,可以方便地將需要的類型以集合類型儲存在一個變數中,主要應用在顯示列表。同樣可以先將Array轉換成List,然後再將List轉換成Set。

difference between Set and HashSet:

A Set represents a generic "set of values". A TreeSet is a set where the elements are sorted (and thus ordered), a HashSet is a set where the elements are not sorted or ordered.

A HashSet is typically a lot faster than a TreeSet.

 

9. 數組翻轉

int[] intArray = { 1, 2, 3, 4, 5 };ArrayUtils.reverse(intArray);System.out.println(Arrays.toString(intArray));//[5, 4, 3, 2, 1]

依然用到了萬能的ArrayUtils。 

 

10. 從數組中移除一個元素

int[] intArray = { 1, 2, 3, 4, 5 };int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new arraySystem.out.println(Arrays.toString(removed));

 

再補充一個:將一個int值轉化成byte數組

byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();for (byte t : bytes) {System.out.format("0x%x ", t);}

 

 

Reference:

http://www.codeceo.com/article/10-java-array-method.html

http://stackoverflow.com/questions/5139724/whats-the-difference-between-hashset-and-set

 

Array: 常用Java操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.