• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Print all Elements of a multi dimensional Array

 
Greenhorn
Posts: 6
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Whizlabs OCA 8 mock excams are Questions like:

What should be inserted to print all Elements of arr?

The correct Answer is something like :

in my opinion this is not correct, because the elements of arr, are int[] not ints.

So the Answer should be:


What do you think and do you have experiences with similar questions in the real Excam and what the correct answer would be?
 
Marshal
Posts: 70667
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Marco Ulrich wrote:. . . the elements of arr, are int[] not ints. . . .

Afraid you are mistaken: you are using the int[] in line 2 to find the individual numbers, so that code is correct.

. . .

Since arrays don't override Object#toString(), that code won't print the elements at all. I think you shouldn't sit the exam until you have more experience.

In real life, use this method.
 
Marco Ulrich
Greenhorn
Posts: 6
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Marco Ulrich wrote:. . . the elements of arr, are int[] not ints. . . .

Afraid you are mistaken: you are using the int[] in line 2 to find the individual numbers, so that code is correct.

. . .

Since arrays don't override Object#toString(), that code won't print the elements at all. I think you shouldn't sit the exam until you have more experience.

In real life, use this method.



I think you got me wrong. I know that Arraay don`t ovverride the toString Method. Anyway the Java Specification says that the toString Method returns a String Represantation of the Object wich is Classname@hashcode. There are also these Questions with Arraylists wich inherits toString from AbstractCollection.
My point is in my opinion an Array of int arrays holds int array ,not int values. So the elements of int[][] arr, are Arrays that holds int's.
I know how to get all the numbers of the inner Arrays. But I want to know what, according to oracle, is the correct definition of an element in an array.
I don't think I'm not ready for the OCA, but if you think you should blame me for this question, feel free to do it again


Edit:
Maybe you should read the Documentation that you are sharring.
Arrays deepToString


[...]
Elements are converted to strings as by String.valueOf(Object), unless they are themselves arrays.
If an element e is an array of a primitive type, it is converted to a string as by invoking the appropriate overloading of Arrays.toString(e). If an element e is an array of a reference type, it is converted to a string as by invoking this method recursively.
[...]



So the elements of multidimensional Arrays are Arrays


 
Campbell Ritchie
Marshal
Posts: 70667
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for causing any annoyance; maybe the question wasn't clear about what they wanted. Was your suggestion given as a possible solution?
I would have interpreted the question as meaning to print the individual numbers. That appears to match what it says in the Java┬« Language Specification (=JLS), where the element type of an int[][] would be regarded as int. That link calls the int[]s the components of the array. It says that element types are not array types.
 
My cellmate was this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic