This week's book giveaway is in the NodeJS forum.We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
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
• Bear Bibeault
• Jeanne Boyarsky
• paul wheaton
Sheriffs:
• Junilu Lacar
• Paul Clapham
• Knute Snortum
Saloon Keepers:
• Stephan van Hulst
• Ron McLeod
• Tim Moores
• salvin francis
• Carey Brown
Bartenders:
• Tim Holloway
• Frits Walraven
• Vijitha Kumara

# what is this line doing in a multi dimensional array?

Ranch Hand
Posts: 424
3

hi guys

not understanding the logic in this line

table[t][i] = (t * 4) + i + 1;

finding it pretty confusing can somebody just break down whats going on in the two loops please

Rancher
Posts: 3121
110
hi Jon.

the line 'table[t][i] = (t * 4) + i + 1;' is setting table element [t][i] to the value (t * 4) + i + 1.

To make that a little bit better visible, change 'System.out.println(table[t][i] + " ");' to 'System.out.print(table[t][i] + " ");'

Saloon Keeper
Posts: 9989
207
Seeing as t is the outer loop variable, and it is multiplied by the number of times the inner loop iterates per iteration of the outer loop, it's a clear sign that this expression calculates a unique index per cell of the array. In this particular case, it creates an array that looks like this:

 1 2 3 4 5 6 7 8 9 10 11 12

It's poor form to use magic values though. This code is easier to understand and maintain:

Rancher
Posts: 3353
33
For a 2 dim array I prefer the variable names to be row and column vs y and x.

Stephan van Hulst
Saloon Keeper
Posts: 9989
207
I'd agree if you called them rowIndex and columnIndex. They're not rows or columns, but indices.

Marshal
Posts: 6634
443

Stephan van Hulst wrote:

I got some nitpicks from my perspective. I most likely would swap the parameters to have them in a habitual order how Java creates an array underneath in order not to breach expectations.

(y * width) + x + 1

I was thinking about the flexibility of this, but if the method was to fill only given array with 1-based indices, probably much simpler to understand would be by using counter and incrementing it by 1. Might seem to be a dumb, but much easier to understand. I think at least OP would have understood it himself that way.

Disadvantage: it couldn't initialize an element by passing an array and specifying its position to fill.
Advantage: simpler to understand, it would work with jagged arrays. But these wouldn't be 2D anymore.

Note: not sure yet what I'd stick with.

TwoDimensionalIntArray.fillWithOneBasedIndices()

This method name to me is misleading a bit. When I think about the "2D" arrays and their elements indices, in my mind I have 2 indices allocated per element, this is in fact what it is. So by reading this method name, I was thinking, how that is even possible? This method name would make sense for "int[]" type of array, though.

My reasoning based on 3rd point is, that "one based indices" simply are indices which start from 1 instead of 0. So the second row's first element I'd expect to get value (2, 1), but not 5. But again - not possible without having an ability to initialize with pair.

But I understand it was just a demo, so nitpicks probably not much valid.

Stephan van Hulst
Saloon Keeper
Posts: 9989
207
• 1

Liutauras Vilda wrote:I most likely would swap the parameters to have them in a habitual order how Java creates an array underneath in order not to breach expectations.

I always do it this way, a habit I learned over the years because many operations are MUCH easier to implement if your table is an array of rows instead of an array of columns. Especially printing is a b**** when your table is an array of columns. Besides, the order is an implementation detail, which is abstracted away if your class provides a method get(int columnIndex, int rowIndex).

probably much simpler to understand would be by using counter and incrementing it by 1.

Agreed.

When I think about the "2D" arrays and their elements indices, in my mind I have 2 indices allocated per element. So by reading this method name, I was thinking, how that is even possible? This method name would make sense for "int[]" type of array, though.

Semi-agree. Anything that is well-ordered can be indexed with an arbitrary number of indices. For instance, blocks of data from a hard drive can be indexed per block, or per (cylinder, head, sector). But I take your point that it might not be immediately obvious for something that has "TwoDimensional" in its name.

Maybe fillWithOneDimensionalOneBasedIndices(). It's a bit on the verbose side, but then again it's also a REALLY specific method to have.

 crispy bacon. crispy tiny ad: global solutions you can do at home or in your backyard https://www.kickstarter.com/projects/paulwheaton/better-world-boo