当前位置: 技术问答>java相关
在JAVA中如何传递一个数组变量作为参数,代码如下!
来源: 互联网 发布时间:2015-06-20
本文导语: import javax.swing.table.*; public class DataTableModel extends AbstractTableModel{ public String[] columnNames; public Object[][] values; public void setHeader(String[] col,Object[][] val){ this.columnNames=col; this.valu...
import javax.swing.table.*;
public class DataTableModel extends AbstractTableModel{
public String[] columnNames;
public Object[][] values;
public void setHeader(String[] col,Object[][] val){
this.columnNames=col;
this.values=val;
}
public int getRowCount(){
return values.length;
}
public int getColumnCount(){
return 5;
}
public Object getValueAt(int row,int column){
return values[row][column];
}
public String getColumnName(int column){
return columnNames[column];
}
}
×××××××××××××××××××××××××××××××××××××××
public class test{
DataTableModel x=new DataTableModel();
public String[] columnNames={"Name","Address","Phone","Postal Code","Date"};
public Object[][] values={
{"Janet Abel","117 Sampson Street",
"323432454","70669-0573","2009/10/3"},
{ "Smith Abel","117 Sampson Street",
"323623444","74069-0573","2009/10/3"}
};
x.setHeader(coloumNames,values);
}
象上边的这两个CLASS,当调用x.setHeader(coloumNames,values);
时就会出错,我就时想把数组作为参数传递,怎么办?希望看代码的大侠能明白我的意思,帮小弟解决!
public class DataTableModel extends AbstractTableModel{
public String[] columnNames;
public Object[][] values;
public void setHeader(String[] col,Object[][] val){
this.columnNames=col;
this.values=val;
}
public int getRowCount(){
return values.length;
}
public int getColumnCount(){
return 5;
}
public Object getValueAt(int row,int column){
return values[row][column];
}
public String getColumnName(int column){
return columnNames[column];
}
}
×××××××××××××××××××××××××××××××××××××××
public class test{
DataTableModel x=new DataTableModel();
public String[] columnNames={"Name","Address","Phone","Postal Code","Date"};
public Object[][] values={
{"Janet Abel","117 Sampson Street",
"323432454","70669-0573","2009/10/3"},
{ "Smith Abel","117 Sampson Street",
"323623444","74069-0573","2009/10/3"}
};
x.setHeader(coloumNames,values);
}
象上边的这两个CLASS,当调用x.setHeader(coloumNames,values);
时就会出错,我就时想把数组作为参数传递,怎么办?希望看代码的大侠能明白我的意思,帮小弟解决!
|
你的 x.setHeader(coloumNames,values);这句语句必须放到一个方法之内
看看你的
public class test {
……
//这部分是定义变量的
//不能放置 x.setHeader(coloumNames,values);这种语句
pulbic test() {
x.setHeader(coloumNames,values);
//放在这里或者其它方法内才行
}
},
看看你的
public class test {
……
//这部分是定义变量的
//不能放置 x.setHeader(coloumNames,values);这种语句
pulbic test() {
x.setHeader(coloumNames,values);
//放在这里或者其它方法内才行
}
},
|
x.setHeader(coloumNames,values);
===>
x.setHeader(columnNames,values);
把x.setHeader(...)放在一个方法里
===>
x.setHeader(columnNames,values);
把x.setHeader(...)放在一个方法里