How java arrays work

Can someone explain to me how arrays really work in Java.

I was surprised by the following code:

        Object test = new Object[2][2];
        Object test2 = new Object[] {
                new Object[2],new Object[2]
        };
        Object test3 = new Object[2][];
        ((Object[])test3)[0] = new Object[2];
        ((Object[])test3)[1] = new Object[2];
        System.out.println(test instanceof Object[]);
        System.out.println(test instanceof Object[][]);
        System.out.println(test2 instanceof Object[]);
        System.out.println(test2 instanceof Object[][]);
        System.out.println(test3 instanceof Object[]);
        System.out.println(test3 instanceof Object[][]);

only test2 is not an instance of Object [] []

What is the difference at runtime?

Edit: I see some answers. John Skeet, please note that I can do:

Object[] test4 = (Object [])test;
test4[0] = "blaaa";
test4[1] = "toto";
System.out.println(test4);

test instanceof Object [] returns true, and an exception does not occur at runtime during translation. According to Sierra and Bates SCJP book, check the IS-A object [] [], as well as the object []

But when I try to reassign the new value using "test4 [0] =" blaaa ";", I get an exception: An exception in the stream "main" java.lang.ArrayStoreException: java.lang.String to Main.main (Main.java: 24)

, , , test, test2 IS-A Object [], , IS-A Object [] []

+3
6

test2 . Object[] - . , :

// Valid
Object[] foo = (Object[]) test2;
foo[0] = "hello";

test:

// Invalid - test isn't just an Object[], it an Object[][]
Object[] foo = (Object[]) test;
test[0] = "hello";

, test, Object[], Object. "", Object[], .

test Object[] , String[] Object[], , , . , .

+8

Test2 . , , , . .

+12

, ...

SCJP . generics, . (Generics vs Arrays) , .

, .

- " ". . , String Set

public static void main(String [] args) {
    Set<Integer> set = new HashSet<Integer>();
    set.add(1);
    set.add(2);
    addString(set,"test");
    for ( Object o : (Set)set ) {
        System.out.println(o);
    }
    for ( Object o : set ) {
        System.out.println(o);
    }
}

public static void addString(Set set,String s) {
    set.add(s);
}

:

1
2
test
1
2
ClassCastException

http://www.ideone.com/nOSQz

, ( ) , Set<Integer>, , String Integer...

( , ), , , ( ), .

- , , , , , , ...

generics , List<Dog> List<Animal> method(List<Animal>) List<Dog> param, , Cat List<Dog>, List<Animal>...


.

, , Jon Skeet sais, .

, [] [], .

, Java Cats Dog. - . , , , , , java... , , , .

JVM , , .


,

Object test = new Object[2][2];
Object[] test2 = (Object [])test;
test2[0] = "blaaa";
test2[1] = "toto";
System.out.println(test2);

test (2D-) test2 (1D-), 2D-, , , 1D- A1, , 1D- A2, .

ArrayStoreException , A1 (, test2) String, , , Object, Object []


:

- 1D 2D-, 1D-, :

:

Dog[] dogs = new Dog[1];
dogs[0] = new Dog();
Animal[] animals = (Animal [])dogs;
animals[1] = new Cat();

4- . .

Set<Dog> dogs = new HashSet<Dog>();
dogs.add( new Dog() );
Set<Animal> animals = (Set<Animal>) dogs;
animals.add( new Cat() );

- . , , .

+2

, test2, [].

Instanceof , test2, .

The contents of the array at run time are Object [] s, which can fit into Object [], because Object [] s are objects.

+1
source

test2- this is what you can put in anyone Object, because it was created through new Object[]. You can only put Object[]in testand test3, because they were created with the help of a building a designer: new Object[][].

0
source

You determined test2how

Object test2 = new Object[];     // This is a plain array of Objects.
0
source

All Articles