纠错!!!!
import java.util.*;
public class shiyan22 {
public static void main(String args[]){
Vector vector=new Vector(3,3);
//声明并创建初始大小为3 增量为3
vector.add(new Employer("zhang",20,"male","manager",3000));
vector.add(new Employer("li",20,"male","leader",2000));
vector.add(new Employer("wang",20,"male","employer",1000));
//连续向数组中加入三个对象
System.out.println("Vector's length is:"+vector.size());
System.out.println("Vector's contents are:");
for(int i=0; i<=vector.size();i++){
//利用循环输出Vector数组中的内容
Employer employer = (Employer)vector.get(i);
//利用vector数组中的get()方法得到对应位置上的元素,并转换为Employer的对象
System.out.println("Name: "+employer.getName());
System.out.println("Age:"+employer.getAge());
System.out.println("Sex:"+employer.getSex());
System.out.println("Post:"+employer.getPost());
System.out.println("Salary:"+employer.getSalary());
System.out.println();
}
System.out.println("add another object into vector:");
vector.add(new Employer("zhao",23,"female","employer",1000));
//向vector数组中再加一个元素
Employer newEmployer=(Employer)vector.lastElement();
System.out.println("Name: "+newEmployer.getName());
System.out.println("Age:"+newEmployer.getAge());
System.out.println("Sex:"+newEmployer.getSex());
System.out.println("Post:"+newEmployer.getPost());
System.out.println("Salary:"+newEmployer.getSalary());
System.out.println();
}
}
class Employer{
private String name;
private int age;
private String sex;
private String post;
private double salary;
public Employer(){}//类的默认构造方法;
public Employer(String name,int age,String sex,String post, double salary){
this.name=name;
this.sex=sex;
this.age=age;
this.post=post;
this.salary=salary;
}
public String getName(){return name;}
public int getAge(){return age;}
public String getSex(){return sex;}
public String getPost(){return post;}
public double getSalary(){return salary;}
}
这个程序中 在vector中添加的 内容为何在运行的时候 不显示啊????? 恳请大家给我纠正错误
------解决方案--------------------for(int i=0; i<=vector.size();i++){
//利用循环输出Vector数组中的内容
i<=vector.size();改为i<vector.size();
------解决方案--------------------因为你的i是从0开始的