数组面试题

1.打印问题,直接打印char类型的数组即数组里面的所有元素,直接打印其他类型的数组是对象地址

String letters = "ABC"; 
char[] numbers = {'1', '2', '3'}; 
int[] aaa = {1,2,3,4,5}; // [I@610455d6
String[] bbb = {"ab", "dc"}; //[Ljava.lang.String;@61bbe9ba
System.out.print(numbers);  // 123
System.out.print(letters + " easy as "+numbers);  // ABC easy as [C@511d50c0

直接打印整个数组使用Arrays.toString(bbb); // [ab, dc]

2.数组初始化
int[] a = {1,2,3}
int b[] = {1,2,3}
int[] a = new int[]{1,2,3};
指定长度只能在new对象的后面指定,并且指定长度后不能初始化数组

猜你喜欢

转载自www.cnblogs.com/zhz-8919/p/11394509.html