java 초급) 배열
조회수 508회
아래 코드를 실행하면
1,2,3,4,5,6,7,8,9,10,
[2, 6, 10, 2, 9, 7, 5, 6, 10, 1]
[100, 95, 80, 70, 60]
[I@5d22bbb7
abcd
같은 결과가 나옵니다.
[I@5d22bbb7 에는 마지막에 왜 ] 기호가 안나왔는지 궁금합니다.
package aaa;
import java.util.*;
class aaA {
public static void main(String[] args) {
int[] iArr1 = new int[10];
int[] iArr2 = new int[10];
int[] iArr3 = {100, 95, 80, 70, 60};
char[] chArr = {'a', 'b', 'c', 'd'};
for(int i=0; i<iArr1.length; i++) {
iArr1[i] = i + 1;
}
for(int i=0; i<iArr2.length; i++) {
iArr2[i] = (int)(Math.random()*10) + 1;
}
for(int i=0; i<iArr1.length; i++) {
System.out.print(iArr1[i]+",");
}
System.out.println();
System.out.println(Arrays.toString(iArr2));
System.out.println(Arrays.toString(iArr3));
System.out.println(iArr3);
System.out.println(chArr);
}
}
1 답변
-
System.out.println(Arrays.toString(iArr2)); System.out.println(Arrays.toString(iArr3));
이 코드의 Arrays.toString() 함수가 배열 원소 처음과 끝에 [, ] 브래킷을 붙여줍니다.
그러나
System.out.println(iArr3)
iArr3 를 출력하실 때 나온 것은 배열의 원소가 아닌 Object 의 toString() 메서드입니다.
toString
public String toString() Returns a string representation of the object. In general, the toString 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. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', 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:
getClass().getName() + '@' + Integer.toHexString(hashCode())
Returns: a string representation of the object.
인트 배열의 클래스네임이 [I
가운데 @ 가 붙고
그 뒤 해시코드가 붙어서 [I@5d22bbb7 가 된 것이고 마지막에 ] 가 붙을 이유가 없습니다.
댓글 입력