• Post Reply Bookmark Topic Watch Topic
  • New Topic

TwoD array declaration  RSS feed

 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

//this line
int matrix[][]=new int[2][1];
this line declares twod array that is capable of holding 2 oned arrays.
or I can say above line declares a matrix that will have two rows.

What does the value in second square bracket indicate.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of it as an array of arrays. Now does it help clear up things?
 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know TwoD array means array of arrays.

I am asking what does value of second square bracket means.

I declared array matrix that can hold two oneD arrays each with size 1.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahtab Alam wrote:I know TwoD array means array of arrays.

Plainly; but do you understand all the consequences of that?

I declared array matrix that can hold two oneD arrays each with size 1.

So: each "row" has one "column".

I'm not quite sure where your problem is. Perhaps you could clarify.

Winston
 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if I declare twoD array as int matrix[][]=new int[2][0]

What that declaration means , that value in second square bracket is irritating.
 
Paweł Baczyński
Bartender
Posts: 2086
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahtab Alam wrote:What if I declare twoD array as int matrix[][]=new int[2][0]

That means you create an array that holds int[] references and assign it to variable matrix. The length of this array is 2.
The array is filled with references to arrays that can hold int values. The length of each of those arrays is 0.

Mahtab Alam wrote:What that declaration means , that value in second square bracket is irritating.

If it is irritating you it is possible to omit it and write: int matrix[][]=new int[2][].
That means you create an array that holds int[] references and assign it to variable matrix. The length of this array is 2.
The array is filled with null references.

By the way, by convention you should write:
int[][] matrix
instead of
int matrix[][].
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahtab Alam wrote:What if I declare twoD array as int matrix[][]=new int[2][0]

What that declaration means , that value in second square bracket is irritating.


I am assuming that you find it irritating because you don't need it. You are going to further define what each array element is, so why do you need to instantiate it.

The answer is ... you don't. You can do this instead...



This will create an array of size two that hold int arrays, but each element will be set to null.

Henry
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahtab Alam wrote:What if I declare twoD array as int matrix[][]=new int[2][0]
What that declaration means , that value in second square bracket is irritating.

Irritating? Blimey. If that's the only thing you find irritating about Java, then you've led a charmed life.

Languages (and I don't just mean computer languages) ARE irritating. Why do I need to say "five and twenty" in German, when I mean "twenty-five"? Because you do.

In this case, I don't even see the problem: You've declared an array of 2 rows with 0 columns each - what's wrong with that?
It's your declaration, and Java has done exactly what you asked it to.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!