• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Review Question about Multidimensional Array

 
Ranch Hand
Posts: 221
27
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question about multidimensional-array, I don't want to create other topic for it, so I write here.
Review questions, #15 of chapter 3, answer B:
Zero confuse me. Can you give any example for this?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

That other topic ...

https://coderanch.com/t/648176/ocajp/certification/multidimensional-array

May still be active (not sure) ... but ... regardless, I am spinning your post off to a new topic, just in case.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mushfiq Mammadov wrote:Zero confuse me. Can you give any example for this?



Java supports arrays of various sizes, including arrays that take zero elements. Can you elaborate a bit on what you are confused about?

Henry
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mushfiq Mammadov wrote:Can you give any example for this?


Nothing to be confused about: zero just means it's an empty array So the array has zero elements. Let's print the length for each possible (sub)array:In the 3rd line, we try to access the 1st element of an empty array and this throws of course a runtime exception (just like when you execute list.get(0) on an empty list).

In real world it would probably not be used that often (if at all). You could use the zero-length array if you need an array with small memory footprint. With a 1D-array it's regularly done in this use case:So if you have a list of strings and you need an array of String (instead of Object) you could use the overloaded version of the toArray() method and pass a zero-length String array.

With multidimensional arrays I never have seen this approach. But you could use it when the length of the second (and third) dimension is unknown when you create the array. For example:But in these scenarios this variant of creating a multidimensional array is (almost) always used:Notice that new Object[3][0][0] and new Object[3][][] are not the same/equivalent with each other. To illustrate, let's print the length of each possible (sub)array:So now you'll get a NullPointerException, because with this declaration you only created a 3D-array with 3 elements. But no subarrays were created! Total number of objects created: just 1. That's why you'll get the NullPointerException at runtime. With new Object[3][0][0] you created a 3D-array with 3 elements as well. But each element (which is in fact a reference variable to a 2D-array) refers to a 2D-array with 0 elements. Total number of objects created: 4 (1 3D-array and 3 2D-arrays).

Hope it helps!
Kind regards,
Roel
 
Mushfiq Mammadov
Ranch Hand
Posts: 221
27
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote: Java supports arrays of various sizes, including arrays that take zero elements. Can you elaborate a bit on what you are confused about?


For example, if zero would be 1, everything is clear i.e.
or


But if second dimension is zero I can't draw it, third dimension ([5]) is confused.


Roel De Nijs wrote: Nothing to be confused about: zero just means it's an empty array So the array has zero elements.


Thanks a lot, Roel! After I read your explanation I understand that if second dimension is zero, third dimension (in this [5]) is useless and it does not matter third dimension is 5 or 7 or so on. Do I think right?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mushfiq Mammadov wrote:After I read your explanation I understand that if second dimension is zero, third dimension (in this [5]) is useless and it does not matter third dimension is 5 or 7 or so on. Do I think right?



Correct. Since the second "dimension" doesn't have any elements, it doesn't really matter what the final array "dimension" value is -- as no element arrays will be created.

Henry
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mushfiq Mammadov wrote:After I read your explanation I understand that if second dimension is zero, third dimension (in this [5]) is useless and it does not matter third dimension is 5 or 7 or so on. Do I think right?


Yes, you are spot-on!

And you could create an empty array using curly braces as well:So if you see it in the initialisation part of a (multidimensional) array, you know how to handle it. E.g.Maybe you should try making a drawing of arr2? And don't forget: there's a difference between null and {}.

Hope it helps!
Kind regards,
Roel
 
Mushfiq Mammadov
Ranch Hand
Posts: 221
27
IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote: Correct. Since the second "dimension" doesn't have any elements, it doesn't really matter what the final array "dimension" value is -- as no element arrays will be created.


Thanks, Henry


Roel De Nijs wrote:So if you see it in the initialisation part of a (multidimensional) array, you know how to handle it. E.g.Maybe you should try making a drawing of arr2? And don't forget: there's a difference between null and {}.


It is a very interesting example for me which can help me to understand unclear part of multidimensional array. I can try to draw it, but I have some questions.



At first, I am confused about dimension of array. We declare threedimensional array, but initialize fourdimensional or I understand wrong. The second doubt is difference between null and {}. I understand such arr2[1][0] = null i.e.

If we try to print array elements it throws NullPointerException because we call length method of null array.


 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mushfiq Mammadov wrote:It is a very interesting example for me which can help me to understand unclear part of multidimensional array. I can try to draw it, but I have some questions.


