• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

arrays

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
found this problem at two diferent places and with different problem.kindly suggest which explanation is correct:

Test1[] t1=new Test1[10];
Test1[][] t2=new Test1[10][];

t2[0]=new Test1[5];
System.out.println(t1[0]);
System.out.println(t2[1][0]);

according to me this should complile and give no runtime error as well as give o/p as null.
The left most dimention of t2 is required acc to that it shud have compiled.


but as we hav t2[0]=new Test1[5] line, so does that means any element of t2[0] is accessed ,accessing other element swill giv an error.???



please expain.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by anshi kohli:
found this problem at two diferent places and with different problem.kindly suggest which explanation is correct:

Test1[] t1=new Test1[10];
Test1[][] t2=new Test1[10][];

t2[0]=new Test1[5];
System.out.println(t1[0]);
System.out.println(t2[1][0]);

according to me this should complile and give no runtime error as well as give o/p as null.
The left most dimention of t2 is required acc to that it shud have compiled.


but as we hav t2[0]=new Test1[5] line, so does that means any element of t2[0] is accessed ,accessing other element swill giv an error.???



please expain.



In your example, t2 is a reference to an array. Each element in that array is a reference to an array of Test1 objects.

Since an array is an object, initially each element of t2 is null.

You assign t2[0] to point to an array of Test1 objects.

However, since you don't assign t2[1] to point to an actual array, you will get a NullPointerException if you try to access an element in t2[1].
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is an array of objects and objects get NULL as default value.
please have a look at following,

Test [] test = new Test[10] // no problemo here in accesing test[0]-test[9]
Test [] [] test1 = new Test[10][]; // problemo accesing test[0][1] until you
test1[0] = new Test[10]

infact no compile-time error but it will give you NullPointerException at Runtime because compiler knows declaration but does not know that it has initialized or not.
 
anshi kohli
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why cant it get a default value that is null ,instead of throwing exception as already initialized???
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Test1[] t1=new Test1[10];
Test1[][] t2=new Test1[10][];

t2[0]=new Test1[5];
System.out.println(t1[0]); // OK as t2[0] is initialized with a object.
System.out.println(t2[1][0]); //Not OK as t2[2] is not initialized.
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well!!have some doubt

i got the point that here in the below shown code
t2 is the object of Array Test1

Test1[] t1=new Test1[10];
Test1[][] t2=new Test1[10][];

here t2 is two dimensional array obj and then it points to elements of Test1 as

t2[0]=new Test1[5]

how is this possible?i am not clear with concept
does it mean that t2[0] is equivalent to t2[0][0]

and later it is explained as

t2[1][0] not initialised .......

i am getting more confused...

please if anyone could ellaborate and explain it will be kinda of you........Thanks in advance
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java, arrays are objects. But there is NOT array of objects but array of object references. Confusing?

Lets say, you have a class named Sample and you have a statement called



That means, there are NO 10 sample objects. There are ten references to sample objects (each of which could point to Sample object or any of its subclasses).

In this case, once the statement is executed, there is ONLY ONE object created in heap. Thats nothing but the array reference "s" object. It does hold room or space for 10 new sample object references, which are not existing yet. The 10 references will get a default value of NULL.

You need to initialize the array individually as



This way you are creating 10 individual Sample objects are storing the object references to the Array "s".

When you look at 2D Arrays, you can declare as



Here you are declaring a 2D Array named "2DSampleArray" which can hold the references to 10 Sample Objects, each of which in turn is a 1D Array. Same as 1D Array, still there are NO Sample Objects existing in this case. You just have got a container or placeholder for the references to the Sample Objects, which are yet to be created.

When you say,



The first element of the 2D Array "2DSampleArray" can hold 2 references to the Sample Objects in Line 6; the second element of the 2DArray can hold 5 references to the Sample Objects;.. this way you say the dimesion of each elements of the 2D Array.

Still, there are NO Sample Objects Created!. They are again, yet to be created. What you have done is, came to one more level down which is upto the 1st index of the 2D Array and mentioned the size/space to be allocated to each 2D Array member.

Next and last step is you need to actually create Sample Objects and assign. This is same as 1D Array. Because, you have now come to the 1D Array level whereas the individual member of 2DArray itself is an 1D Array.

Now, you need to create the actual Sample Objects and assign the references to 2D Array dimensions, like



Now each of the elements of the first index/dimension of 2DArray is initialized. Means, 2DSampleArray[0] and 2DSampleArray[1] both are pointing to an Object in Heap.

How about 2DSampleArray[1]? We have declared that 2DSampleArray[1] can hold 5 references to Sample Objects in Line 7. But, its NOT yet assigned with any objects. So, when you try to access any of the members of 2DSampleArray[1] i.e, either 2DSampleArray[1][0] or 2DSampleArray[1][1] till 2DSampleArray[4] - as they don't have any object references and they contain NULL, you get a NullPointerException at runtime.

Hope this helps!!!
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Raghavan

for such a perfect explanation............
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome dhwani Mathur
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
going back to the question of T1 and T2
System.out.println(t1[0]); // OK as t2[0] is initialized with a object.
how t1 and t2 related they both are different objects then why above statement not giving any exception.
In question only t2[0] is initialised not t1.....
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there is a typo.
I think he/she want to say:
System.out.println(t1[0]); // OK as t1[0] is initialized with a object.

Murali...
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tashi Rautela:
In question only t2[0] is initialised not t1.....


t1 is initialized here:
Test1[] t1=new Test1[10];
 
They gave me pumpkin ice cream. It was not pumpkin pie ice cream. Wiping my tongue on this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic