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 답변

  • 좋아요

    0

    싫어요
    채택 취소하기
    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 가 된 것이고 마지막에 ] 가 붙을 이유가 없습니다.

    • 감사요 승현 2021.2.16 06:16

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)