java中,數組toString()方法,數組常用操作


https://blog.csdn.net/feicongcong/article/details/77893717?utm_medium=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromMachineLearnPai2~default-8.baidujs&dist_request_id=&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromMachineLearnPai2~default-8.baidujs


   
   
   
           
  1. int[] arr ={ 1, 2, 3, 4, 5};
  2. String arrString = Arrays.toString(arr);
  3. //輸出[I@7150bd4d
  4. System.out.println(arrString);
  5. //輸出[1, 2, 3, 4, 5]

java里,所有的類,不管是java庫里面的類,或者是你自己創建的類,全部是從object這個類繼承的。object里有一個方法就是toString(),那么所有的類創建的時候,都有一個toString的方法。

java輸出用的函數print();是不接受對象直接輸出的,只接受字符串或者數字之類的輸出。那么你想把一個創建好的對象拿來輸出怎么辦


   
   
   
           
  1. package com.spring.h3;
  2. public class Test2 {
  3. public static void main(String[] args) {
  4. System.out.println( "new Test2()==="+ new Test2());
  5. //輸出結果為:new Test2()===com.spring.h3.Test2@18a992f
  6. }
  7. }

按照print接受的類型來說,s1是不能直接輸出的,那么是否代表這個是不能編譯運行的呢?當然不是。因為當print檢測到輸出的是一個對象而不是字符或者數字時,那么它會去調用這個對象類里面的toString 方法,輸出結果為[類型@哈希值]。Object類中的toString()方法的源代碼如下:


   
   
   
           
  1. /**
  2. * Returns a string representation of the object. In general, the
  3. * <code>toString</code> method returns a string that
  4. * "textually represents" this object. The result should
  5. * be a concise but informative representation that is easy for a
  6. * person to read.
  7. * It is recommended that all subclasses override this method.
  8. * <p>
  9. * The <code>toString</code> method for class <code>Object</code>
  10. * returns a string consisting of the name of the class of which the
  11. * object is an instance, the at-sign character `<code>@</code>', and
  12. * the unsigned hexadecimal representation of the hash code of the
  13. * object. In other words, this method returns a string equal to the
  14. * value of:
  15. * <blockquote>
  16. * <pre>
  17. * getClass().getName() + '@' + Integer.toHexString(hashCode())
  18. * </pre></blockquote>
  19. *
  20. * @return a string representation of the object.
  21. */
  22. public String toString() {
  23. return getClass().getName() + "@" + Integer.toHexString(hashCode());
  24. }

 而數組類中並沒有對此方法重寫(override),僅僅是重載(overload)為類的靜態方法(參見java.util.Arrays)。所以,數組直接使用toString()的結果也是[類型@哈希值]。

  所以數組轉為字符串應寫成:

Arrays.toString(a) 

  這種方法的toString()是帶格式的,也就是說輸出的是[a, b, c],如果僅僅想輸出abc則需用以下兩種方法:

  方法1:直接在構造String時轉換。

char[] data = {'a', 'b', 'c'};
String str = new String(data);

  方法2:調用String類的方法轉換。

String.valueOf(char[] ch)


數組常用操作

1. 聲明一個數組


   
   
   
           
  1. String[] arr1 = new String[ 5];
  2. String[] arr2 = { "a", "b", "c", "d", "e"};
  3. String[] arr3= new String[]{ "a", "b", "c", "d", "e"};
2. 輸出一個數組

   
   
   
           
  1. int[] arr = { 1, 2, 3, 4, 5 };
  2. String arrString = Arrays.toString(arr);
  3. // 直接輸出,為內存地址
  4. System.out.println(arr);
  5. // [I@139a55
  6. System.out.println(arrString );
  7. // [1, 2, 3, 4, 5]
3. 檢查一個數組是否包含某值

   
   
   
           
  1. String[] arr= { "a", "b", "c", "d", "e" };
  2. boolean b = Arrays.asList(arr).contains( "a");
  3. System.out.println(b);
  4. // true
4. 連接兩個數組

方法一


   
   
   
           
  1. //使用Apache Commons Lang library
  2. int[] arr1 = { 1, 2, 3, 4, 5 };
  3. int[] arr2= { 6, 7, 8, 9, 10 };
  4. int[] combArr = ArrayUtils.addAll(arr1 , arr2);
方法二

   
   
   
           
  1. // System.arraycopy()
  2. static String[] concat(String[] a, String[] b) {
  3. String[] c = new String[a.length + b.length];
  4. System.arraycopy(a, 0, c, 0, a.length);
  5. System.arraycopy(b, 0, c, a.length, b.length);
  6. return c;
  7. }
方法三


   
   
   
           
  1. //Arrays.copyOf()
  2. public static int[] concat( int[] first, int[] second) {
  3. int[] result = Arrays.copyOf(first, first.length + second.length);
  4. System.arraycopy(second, 0, result, first.length, second.length);
  5. return result;
  6. }
5. 逆向輸出一個數組

方法一


   
   
   
           
  1. // Apache Commons Lang library
  2. int[] arr= { 1, 2, 3, 4, 5 };
  3. ArrayUtils.reverse(intArray);
  4. System.out.println(Arrays.toString(intArray));
  5. //[5, 4, 3, 2, 1]
方法二


   
   
   
           
  1. int[] arr = { 1, 2, 3, 4, 5 };
  2. int[] revArr = new int[arr.length];
  3. for( int i = 0; i < arr.length; i++){
  4. revArr[i] = arr[arr.length - i - 1];
  5. }
  6. System.out.println(Arrays.toString(revArr));
  7. //[5, 4, 3, 2, 1]
6. 移除數組中的元素

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






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM