2D array syntax

A

anon0ne

Hello I am making a 2D array however I am wondering what is wrong with the
following syntax.

int[][] parameters = new int[7][5];
parameters[0] = new int[] {1,2,3,4,5};
parameters[1] = new int[] {2,3,4,5,6};
....

I know I could use the following to create the same thing:
int[][] parameters = {{1,2,3,4,5},....,{7,8,9,10,11}};

to get the same effect but I would rather use the initial syntax for what I
am trying to do.
Is there a way where I can achieve this?

thank you for any advice.
 
M

Mark Space

anon0ne said:
Hello I am making a 2D array however I am wondering what is wrong with the
following syntax.

int[][] parameters = new int[7][5];

Try:

int[][] parameters = new int[5][7];

instead.

parameters[0] = new int[] {1,2,3,4,5};
parameters[1] = new int[] {2,3,4,5,6};
...
 
A

Andreas Leitgeb

Mark Space said:
anon0ne said:
Hello I am making a 2D array however I am wondering what is wrong with the
following syntax.
int[][] parameters = new int[7][5];
Try:
int[][] parameters = new int[5][7];
instead.
parameters[0] = new int[] {1,2,3,4,5};
parameters[1] = new int[] {2,3,4,5,6};
...

I guess, you (the OP) got an java.lang.ArrayIndexOutOfBoundsException: 5
at a line like:
parameters[5] = new int[] {42,42,42,42,42};


Anyway, even with Mark's correction, you waste a couple of arrays,
because the initialization already creates a structure of nested
arrays, and the inner ones then get replaced by those later
assignments.

I'd suggest you initialize it as:
int[][] parameters = new int[][7];
unless you intended to replace only a few of them and leave
some/most of the inner arrays unreplaced.
 
T

Tom McGlynn

Hello I am making a 2D array however I am wondering what is wrong with the
following syntax.

int[][] parameters = new int[7][5];
parameters[0] = new int[] {1,2,3,4,5};
parameters[1] = new int[] {2,3,4,5,6};
...

I know I could use the following to create the same thing:
int[][] parameters = {{1,2,3,4,5},....,{7,8,9,10,11}};

to get the same effect but I would rather use the initial syntax for what I
am trying to do.
Is there a way where I can achieve this?

thank you for any advice.

I don't think that there is anything wrong with this (despite Mark's
comments). E.g., the following program runs fine and produces the
expected output.

public class Test {
public static void main(String[] args) throws Exception {

int[][] x = new int[7][5];

x[0] = new int[]{1,2,3,4,5};
x[1] = new int[]{2,3,4,5,6};
x[2] = new int[]{3,3,4,5,6};
x[3] = new int[]{4,3,4,5,6};
x[4] = new int[]{5,3,4,5,6};
x[5] = new int[]{6,3,4,5,6};
x[6] = new int[]{7,3,4,5,6};

for (int i=0; i<x.length; i += 1) {
for (int j=0; j<x.length; j += 1) {
System.out.print(x[j]+" ");
}
System.out.println();
}
}
}

What problems had you seen?

If you are going to build the array piecemeal, then you may wish to
consider Andreas' comment, though I think that you need to invert his
final sets of brackets.

int[][] parameters = new int[7][];


Regards,
Tom McGlynn
 
J

John B. Matthews

"anon0ne said:
Hello I am making a 2D array however I am wondering what is wrong with the
following syntax.

int[][] parameters = new int[7][5];

I think of this as a request for 7 arrays (numbered 0 through 6), each
holding 5 integers (numbered 0 through 4). The initial value of each
parameters is an array reference; the initial value of each
parameters[j] is zero.

Using this approach, I'd defer the column count declaration, as shown
below. This will save allocating 7 arrays, each containing 5 zeros,
which are replaced by your explicit initialization.
parameters[0] = new int[] {1,2,3,4,5};
parameters[1] = new int[] {2,3,4,5,6};

Coming from a number of row-major array order languages, my mental model
is 7 rows and 5 columns, but Java arrays are allowed to vary in length.
As a result, a ragged declaration is permitted, e.g.:

parameters[2] = new int[] {-1,-2,-3};
I know I could use the following to create the same thing:
int[][] parameters = {{1,2,3,4,5},....,{7,8,9,10,11}};

The advantage of this scheme is that the compiler will calculate the
dimensions; the disadvantage is that it will calculate the wrong
dimensions if you skip a value, or add an extra one.
to get the same effect but I would rather use the initial syntax for
what I am trying to do. Is there a way where I can achieve this?

I would encourage you to display the initialization in whatever way
makes most sense to someone reviewing the program's correctness or
maintaining the code as it evolves.

public class Test {

public static void main(String[] args) {
int[][] parameters = new int[7][];
parameters[0] = new int[] {1,2,3,4,5};
parameters[1] = new int[] {1,2,3,4,5};
parameters[2] = new int[] {1,2,3,4,5};
parameters[3] = new int[] {1,2,3,4,5};
parameters[4] = new int[] {1,2,3,4,5};
parameters[5] = new int[] {1,2,3,4,5};
parameters[6] = new int[] {1,2,3,4,5};
}
}
 
D

Durango2008

anon0ne said:
Hello I am making a 2D array however I am wondering what is wrong with the
following syntax.

int[][] parameters = new int[7][5];
parameters[0] = new int[] {1,2,3,4,5};
parameters[1] = new int[] {2,3,4,5,6};
...

I know I could use the following to create the same thing:
int[][] parameters = {{1,2,3,4,5},....,{7,8,9,10,11}};

to get the same effect but I would rather use the initial syntax for what
I am trying to do.
Is there a way where I can achieve this?

thank you for any advice.


Thank you everyone,
I figured out my problem the syntax is correct I just had it in the wrong
place and
overlooked this easy fix.
 
B

blue indigo

Hello I am making a 2D array however I am wondering what is wrong with the
following syntax.

int[][] parameters = new int[7][5];
parameters[0] = new int[] {1,2,3,4,5};
parameters[1] = new int[] {2,3,4,5,6};

Nothing so far as I can tell, although you're redundantly creating arrays
of five zeros and then replacing them with arrays of nonzero digits. Try
using new int[7][].

P.S. you're not very anonymous. DSL from Southwest Bell in Houston, Texas
and a Prodigy account. :)

Several free newsservers exist that hide your IP if you want to be more
anonymous. Motzarella.org and aioe.org are two of them. The downside:
they're not particularly reliable. They are also useful in case your usual
newsserver has downtime. Or if your ISP stops carrying usenet, as so many
of them are doing lately because of a stupid and pointless crusade by some
attorney general or another. They certainly beat the hell out of google
effing groups.

Several free newsreaders exist that are much better than what you're
using, too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top