ijaz khattak ijaz khattak - 6 months ago 15
Java Question

Passing 2d array for print in jjava

Hi i am new to java as u can see from the code. what i want it to populate an array in one method and then in second method i want to display that array.

public class Rulebase {

String[][] array()
{
String[][] table = new String[3][4];
table[0][0]="1";
table[0][1]="person(?x),patientid(?y),hasid(?x,?y)";
table[0][2]="patient(?x)";
table[0][3]="1";

table[1][0]="1";
table[1][1]="patient(?x),hasbp(?x,high)";
table[1][2]="hassituation(?x,emergency)";
table[1][3]="1";

table[2][0]="1";
table[2][1]="hassituation(?x,emergency)";
table[2][2]="calldr(emergency)";
table[2][3]="1";
return table;

}
void showarray(String[][] table)
{
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
System.out.println(table[i][j]);
}
}
public static void main(String[] args) {
Rulebase rb = new Rulebase();
rb.array();
rb.showarray();
// TODO code application logic here

}

}


advice is required please.

Answer

array() is returning the array but you aren't storing it anywhere and showarray() needs String [][] but you haven't passed anything.

String[][] temp= rb.array();
rb.showarray(temp);

btw, in showarray() you are hard coding array dimensions(4x4) when your array is 3x4 use length instead.

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