-5

Is it possible to explain multiTab in Java ? For this example :

int[][] multiTab = {{1,2,3,4,5,6},
{1,2,3,4},
{1,2,3,4,5,6,7,8,9}};

What would be the rows and the columns ?

  • 1
    first row 6 columns and 2nd 4 and 3rd 9 – Pavneet_Singh Oct 07 '16 at 12:09
  • It is in array (which in Java is an object by itself), which has another array as each of its elements. – SJuan76 Oct 07 '16 at 12:10
  • Possible duplicate of [Syntax for creating a two-dimensional array](http://stackoverflow.com/questions/12231453/syntax-for-creating-a-two-dimensional-array) – Turing85 Oct 07 '16 at 12:20
  • @PavneetSingh Really? Why can't it be 3 columns with different number of lines? – Adrian Colomitchi Oct 07 '16 at 12:23
  • @AdrianColomitchi because 2D array are like matrix so it's better to keep things clear and simple instead of cool and messy :P – Pavneet_Singh Oct 07 '16 at 12:26
  • @Turing85 it is not a duplicate: at least not of that question. This question asks about how it works, not how to use it. – Zoe Oct 07 '16 at 13:07
  • @PavneetSingh Seen many matrices lately with uneven number of elements on a line/column? Other than that, [I wouldn't call Fortran or OpenGL](https://en.wikipedia.org/wiki/Row-major_order#Programming_languages_and_libraries) cool (maybe I'd agree with messy, if they wouldn't be so fast) – Adrian Colomitchi Oct 07 '16 at 13:13
  • @AdrianColomitchi i am familiar with opengl and how they use array and they work with pointers to access memory but it's java anyway eventually you have to increment something you can call it columns numbers or whatever (although 3 columns with different numbers makes no sense because columns are known as vertically on earth ) , it;s not really beneficial for either or any to pursue this conversation ,thanks for your time – Pavneet_Singh Oct 07 '16 at 13:24
  • @PavneetSingh Even in Java you may consider column-major approaches - many linear algebra packages allow one to switch between row and column major. And this is natural if you think at matrix multiplication, the best performance combination would be first matrix in line major but column-major for the second. Other java libs [prefer 'column major'](https://en.wikipedia.org/wiki/Matrix_Toolkit_Java) for dense matrices. That is to say: no, Java doesn't impose any row/column semantics on the `array of arrays` construct - they are simply arrays of arrays full-stop. – Adrian Colomitchi Oct 07 '16 at 13:37
  • @AdrianColomitchi mostly the world say multiplication with matrix is first row first column , first row second column , i am looking forward to see your answer on this post then SO community will be able to enlighten us more – Pavneet_Singh Oct 07 '16 at 13:58

1 Answers1

0
int[][] multiTab = {{1,2,3,4,5,6},//[0][x]
{1,2,3,4},//[1][x]
{1,2,3,4,5,6,7,8,9}};//[2][x]

This type of array can also be called a multidimensional array. This is a type of array that is useful if you want to get information that hangs together. Example:

if [0][x] is a country, [1][x] is a city and [2][x] is some other info, you can use the same var for x but change the first var to get the info that hangs together.

Read more here: http://www.homeandlearn.co.uk/java/multi-dimensional_arrays.html

Or here: Syntax for creating a two-dimensional array

Or here: http://www.dailyfreecode.com/code/multi-dimensional-array-1352.aspx

Community
  • 1
  • 1
Zoe
  • 27,060
  • 21
  • 118
  • 148