About Question enthuware.ocajp.i.v7.2.1286 :
Moderator: admin
-
- Posts: 2
- Joined: Fri Sep 19, 2014 4:07 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1286 :
I simply don't understand this one: how come int[] array1, array2[] makes array2[] an array of arrays?
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1286 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 13
- Joined: Mon Jun 16, 2014 1:07 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1286 :
To make sure I understand this one,
this:
is essentially the same as:
or more along the lines of Java coding convention as:
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
this:
Code: Select all
int[] array1, array2[];
Code: Select all
int[] array1;
int[] array2[];
Code: Select all
int[] array1;
int[][] array2;
So String[] array1[][], array2[], array3 translates to:
String[] array1[][]; // three dimensional array
String[] array2[]; // two dimensional array
String[] array3; // one dimensional array
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1286 :
That is correct
If you like our products and services, please help us by posting your review here.
-
- Posts: 66
- Joined: Mon Feb 20, 2017 12:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1286 :
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!!
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!!
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1286 :
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!
Array of ints is neither an int nor an Integer!
If you like our products and services, please help us by posting your review here.
-
- Posts: 125
- Joined: Thu Jul 05, 2018 6:44 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1286 :
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.Javier wrote: ↑Thu May 10, 2018 10:37 amHi 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!!
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
-
- Posts: 3
- Joined: Mon Aug 27, 2018 2:15 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1286 :
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:
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
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1286 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Mon Aug 27, 2018 2:15 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1286 :
I understand your explanation and agree, thanks
-
- Posts: 1
- Joined: Tue Sep 18, 2018 6:42 am
- Contact:
Who is online
Users browsing this forum: Google [Bot] and 112 guests