java Beginner)Arrays

Asked 2 years ago, Updated 2 years ago, 19 views

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@5d22bb7 is...] At the end, why ] I'm curious if the symbol didn't come out.

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);
    }
}

java

2022-09-20 18:02

1 Answers

System.out.println(Arrays.toString(iArr2));
System.out.println(Arrays.toString(iArr3));

The Arrays.toString() function in this code attaches the [, ] bracket to the beginning and end of the array element.

However, when you output System.out.println(iArr3) iArr3, it is the toString() method of the object, not the element of the array.

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.

Oracle

Class name for the int array is [I

]

With a middle @

There's no reason to add [I@5d22bbb7 at the end] because of the hash code.


2022-09-20 18:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.