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

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
JohnnyFanta

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

Post by JohnnyFanta »

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

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

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.

Guest

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

Post by Guest »

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? :?

lordnovas
Posts: 12
Joined: Tue Jan 15, 2013 3:31 pm
Contact:

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

Post by lordnovas »

Question: If array2[] is an Array of Arrays of Type int, does that make it a 2D Array?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

lordnovas wrote:Question: If array2[] is an Array of Arrays of Type int, does that make it a 2D Array?
Yes.

lordnovas
Posts: 12
Joined: Tue Jan 15, 2013 3:31 pm
Contact:

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

Post by lordnovas »

Awesome, thanks a bunch.

Crashtest
Posts: 18
Joined: Fri May 31, 2013 1:18 pm
Contact:

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

Post by Crashtest »

To make it crystal clear I broken it into:
int[] array1, array2[];
is the same as
int[] array1;
int[] array2[]; // which is the same as int[][] array2;

Now the question does look easy 8-)

ewebxml
Posts: 77
Joined: Sun Jun 30, 2013 10:04 pm
Contact:

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

Post by ewebxml »

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.

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

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

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.

1stTiger
Posts: 3
Joined: Thu Jan 23, 2014 6:19 pm
Contact:

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

Post by 1stTiger »

The last answer can't be valid since it doesn't have a semicolon. "array5 = array3"

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

1stTiger wrote:The last answer can't be valid since it doesn't have a semicolon. "array5 = array3"
You are right. Fixed.
thank you for your feedback!

Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests