在一个操作类中有个方法是接收外部对象数组,并在方法里根据数据库的统计进行实例化,
 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需要实例化,但在其他类中根本不知道该对像数组的大小, 这要如何解决!

解决方案 »

  1.   

    简单方案:
    将楼主的代码: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.   

    1.将数组作为返回值,反正要在方法内部new,还传这个参数干什么。
    2.使用ArrayList之类的东西。不用管大小,往里放数据就是了。
      

  3.   

    to 1 楼 crazylaa,按照你的方法,该对象数组的操作结束后依旧是null。 
    to 2 楼 islandrabbit, 你的方法考虑过,可要求是操作方法没有返回值,是引用传递。
    to 3 楼 ZangXT,使用ArrayList是可以,不过还是希望能找到直接用对象数组的方法!谢谢大家啦!
      

  4.   

    关键的问题在于Java的参数只能通过值传递的方式进行。注意,数组也是引用类型,在函数内部创建新的数组对象并赋值给参数的做法是无效的,如:
    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);
        }
    }但如果楼主既不能在函数调用前确定数组元素的个数,又不想使用返回值的方式,更不想利用除了数组类型以外的类,那我们就帮不了你了!