Arrays.toString(a)--->将数组a的值转换为字符串

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

posted @ 2016-01-01 15:45  丁少华  阅读(4603)  评论(0编辑  收藏  举报