-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?

F. Müller
  • 3,969
  • 8
  • 38
  • 49
Rubini
  • 15
  • 2
  • 2
    what is the error? can u pls provide it? – reyad Jul 31 '20 at 12:11
  • also, provide what is the constraint of rowarr[] and colarr[] elements.. – reyad Jul 31 '20 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??!! – Omid.N Jul 31 '20 at 12:42

3 Answers3

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...

reyad
  • 1,392
  • 2
  • 7
  • 12
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.

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
  • using `rowarr[k]` and `colarr[m]` as an index for the 2d is liklely to cause errors! it is meaningless.! – Omid.N Jul 31 '20 at 12:44
  • @Omid.N excuse me? do you mean that my answer is meaningless? I think that is exactly what I explain here.. no? :) – Giorgi Tsiklauri Jul 31 '20 at 12:46
  • No no! not at all! the problem is with the code in the question. :) – Omid.N Jul 31 '20 at 12:49
  • 1
    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. – Ted Hopp Jul 31 '20 at 13:45
  • @TedHopp good point. I've corrected my mechanical mistake and provided correct answer. – Giorgi Tsiklauri Jul 31 '20 at 14:03
0

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

erwanlfrt
  • 111
  • 11
  • This is not an answer, please keep these kind of conversations/clarifications in the comments. – Giorgi Tsiklauri Jul 31 '20 at 12:39
  • 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 – erwanlfrt Jul 31 '20 at 13:01
  • 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. – Giorgi Tsiklauri Jul 31 '20 at 13:02
  • Sorry it was just a figure of speech :) – erwanlfrt Jul 31 '20 at 13:06