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.
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:
It's poor form to use magic values though. This code is easier to understand and maintain:
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.
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
posted 9 months ago
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.
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