本帖最后由 liangliangliang2009 于 2009-09-22 16:27:42 编辑

解决方案 »

  1.   

    或者可以让index超出范围时,让方法不执行,并打印出错误信息。还有可不可让方法根据情况返回两种类型的值??????
      

  2.   

    变通一下
    如果index超出了范围抛出异常
    public int get(int index) throws Exception {
        if(超出范围)
           throw new Exception("超出范围");
        return 需要的值
    }
      

  3.   

    同意楼上的,何必非要返回null呢抛出异常的方式岂不更好?!
      

  4.   

    public int get(int index) {
    try{
    if(index > 0 && index <= listSize)
    {return (dataindex);}
    else {
    throw new Exception("超出了范围!");
    }
    }
    catch (Exception){
    System.out.println();
    }

    }