在一个操作类中有个方法是接收外部对象数组,并在方法里根据数据库的统计进行实例化,
public void GetInfoNewToNSSformChanNo(InfoBean[] Info1, String ChanNo)
{
ds.last();
int count=ds.getRow();
ds.beforeFirst();
Info1 = new InfoBean[count];
while(ds.next())
{
Info1[i]=new InfoBean();
}而在调用该方法的其他类中若只声明对象数组:
InfoBean[] bean;
Operator obj=new Operator();
obj.GetInfoNewToNSSformChanNo(bean, "1");则提示bean需要实例化,但在其他类中根本不知道该对像数组的大小, 这要如何解决!
public void GetInfoNewToNSSformChanNo(InfoBean[] Info1, String ChanNo)
{
ds.last();
int count=ds.getRow();
ds.beforeFirst();
Info1 = new InfoBean[count];
while(ds.next())
{
Info1[i]=new InfoBean();
}而在调用该方法的其他类中若只声明对象数组:
InfoBean[] bean;
Operator obj=new Operator();
obj.GetInfoNewToNSSformChanNo(bean, "1");则提示bean需要实例化,但在其他类中根本不知道该对像数组的大小, 这要如何解决!
解决方案 »
- 求SSH分页程序。
- hibernate 级联删除问题
- 各位看一下这个问题啊
- stuts编程中从一个action如何直接转到另一个action?
- stated in source org.apache.jsp does not match directory
- Tomcat5.0运行Struts出现的问题,请指教。
- struts见鬼的问题~~!很是奇怪~
- 急::vb和java连接的问题????(给分)
- 初写EJB的SessionBean,抱错,请帮忙看看。谢谢
- 关联查询效率,与数据冗余如何权衡
- 各位前辈,请帮忙看一个我的简历。。。哪里还要改进一下,谢谢。。。
- myeclipse下jsp怎么用不了jquery,而html用得起?
将楼主的代码:public void GetInfoNewToNSSformChanNo(InfoBean[] Info1, String ChanNo)
{
ds.last();
int count=ds.getRow();
ds.beforeFirst();
Info1 = new InfoBean[count];
while(ds.next())
{
Info1[i]=new InfoBean();
} 改为: public InfoBean[] GetInfoNewToNSSformChanNo(InfoBean[] Info1, String ChanNo)
{
ds.last();
int count=ds.getRow();
ds.beforeFirst();
Info1 = new InfoBean[count];
while(ds.next())
{
Info1[i]=new InfoBean();
return InfoBean[] Info1;
}
2.使用ArrayList之类的东西。不用管大小,往里放数据就是了。
to 2 楼 islandrabbit, 你的方法考虑过,可要求是操作方法没有返回值,是引用传递。
to 3 楼 ZangXT,使用ArrayList是可以,不过还是希望能找到直接用对象数组的方法!谢谢大家啦!
public static void update(int[] tmp) {
tmp = new int[2];
tmp[0] = 10;
tmp[1] = 20;
}public static void main(String[] args) {
int[] data = new int[1];
update(data);
// 此时data的数据没有改变
for (int item : data) {
System.out.println(item);
}
}方式一:如2楼阿牛所述,在函数内部创建数组对象,通过返回值的方式返回是可行的。如:
public static int[] update() {
int[] tmp = new tmp[2];
tmp[0] = 10;
tmp[1] = 20;
}public static void main(String[] args) {
int[] data = new int[2];
data = update();
// 此时data的数据变化了
for (int item : data) {
System.out.println(item);
}
}方式二:如果在函数调用之前已经明确数组的大小,函数内部改变数组元素的值(即改变数组对象的内容)是可行的。如:
public static void update(int[] tmp) {
tmp[0] = 10;
tmp[1] = 20;
}public static void main(String[] args) {
int[] data = new int[2];
update(data);
// 此时data的数据变化了
for (int item : data) {
System.out.println(item);
}
}方式三,如3楼ZangXT所述,参数是一个ArrayList(对象),函数内部改变对象的内容(增加或删除ArrayList的元素)也是可行的。如:
public static void update(List<Integer> tmp) {
tmp.add(10);
tmp.add(20);
}public static void main(String[] args) {
List<Integer> data = new ArrayList<Integer>();
update(data);
// 此时data的数据变化了
for (int item : data) {
System.out.println(item);
}
}但如果楼主既不能在函数调用前确定数组元素的个数,又不想使用返回值的方式,更不想利用除了数组类型以外的类,那我们就帮不了你了!