Your drawing is spot-on! Just one little nitpicky remark: String is an object, so the arrays at the bottom of your drawing don't contain strings but String reference variables. So to be 100% correct the array element should not contain the String value, but be an arrow which refers to the actual String value (unless it's null). But you probably have just drawn the shortcut version

Mushfiq Mammadov wrote:At first, I am confused about dimension of array. We declare threedimensional array, but initialize fourdimensional or I understand wrong.


You declare indeed a 3D-array. That's correct! But why would you think a 4D-array was initialized? If that's the case, your code would not have compiled, because dimensions must really match. You just initialized a 3D-array with 4 elements and I guess that's probably the source of your confusion Here's an actual 4D-array assigned to a 3D-array reference variable (which does NOT compile):And there is an easy thumb of rule to determine the dimension of an array: in the declaration just count the occurences of []. For the initialisation part of an array, the dimension is equal to the maximum number of open (or closed) curly braces.

First an example of the declaration part: I count 6 pairs of square brackets, so array is a 6D-array.

Secondly an example of the initialisation part: The o indicates the opening curly brace and the c the closing curly brace. The maximum number you encounter is 4, so this is a 4D-array.


Mushfiq Mammadov wrote:The second doubt is difference between null and {}. I understand such arr2[1][0] = null i.e.

If we try to print array elements it throws NullPointerException because we call length method of null array.


Yes! You are correct. If your array (no matter which dimension) is null, you can't invoke any method nor access any property (like length). If you try to do so, you'll get a NullPointerException. That's exactly the same as trying to invoke the length() method on a null String reference variable, e.g.And to have an exact parallel, invoking the length() method on an empty String returns the same value as accessing the length property on an empty array.
So in order to print the array arr2 without any NullPointerException exceptions you have to add a null check in your code

Hope it helps!
Kind regards,
Roel
 
Mushfiq Mammadov
Ranch Hand
Posts: 221
27
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote: You just initialized a 3D-array with 4 elements and I guess that's probably the source of your confusion


You are absolutely right, Roel I am confused this array with above array (Object[][][] cubbies = new Object[3][0][5];) After your following example

Roel De Nijs wrote:


everything was clear for me.

You explain everything perfectly, thanks a lot!
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad to hear I could help and from now on you'll nail every question about multidimensional arrays
 
Ranch Hand
Posts: 386
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thought i'd bump this, because of the drawings () and I really rate the counting of the brackets method to check if the initialisation matches the declared dimensions. has helped a lot.

bit of an aside - in enthuware i highlight each group of brackets with my mouse, helping me to keep track of each array - can you remember Roel if the official software lets you highlight parts of the code?

nick
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nick woodward wrote:can you remember Roel if the official software lets you highlight parts of the code?


Honestly I can't remember I have ever tried to highlight any parts of the code, so I can't help you on this one. But maybe someone else tried it and can let us know if it's possible or not.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can change colours of text but only in normal text. The code tags support syntax colouring, but the colours are fixed for a particular language. You can change languages with the dropdown list, but I don't know of any way to highlight code or text. You can copy and paste the code to a decent text editor (not MS' Notepad) and you get bracket matching options as you describe on most of them.

I don't know of any other highlighting options.

Maybe I was away when this thread first came up because I can see no sign of a post of mine telling people off for saying there are such things as multidimensional arrays. They are of course arrays of arrays.
 
nick woodward
Ranch Hand
Posts: 386
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure?

In the exam, right?

Nick
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You can change colours of text but only in normal text. The code tags support syntax colouring, but the colours are fixed for a particular language. You can change languages with the dropdown list, but I don't know of any way to highlight code or text. You can copy and paste the code to a decent text editor (not MS' Notepad) and you get bracket matching options as you describe on most of them.


I am pretty sure nick was asking his question on "highlighting code" about the certification exam software and not about the CodeRanch forums And unfortunately during your certification exam you can't use any other tools than the exam software, so copy/paste to a decent text editor will not be possible.

Campbell Ritchie wrote:Maybe I was away when this thread first came up because I can see no sign of a post of mine telling people off for saying there are such things as multidimensional arrays. They are of course arrays of arrays.


Of course! But Oracle's certification exam objectives use "multi-dimensional arrays" as well, so it's ok to use this term here as well. And if we are really nitpicking, then "arrays of arrays" is also incorrect, it should be "arrays of reference variables to other arrays" (as explained in this and following posts). But that's way too long to type each time, so we use "arrays of arrays", "multi-dimensional arrays" or (if I'm really lazy) "nD arrays".
 
reply
    Bookmark Topic Watch Topic
  • New Topic