import java.util.Arrays;
public class Solution {
public static void main(String[] args) {
String myWord = "aaa bbb ccc";
String[] strings = myWord.split("");
System.out.println(strings); //[Ljava.lang.String;@1540e19d
System.out.println(Arrays.toString(strings)); //[a, a, a, , b, b, b, , c, c, c]
char[] chars = myWord.toCharArray();
System.out.println(chars); //aaa bbb ccc
System.out.println(chars.toString()); //[C@1540e19d
System.out.println(Arrays.toString(chars)); //[a, a, a, , b, b, b, , c, c, c]
System.out.println(String.valueOf(chars)); //aaa bbb ccc
String myWord2 = "111 222 333";
String[] strings2 = myWord2.split("");
System.out.println(strings2); //[Ljava.lang.String;@14ae5a5
System.out.println(Arrays.toString(strings2)); //[1, 1, 1, , 2, 2, 2, , 3, 3, 3]
char[] chars2 = myWord2.toCharArray();
System.out.println(chars2); //111 222 333
System.out.println(chars2.toString()); //[C@677327b6
System.out.println(Arrays.toString(chars2)); //[1, 1, 1, , 2, 2, 2, , 3, 3, 3]
System.out.println(String.valueOf(chars2)); //111 222 333
int[] ints = new int[]{1,2,3,4};
System.out.println(ints.toString()); //[I@14ae5a5
System.out.println(Arrays.toString(ints)); //[1, 2, 3, 4]
System.out.println(String.valueOf(ints)); //[I@14ae5a5
}
}
如果要把字符串变成数组:
第一种方法:先split,将字符串变为字符串数组,这样打印是以字符串的形式打印,再Arrays.toString(字符串数组),这样打印是以数组的形式打印;
第二种方法:字符串.toCharArray(),生成字符数组。
如果要把字符数组变成字符串:
第一种方法:Arrays.toString(字符数组);
第二种方法:String.valueOf(字符数组);
如果要把整型数组变成字符串:
Arrays.toString(整型数组);
关于输出:
public class Solution {
public static void main(String[] args) {
char[] chars = new char[]{'a', 'b', 'c'};
int[] ints = new int[]{1,2,3};
String[] strings = new String[]{"aaa", "bbb", "ccc"};
System.out.println(chars); //abc
System.out.println(ints); //[I@1540e19d
System.out.println(strings); //[Ljava.lang.String;@677327b6
}
}
System.out.println(char数组):输出的是char数组的内容
System.out.println(其他类型的数组):当作Object类型处理,自动调用了String.valueOf()方法,输出的是getClass().getName() + '@' + Integer.toHexString(hashCode()),即数组hash码的字符串表示形式,作为16位中的无符号整数。