-1

I need to print a 2d array in which I will be giving the values for the row and column at run time.

package test;

import java.util.Scanner;

public class test1 {
    
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int rowsize=scan.nextInt();
        int i=0;int j=0;
        int rowarr[]=new int[rowsize];
        int colsize=scan.nextInt();
        int colarr[]=new int[colsize];
        int d[][]=new int[rowsize][colsize];
        for( i=0;i<rowarr.length;i++) {
            rowarr[i]=scan.nextInt();
        }
        
        for( j=0;j<colsize;j++) {
            colarr[j]=scan.nextInt();
        }
        
        for(int k=0;k<rowarr.length;k++) {
            for(int m=0;m<colarr.length;m++) {
                System.out.print(d[rowarr[k]][colarr[m]]);
            }
            System.out.println();
        }
    }
}

I'm getting an error in last line while printing d[rowarr[k]][colarr[m]]. Could anyone provide suggestions?

3
  • 2
    what is the error? can u pls provide it? Commented Jul 31, 2020 at 12:11
  • also, provide what is the constraint of rowarr[] and colarr[] elements.. Commented Jul 31, 2020 at 12:12
  • What are you doing with this code...!!!... First, you declare two one-dimensional arrays then declare a 2d array which is empty! more ridiculously you're using rowarr[k] and colarr[m] as an index for the 2d array. what if they are more than array size??!! Commented Jul 31, 2020 at 12:42

3 Answers 3

1

As you are not providing any details about errors and constraints, I am answering on assumptions:

The array d[][] is accessible for d[0...rowsize-1][0...colsize-1] indices.

But, if you try to access any other index which in negative or which is greater than rowsize-1 like d[rowsize+5][0], then it'll produce error.

And likewise, if u try to access d[0][colsize+2] or something like that, or d[0][-3], it'll also give error.

So, if you want to access d[x][y] (where, x and y are integers) then x and y must follow the below rules:

  1. 0 <= x < rowsize
  2. 0 <= y < colsize

check if you're accessing d[][] properly...

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

Comments

0

Tell me if I'm wrong, but I think your d array remains empty. It's initialized and then nothing.

4 Comments

This is not an answer, please keep these kind of conversations/clarifications in the comments.
For me this is a part of the answer as you said yourself in your own answer, he's not populating the d array. @GiorgiTsiklauri
You have a correct understanding of the problem, but answer can't be a question itself. You're asking if you're wrong or not. When you want to question something, better to go in the comments and clarify any doubts you might have.
Sorry it was just a figure of speech :)
0

You're having an ArrayIndexOutOfBoundsException, because:

System.out.println(d[rowarr[k]][colarr[m]])

tries to access the element of d[][] array, with particular values of rowarr[k] and colarr[m], which goes outside the range of the given array.

For example, if you populate rowarr[0] with 1, colarr[0] - with 6, but you have generated rowarr[] and colarr[] arrays with the sizes of 2 and 3 respectively, then d[1][6] (at your first iteration - d[rowarr[k]][colarr[m]]), will be out of bounds, because there is no 6th element in the d[1] array.

5 Comments

using rowarr[k] and colarr[m] as an index for the 2d is liklely to cause errors! it is meaningless.!
@Omid.N excuse me? do you mean that my answer is meaningless? I think that is exactly what I explain here.. no? :)
No no! not at all! the problem is with the code in the question. :)
The d[][] array is not "empty" (as in having null row values). It is fully populated with zeroes as a result of the initializer new int[rowsize][colsize]. This does not explain why OP's code is throwing an exception.
@TedHopp good point. I've corrected my mechanical mistake and provided correct answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.