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
Thanks
About Question enthuware.ocajp.i.v7.2.1404 :
Moderator: admin
-
- Posts: 29
- Joined: Sun Apr 05, 2015 11:26 am
- Contact:
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1404 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1404 :
Can we imagine
to be ike this
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;
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1404 :
yes, that is a very good way to represent a multi dimensional array.
If you like our products and services, please help us by posting your review here.
-
- Posts: 23
- Joined: Fri Mar 26, 2021 7:25 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1404 :
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?
But why a[0]=new int[2] is valid in option 4?
Does it mean null = new int[2] is valid?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1404 :
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
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 56 guests