0

I have a 2d array named multi and its declaration is

Object[][] multi = new Object[10][10];

I tried to add some elements like that x and y are Integers

 multi[y][x]=10;

not when i print anything from this array or the hole array i got nulloutput the whay i print

System.out.println(multi[0][0]);

or

for(int r = 0; r < 10; r++) {
   for(int g = 0; g <10; g++) {
       System.out.println(multi[r][g]);
   }
}
6
  • 1
    you should get 99 null prints, and one print with 10, if you only assign one value. Could you include the whole code, or rather the method that is executed this specific code. Commented Feb 25, 2016 at 10:46
  • 1
    All of the elements of multi[r] (for 0 <= r < 10) are initially null; they would only not be if you assign them a non-null value subsequently. Commented Feb 25, 2016 at 10:47
  • no m8 also when i add oly one element multi[0][0]=10; and print oly one element like that System.out.println(multi[0][0]); i got a null print Commented Feb 25, 2016 at 10:50
  • If you set multi[0][0]=10; and then print it, you WILL get "10", Commented Feb 25, 2016 at 10:54
  • @ring bearer no i got a null output Commented Feb 25, 2016 at 10:54

2 Answers 2

1

Put values to array and print them.

Object[][] multi = new Object[10][10];
Double d        = 0.1;
Double anotherD = 2.5;

multi[0][0] = d       ;
multi[1][3] = anotherD;

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

You should see following output when run the above code:

0.1 null [lots of nulls ommited] null 2.5 null [lots of nulls ommited] null

Sign up to request clarification or add additional context in comments.

Comments

0

If you array stores obects of the type Object you should consider adding real objects and not just an undeclared value.

Object[][] multi = new Object[10][10];

Instead of: multi[0][0] = 10; You should use multi[0][0] = new Double(10);

Or you do an additional step and declare a variable for that

Double dValue = 10; multi[0][0] = dValue;

If you only store one type of object (or basic type) you should consider declaring an array of that specific type; e.g. for double:

double[][] multi = new double[10][10];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.