2

我成功添加了第一个学生,但是当我添加第二个学生时,线程“ ”
中出现异常:数组索引超出范围:11 mainjava.lang.ArrayIndexOutOfBoundsException

at java.util.Vector.get(Unknown Source)  
    at business.StudentCollection.UseArray(StudentCollection.java:58  
    at business.Application.main(Application.java:30) 

代码段

 public class StudentCollection {  
private Vector<Student> collection;  
private int count;  

public StudentCollection ()
{  
collection=new Vector<Student>(10,2);  
count=0;  
for( int i=0;i< collection.capacity(); i++) //i read that object cannot be added to 
vectors if empty  
collection.add(i,new Student(0,"No Student",0));

}  

public void addStud(int ID,String name,int Credits)
   {    

for(int i=0;i< collection.capacity();i++)  
 if(collection.get(i)==null)  // new Error
collection.add(i,new Student(0,"No Student",0)); //making sure vector new index are   filled

collection.add(count,new Student(ID,name,Credits));  
count++;  

  }  
public Student UseArray(int x){  \\ Error here line 58
return collection.get(x);  

                      }

 public int getlengthh(){  
    return collection.capacity();  
                }  
}  
 public static void main (String [] args ){  
 StudentCollection C=new StudentCollection();  


        System.out.println("Enter Student's ID");  
        x=scan.nextInt();  
        for (int i=0;i< C.getlengthh();i++){    
if(C.UseArray(i).getID()==x){  // Error here
        System.out.println("A student with this ID already exists.Do you want to overwrite the existing student?yes/no");  
        scan.nextLine();  
        ans=scan.nextLine();  

        if (ans.equalsIgnoreCase("yes")){
            C.delete(x);
        continue;
        }
        else {
            System.out.println("Enter Student's ID");
        x=scan.nextInt();
        }
            }
        }

        System.out.println("Enter Student's name");
        Str=scan.nextLine();
        Str=scan.nextLine()+Str;
        System.out.println("Enter number of credits");
        y=scan.nextInt();
        C.addStud(x,Str,y);

    }
4

2 回答 2

1

修改为

 public Student UseArray(int x){  \\ Error here line 58
     if(collection.size() > x)
        return collection.get(x); 
     return null; 

    }

容量和大小是有区别的。容量返回由创建的数组的长度Vector以保存当前和传入元素。而 size 是已经放入向量中的元素的数量。话虽如此,在检查元素是否存在时,不使用容量使用大小,如下所示:

 public int getlengthh(){  
    return collection.size();  
                } 

即使capacity大于index仍然添加会抛出异常。看这里

于 2013-01-13T12:45:51.830 回答
0

The whole point of collection classes like Vector is that you don't need to manually index into them like an array. You don't need to maintain a count variable, either -- when you want to know how many students you have, simply call size() on the Vector. Now, unless you need Vector's thread-safety, I would go with ArrayList instead, but they are both implementations of List, which means all you need to do is call add(Student).

I would take a good look at the Java Collections Trail before continuing.

Also, on a stylistic note, clean up your source formatting. Having inconsistent indentation will make it very hard to review your code for bugs.

于 2013-01-13T13:05:10.030 回答