This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Array Questions in OCA Book mc graw hill (cd)

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I saw this code in a question in the above CD Programm from the CD Book mac graw hill. Before I thought, I knew a lot of Arrays and Multi-Dimensional-Arrays. But to solve this question I had no clue what to do or what is right??!!



And the questions is:

Which of the following lines of code could be inserted at line 7 and still allow the code to compile?(Choose all that apply.)

A.   b2 [1] [1] = big;

B.   b [1] [0] = b3;

C.   b2 [0] [1] [1] = b;

D.   b2 [0] [2] [1] = b [1] [0];

E.   b2 [1] [1] [0] [1] = b [1] [0];

F.   b2 [1] [1] = b;


The correct answers are: A, B, E, F!

But I don't understand why I can assign a two dim Array to 4 dim Array. And why C and D are incorrect? Could somebody explain me step by step how this works? I never saw or read such example in a book!!

A. For example b has 4 dimensions b [2] [3] [2] [2] and big has two dim big [2] [2]. But what does it mean b2 [1] [1] = big (big = new short[2] [2]); that b2 is now b2 [2] [2] but b2 was initialized with new short [2][3][2][2]; that means after A we have short b2 [][][][] = new b2 [2] [2] [2] [2] are these the first two dimensions in b2 [2] [3] and in this case the size is enough? But in F we have 2 dims with short [][] b = new short [4][4]; on the right side and on the left side we have 4 dims with short b2 [][][][] = new short [2][3][2][2];??

Is C and D incorrect because on the left side we have 3 from 4 dims and the right side has only 2 dims?? Answer B I THINK I understand, the result will be: short [] [] b = {{{{0, 0, 0, 0}, {8, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}}} -----> ok?

Thanks a lot for your patience in explaining me this question!
 
Marshal
Posts: 65806
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Gualeni wrote:. . . I don't understand why I can assign a two dim Array to 4 dim Array. . . .

You aren't. What you are doing is assign an array with two dimensions (actually an array of arrays) to an element of an element of an array with four dimensions (actually an array of arrays of arrays of arrays).. The middle level has elements which are arrays of arrays, soRemember the /*[][]*/ part is a comment, just there to show you that the array with two []s will fit as an element of the left half of the assignment.

The fact that you can get that sort of code pasts the compiler doesn't mean that it will run successfully. My code will probably throw an exception at runtime.
 
Mike Gualeni
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks.  I only tried if the correct solutions in the code will compile and that's what it did! I didn't controlled if they run in runtime.

But I don't understand your example. In foo you have four square brackets. But in line 3 and 4 you have only two. Which two of the 4 are meant? Normally in a multi-dim-array the first number in square brackets indicates, how many array(s) are in this array, right?

For example,  solution F: b2 [1] [1] = b; in other writing is: b2 [1] [1] = b [4] [4]; that means if I have in the 4. array (beginning with 0= 5. array), the 4. number (only beginning with 0= 5. number) for example short "3", it will be after the statement in the 2. array, the 2. number in b2 or not?? The question for me is where is b2 [1] [1] assigned to "b2" because b2 is a 4 dim array b2 [2] [3] [2] [2]? Does it concern [2] [3] or [3] [2] or the last two [2] [2]?


If my array is initialized directly like: String [] [] square = {{"1","2"}, {"3", "4"}}; then it's possible to get the number of dimensions only by counting the beginning brackets {{, in this case 2 dim, ok? If I write String [] [] square = {{"1","2"}, {"3", "4"}};  it's the same as I would write in code:



The output would be:

[[1, 2], [3, 4]]


It is also the same if I would initialize directly with brackets or with square brackets with the amount in it?

Sorry, I hope you will understand my position and my complex writing!! Or my thoughts go too far or in the wrong direction?
 
Campbell Ritchie
Marshal
Posts: 65806
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Gualeni wrote:Many thanks.

That's a pleasure

. . . . In foo you have four square brackets. But in line 3 and 4 you have only two. Which two of the 4 are meant?

The idea of line 4 was to show you that the two [] on the left refer to the leftmost pairs and the two [] on the right refer to the right pair. When you put them together you get [][][][]. You can only get the code to compile with four []s.

Normally in a multi-dim-array the first number in square brackets indicates, how many array(s) are in this array, right?

Afraid not. Those numbers give the indices in the existing arrays. It only tells you how many elements there are if you write new Foo[3][4][5][6]

For example,  solution F: b2 [1] [1] = b; in other writing is: b2 [1] [1] = b [4] [4]; . . .

Afraid that isn't correct. If you have those indices as in the right statement, you are trying to assign an individual value where you need an array of arrays. That is a type mismatch and won't compile. The two expressions on the right of the assignment operators are different types.

. . . I hope you will understand my position . . .

Yes, i do, but I have to go somewhere now, so I must stop for a few hours.
 
Mike Gualeni
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevertheless thank you for explaining me, this for me confusing multi-dim-THINGS.

Now I hope I think, it seems a little bit clearer for me.

For example,  solution F: b2 [1] [1] = b; in other writing is: b2 [1] [1] = b [4] [4]; . . .



where you told that is a type mismatch and won't compile.
My conclusion in this case is the following: It's a type mismatch because b is a 2 Array-Dimension und b2 is a 4 Array-Dimension and this would be always a type mismatch as you wrote above. We can never assign more Arrays of Arrays to an Array type with fewer Arrays of Arrays Right?

Your commment to solution F was:

The idea of line 4 was to show you that the two [] on the left refer to the leftmost pairs and the two [] on the right refer to the right pair. When you put them together you get [][][][]. You can only get the code to compile with four []s.



That's because I love this forum. Specialists like you take time to explain Greenhorn like me. Very generous.

Sometimes there is just a button in the understanding that you have to untangle somehow!
 
Campbell Ritchie
Marshal
Posts: 65806
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What nice things to say; thank you.

Try this sort of thing:-If you can get that to work when I am writing on the back seat of a bus, boucning around, maybe that will help answer the original question.]
 
Mike Gualeni
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everything in this code is mismatch. No way to get this to work!!!
 
Campbell Ritchie
Marshal
Posts: 65806
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yesterday, I wrote:. . .. . . back seat of a bus, boucning around . . .

We seem to have hit a large bump when I was on line 1. The following edited version will work betterSorry about that. If you try it on JShell, you will get slightly different class names.
 
Mike Gualeni
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Eclipse IDE it says:

Foo cannot be resolved to a type!

But that's clear, Foo is the type of the array "array4"! And there is no class named Foo???!!!

I'll try it on JShell.

I'will be bouncing soon too!
 
Mike Gualeni
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JShell says:

Cannot find Symbol:

Symbol: class Foo

What do you do for making working your code?
 
Campbell Ritchie
Marshal
Posts: 65806
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a class Foo.
 
Mike Gualeni
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK got it. Right with class Foo:

Result is:

[[[[Loca.Foo;
[[Loca.Foo;
[[Loca.Foo;



because my package name ist oca.

It's right?

 
Campbell Ritchie
Marshal
Posts: 65806
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You tell me. I would expect that the second and third class names would be the same if the assignment will compile normally.
To work out why the answers C and D are wrong, look at the class names of the right and left halves of those assignment statements. Use Foo[][][][]s not int[][][][]s because you can't call getClass() on an int.
 
Everybody! Do the Funky Monkey! Like 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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!