Originally posted by mandy fern:
public class number
{
public int[][] array2D = {
{ 1, 2, 3 ,4 , 5},
{ 6, 7, 8, 9, 10},
{ 11, 12, 13, 14, 15},
{ 16, 17, 18, 19, 20}
};
{
int a= Console.readInt("add a number");
array2D[0][0]=a;
int b= Console.readInt("add a number");
array2D[0][1]=b;
int c= Console.readInt("add a number");
array2D[0][2]=c;
int d= Console.readInt("add a number");
array2D[0][3]=d;
int e= Console.readInt("add a number");
array2D[0][4]=e;
int f= Console.readInt("add a number");
array2D[1][0]=f;
int g= Console.readInt("add a number");
array2D[1][1]=g;
int h= Console.readInt("add a number");
array2D[1][2]=h;
....etc
My question is:Is there a way to shorten this or do I have to to do this
for every component of the array?
Instead of reading each number into an int and then moving it into an element of your array, couldn't you read a number from console directly into the arrary element. That way you could put this in a loop and use a variable in each of the two elements of your array and just increment them each time you add another number.
I think this would work.