Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

what is this line doing in a multi dimensional array?  RSS feed

 
Ranch Hand
Posts: 339
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
Master Rancher
Posts: 2695
91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 8975
168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 9101112

It's poor form to use magic values though. This code is easier to understand and maintain:
 
Rancher
Posts: 3137
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 8975
168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd agree if you called them rowIndex and columnIndex. They're not rows or columns, but indices.
 
Marshal
Posts: 5797
401
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 8975
168
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!