About Question enthuware.ocajp.i.v7.2.1286 :
Moderators: Site Manager, fjwalraven
About Question enthuware.ocajp.i.v7.2.1286 :
Can someone explain this question in more detail for me I'm a bit confused with the explanation given.
There is a subtle difference between: int[] i; and int i[]; although in both the cases, i is an array of integers.
The difference is if you declare multiple variables in the same statement such as: int[] i, j; and int i[], j;, j is not of the same type in the two cases.
In the first case, j is an array of integers while in the second case, j is just an integer.
Therefore, in this question: array1 is an array of int array2, array3, array4, and array5 are arrays of int arrays Therefore, option 1, 2 and 5 are valid.
Thanks
There is a subtle difference between: int[] i; and int i[]; although in both the cases, i is an array of integers.
The difference is if you declare multiple variables in the same statement such as: int[] i, j; and int i[], j;, j is not of the same type in the two cases.
In the first case, j is an array of integers while in the second case, j is just an integer.
Therefore, in this question: array1 is an array of int array2, array3, array4, and array5 are arrays of int arrays Therefore, option 1, 2 and 5 are valid.
Thanks
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1286 :
The first thing is that for an assignment to work, the object on the right hand side (of = sign) that you are trying to assign to the variable on the left hand side should be compatible with the type of the variable.
For example, String s = new Integer(19); will not work because the type of the variable s is String but the object is Integer.
Similarly, in case of array variables, if the variable is of type "array of int", then on the right hand side you must have an array of int. An "array of array of int" is not compatible with an "array of int".
So, in this question, all you need to do is to first identify the type of the variable and make sure that the object that you are trying to assign to this variable is of the same type.
array1 = array2; is not valid because array1 is of type "array of int", while array2 is of type "array of array of int", which are not compatible with each other.
If you go through the explanation again and work out the other option now, I think you will get it.
Please let me know if you still have any confusion.
HTH,
Paul.
For example, String s = new Integer(19); will not work because the type of the variable s is String but the object is Integer.
Similarly, in case of array variables, if the variable is of type "array of int", then on the right hand side you must have an array of int. An "array of array of int" is not compatible with an "array of int".
So, in this question, all you need to do is to first identify the type of the variable and make sure that the object that you are trying to assign to this variable is of the same type.
array1 = array2; is not valid because array1 is of type "array of int", while array2 is of type "array of array of int", which are not compatible with each other.
If you go through the explanation again and work out the other option now, I think you will get it.
Please let me know if you still have any confusion.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1286 :
So this just means
You can put an int[] of arrays into an array but can't put an array into an int[] of arrays?
You can put an int[] of arrays into an array but can't put an array into an int[] of arrays?

-
- Posts: 12
- Joined: Tue Jan 15, 2013 3:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1286 :
Question: If array2[] is an Array of Arrays of Type int, does that make it a 2D Array?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1286 :
Yes.lordnovas wrote:Question: If array2[] is an Array of Arrays of Type int, does that make it a 2D Array?
-
- Posts: 12
- Joined: Tue Jan 15, 2013 3:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1286 :
Awesome, thanks a bunch.
-
- Posts: 18
- Joined: Fri May 31, 2013 1:18 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1286 :
To make it crystal clear I broken it into:
int[] array1;
int[] array2[]; // which is the same as int[][] array2;
Now the question does look easy
is the same asint[] array1, array2[];
int[] array1;
int[] array2[]; // which is the same as int[][] array2;
Now the question does look easy

-
- Posts: 77
- Joined: Sun Jun 30, 2013 10:04 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1286 :
Can someone explain why
the assignment statements given in the problem work
"only if" you place the declarations of the arrays at the Class level and the assignments within main.
If you place the code given in the problem
1) At the class level or
2) within main
it will not compile.
the assignment statements given in the problem work
"only if" you place the declarations of the arrays at the Class level and the assignments within main.
If you place the code given in the problem
1) At the class level or
2) within main
it will not compile.
Code: Select all
// The following code compiles
public class ArrayTest1 {
static int[] array1, array2[];
static int[][] array3;
static int[] array4[], array5[];
public static void main(String[] args) {
array2 = array3;
array2 = array4;
//array1 = array2;
//array4 = array1;
array5 = array3;
}
}
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1286 :
That's how the grammer of the language is designed. Anything outside the methods has to be either a declaration or a static or instance bloc. A declaration may include an initialization part, which can be a call to a method as well. So if you put something like array2 = array3; outside any method, it is neither a declaration and neither a static or instance code block. So it will not compile.
-Paul.
-Paul.
-
- Posts: 3
- Joined: Thu Jan 23, 2014 6:19 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1286 :
The last answer can't be valid since it doesn't have a semicolon. "array5 = array3"
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1286 :
You are right. Fixed.1stTiger wrote:The last answer can't be valid since it doesn't have a semicolon. "array5 = array3"
thank you for your feedback!
Who is online
Users browsing this forum: No registered users and 5 guests