Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1286 :

Posted: Fri Sep 19, 2014 4:12 am
by nikolaj.mattrup
I simply don't understand this one: how come int[] array1, array2[] makes array2[] an array of arrays?

Re: About Question enthuware.ocajp.i.v7.2.1286 :

Posted: Fri Sep 19, 2014 10:29 am
by admin
Think of it this way, the square brackets attached to int apply on all the variables declared on that line. But the square brackets attached to the variable name apply only on that variable. So array1 has only one set of [] applied to it but array2 has two. Thus, array1 is an array of one dimension but array2 is of two dimensions i.e. array or arrays.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1286 :

Posted: Tue Apr 07, 2015 11:28 am
by Brian B
To make sure I understand this one,

this:

Code: Select all

int[] array1, array2[];
is essentially the same as:

Code: Select all

int[] array1;
int[] array2[];
or more along the lines of Java coding convention as:

Code: Select all

int[] array1;
int[][] array2;
So in order to keep them straight, you could just take lines that include multiple declarations, keep the declaration type (e.g. String[]), and re-write them (mentally or on scratch paper as: (declaration type) (reference variable); on separate lines?

So String[] array1[][], array2[], array3 translates to:

String[] array1[][]; // three dimensional array
String[] array2[]; // two dimensional array
String[] array3; // one dimensional array

Re: About Question enthuware.ocajp.i.v7.2.1286 :

Posted: Tue Apr 07, 2015 8:34 pm
by admin
That is correct :)

Re: About Question enthuware.ocajp.i.v7.2.1286 :

Posted: Thu May 10, 2018 10:37 am
by Javier
Hi Enthuware!!

I have a doubt with Arrays mixed with boxing/unboxing, I hope you can help me.

Why is not possible to convert:

Integer[] i= new int[2];
int[] ii= new Integer[2];

I have been searching in google but I couldn´t find the answer :(

Thank you for you help!!

Re: About Question enthuware.ocajp.i.v7.2.1286 :

Posted: Thu May 10, 2018 12:03 pm
by admin
You need to read about arrays from a good book. Boxing/unboxing happenss between int and Integer.
Array of ints is neither an int nor an Integer!

Re: About Question enthuware.ocajp.i.v7.2.1286 :

Posted: Thu Jul 05, 2018 6:56 pm
by __JJ__
Javier wrote:
Thu May 10, 2018 10:37 am
Hi Enthuware!!

I have a doubt with Arrays mixed with boxing/unboxing, I hope you can help me.

Why is not possible to convert:

Integer[] i= new int[2];
int[] ii= new Integer[2];

I have been searching in google but I couldn´t find the answer :(

Thank you for you help!!
Sorry, it's a bit late, but you just have to remember, int[] IS NOT A Integer[] and vice versa (of course). I think it hardly matters why.
You can though do

Code: Select all

        Object[] oa  = new Object[1];
        Integer[] ia = new Integer[4]; 
        oa=ia; //assign an Integer[] to an Object[] ie subclass array to superclass array
        oa[3]=99;  //note the index
        //oa[3]++; //Compilation error: oa is still a reference of type Object[] even though "we know" it points to an Integer[]
        ((Integer[])oa)[3]++; //OK - explicit cast of oa to Integer[] before indexing into it
        System.out.println(oa[3]); //100

Re: About Question enthuware.ocajp.i.v7.2.1286 :

Posted: Wed Aug 29, 2018 8:09 am
by sebaskoot
Hi admin,
In the answers I read array2 = array3; this confuses me since = is the assignment operator.
My suggestion: == would be better than =, or length would even be better.
Would you please consider changing the answers?
Thanks ,
Sebastiaan

NOTE:

Code: Select all

array2=new int[][]{};//initialisation needs to happen first
array3=new int[][]{};
		
System.out.println((array2.length == array3.length));//prints true

Re: About Question enthuware.ocajp.i.v7.2.1286 :

Posted: Wed Aug 29, 2018 8:24 am
by admin
Hello,
The problem statement asks you to identify the valid statements. All the options are potentially valid statement out of which 1, 2, and 5 are valid statements that assign array pointed to by one variable to another (that is why the assignment operator is used in these statements. There is no need to change them because you can expect such statements in the exam.

Re: About Question enthuware.ocajp.i.v7.2.1286 :

Posted: Wed Aug 29, 2018 8:52 am
by sebaskoot
I understand your explanation and agree, thanks

Re: About Question enthuware.ocajp.i.v7.2.1286 :

Posted: Tue Sep 18, 2018 6:43 am
by ibrahimovic
sebaskoot wrote:
Wed Aug 29, 2018 8:52 am
I understand your explanation V Tight Gel here and agree, thanks
same here :)