JAVA中能否返回二维数组

来源:百度知道 编辑:UC知道 时间:2024/05/11 03:15:58
看下面一段代码:
public int FormToArray(JTable ta){
int row=ta.getrowCount();
int col=ta.getcolCount();
int[][] array=new int[row][];
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
array[i][j]=ta.getValueAt(i+1,j+1)
return array;
}
里面可能有一些单词错误,主要看下能否返回二维数组,如不行,应怎样才行呢?
但是上面不行啊,出现不兼容类型,怎么解决它呢
回复_sky:是从键盘输入的数字

用一个4*N的数组,申明如下:
String[][] str=new String[4][];
下面初始化:
str[0]=new String[]{"",",","幢,","室",""};
str[1]=new String[]{"",",","弄,","号,","室",""};
str[2]=new String[]{"",",","号",""};
str[3]=new String[]{"","",""};
数组的第二维长度可以不一样
int twoD[][] = new int [4][];
twoD[0]=new int[4];
twoD[1]=new int[5];
twoD[2]=new int[6];
twoD[3]=new int[7];
再进行赋值
twoD[0]={1,2,3,4};

public int[][] FormToArray(JTable ta){
int row=ta.getRowCount();
int col=ta.getColumnCount();
int[][] array=new int[row][col];
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
array[i][j]=(Integer)ta.getValueAt(i+1,j+1);
return array;
}

这是可以编译过的方式
还有就是忆天_sky的问题,你table中存的什么类型很关键

可以,设置返回值类型就可以了