Page 1 of 1

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

Posted: Sun Apr 05, 2015 11:39 am
by ElizabethCM
Hello,

I have a question regarding the first available optional answer for this topic, i.e int[][] a = new int[2][];
After the review, checking the answers, it says the above option is not correct because:
"This will instantiate only the first dimension of the array. The elements in the second dimension will be null. In other words, a will be instantiated to two elements but a[0] and a[1] will be null and so a[0][0] (and access to all other such ints) will throw a NullPointerException."

First question: Why would it throw a NullPointerException and why a[0] and a[1] would be null since they're declared as "int"? Wouldn't they be 0 instead?

However, I have tried it in Eclipse as I was not convinced by the above:
The below code runs smoothly:

import java.util.Arrays;

public class StringExercise {

public static void main(String args[]) {

int[][] a = new int[2][];
a[0][0] = 1;
a[0][1] = 2;
a[1][0] = 3;
a[1][1] = 4;
a[1][2] = 5;
a[1][3] = 6;
}
}

Please correct me if this answer is not ok. According to me it should be right.
Please note that I ran this with the Java 8 installed. If this is the issue, please let me know if the above would be wrong in Java 7.
Any other explanations are welcomed by a beginner like me :D

Thanks

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

Posted: Sun Apr 05, 2015 9:46 pm
by admin
Did you try running the code that you've posted? It cannot run smoothly.
If the array is of ints, then all its elements will be initialized to 0. But you have to create the array first. a is a two dimensional array, so you have to create the second dimension also.

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

Posted: Fri Jul 06, 2018 12:29 pm
by flex567
Can we imagine

Code: Select all

int[][] a = new int[2][];
a[0] = new int[2];
a[1] = new int[4];

a[0][0] = 1;         
a[0][1] = 2;                   
a[1][0] = 3;         
a[1][1] = 4;         
a[1][2] = 5;         
a[1][3] = 6;
to be ike this

Image

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

Posted: Fri Jul 06, 2018 8:07 pm
by admin
yes, that is a very good way to represent a multi dimensional array.

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

Posted: Sun Jun 13, 2021 5:31 am
by baichen7788
It says a[0] and a[1] are null in option 1.
But why a[0]=new int[2] is valid in option 4?
Does it mean null = new int[2] is valid?

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

Posted: Sun Jun 13, 2021 7:52 am
by admin
baichen7788 wrote:
Sun Jun 13, 2021 5:31 am
Does it mean null = new int[2] is valid?
NOO!!! a[0] is a "reference". It initially points to null. Then it is made to point to an actual array by the statement a[0]=new int[2]. That doesn't mean null = = new int[2] at all!

References and objects are something very basic in Java. If you are not clear about this, I strongly suggest you to go through a book before attempting mock exams.
At least checkout "Kickstarter for Beginners" chapter of Deshmukh's book: https://amzn.to/2PucBeT