-
int[] arr ={
1,
2,
3,
4,
5};
-
String arrString = Arrays.toString(arr);
-
//輸出[I@7150bd4d
-
System.out.println(arrString);
-
//輸出[1, 2, 3, 4, 5]
java里,所有的類,不管是java庫里面的類,或者是你自己創建的類,全部是從object這個類繼承的。object里有一個方法就是toString(),那么所有的類創建的時候,都有一個toString的方法。
java輸出用的函數print();是不接受對象直接輸出的,只接受字符串或者數字之類的輸出。那么你想把一個創建好的對象拿來輸出怎么辦
-
package com.spring.h3;
-
-
public
class Test2 {
-
public static void main(String[] args) {
-
System.out.println(
"new Test2()==="+
new Test2());
-
//輸出結果為:new Test2()===com.spring.h3.Test2@18a992f
-
}
-
}
按照print接受的類型來說,s1是不能直接輸出的,那么是否代表這個是不能編譯運行的呢?當然不是。因為當print檢測到輸出的是一個對象而不是字符或者數字時,那么它會去調用這個對象類里面的toString 方法,輸出結果為[類型@哈希值]。Object類中的toString()方法的源代碼如下:
-
/**
-
* Returns a string representation of the object. In general, the
-
* <code>toString</code> method returns a string that
-
* "textually represents" this object. The result should
-
* be a concise but informative representation that is easy for a
-
* person to read.
-
* It is recommended that all subclasses override this method.
-
* <p>
-
* The <code>toString</code> method for class <code>Object</code>
-
* returns a string consisting of the name of the class of which the
-
* object is an instance, the at-sign character `<code>@</code>', and
-
* the unsigned hexadecimal representation of the hash code of the
-
* object. In other words, this method returns a string equal to the
-
* value of:
-
* <blockquote>
-
* <pre>
-
* getClass().getName() + '@' + Integer.toHexString(hashCode())
-
* </pre></blockquote>
-
*
-
* @return a string representation of the object.
-
*/
-
public String toString() {
-
return getClass().getName() +
"@" + Integer.toHexString(hashCode());
-
}
而數組類中並沒有對此方法重寫(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. 聲明一個數組
-
String[] arr1 =
new String[
5];
-
String[] arr2 = {
"a",
"b",
"c",
"d",
"e"};
-
String[] arr3=
new String[]{
"a",
"b",
"c",
"d",
"e"};
2. 輸出一個數組
-
int[] arr = {
1,
2,
3,
4,
5 };
-
String arrString = Arrays.toString(arr);
-
-
// 直接輸出,為內存地址
-
System.out.println(arr);
-
// [I@139a55
-
-
System.out.println(arrString );
-
// [1, 2, 3, 4, 5]
3. 檢查一個數組是否包含某值
-
String[] arr= {
"a",
"b",
"c",
"d",
"e" };
-
boolean b = Arrays.asList(arr).contains(
"a");
-
System.out.println(b);
-
// true
4. 連接兩個數組
方法一
-
//使用Apache Commons Lang library
-
-
int[] arr1 = {
1,
2,
3,
4,
5 };
-
int[] arr2= {
6,
7,
8,
9,
10 };
-
int[] combArr = ArrayUtils.addAll(arr1 , arr2);
方法二
-
// System.arraycopy()
-
static String[] concat(String[] a, String[] b) {
-
String[] c =
new String[a.length + b.length];
-
System.arraycopy(a,
0, c,
0, a.length);
-
System.arraycopy(b,
0, c, a.length, b.length);
-
return c;
-
}
方法三
-
//Arrays.copyOf()
-
-
public
static
int[] concat(
int[] first,
int[] second) {
-
int[] result = Arrays.copyOf(first, first.length + second.length);
-
System.arraycopy(second,
0, result, first.length, second.length);
-
return result;
-
}
5. 逆向輸出一個數組
方法一
-
// Apache Commons Lang library
-
-
int[] arr= {
1,
2,
3,
4,
5 };
-
ArrayUtils.reverse(intArray);
-
System.out.println(Arrays.toString(intArray));
-
//[5, 4, 3, 2, 1]
方法二
-
int[] arr = {
1,
2,
3,
4,
5 };
-
int[] revArr =
new
int[arr.length];
-
for(
int i =
0; i < arr.length; i++){
-
revArr[i] = arr[arr.length - i -
1];
-
}
-
System.out.println(Arrays.toString(revArr));
-
-
//[5, 4, 3, 2, 1]
6. 移除數組中的元素
-
// Apache common lang
-
-
int[] arr= {
1,
2,
3,
4,
5 };
-
int[] removed = ArrayUtils.removeElement(intArray,
3);
//create a new array
-
System.out.println(Arrays.toString(removed))