Arrays.toString(數組)是java內置類Arrays類的一個方法,具體查Api可知。因為數組是不可直接輸出的,它的作用是將數組轉換為字符串。其實用for循環也是可以做到的,只不過比for循環省事。
字符串數組轉字符串
Arrays.toString(數組)方法,輸出數組成員
public class Demo { static String[] a={"Tom","jack"}; public static void main(String[] args) { System.out.println(a);//數組的地址 System.out.println(Arrays.hashCode(a));//將字符串數組轉為哈希值 System.out.println(Arrays.toString(a));//將字符串數組轉為字符串 } }
[Ljava.lang.String;@4b6e3f87
5867694
[Tom, jack]
for循環輸出數組成員
public class Demo { static String[] a={"Tom","jack"}; public static void main(String[] args) { for(int i=0; i<a.length; i++){ System.out.println(a[i]); } } }
Tom jack
字節數組的轉換字符串
public class Demo { static String str="ok"; static byte[] a=str .getBytes();//getBytes()方法將字符串str轉為字節數組a。 public static void main(String[] args) { System.out.println(a);//數組的地址 System.out.println(Arrays.hashCode(a));//將字符串數組轉為哈希值 System.out.println(Arrays.toString(a));//輸出字節對應的ascii碼表值,其中o對應111,對應107 System.out.println(new String(a));//輸出字節數組對應的字符串 } }
[B@188edd79
4509
[111, 107]
ok