您的位置:首页 > 其它

Vector 应用实例

2010-08-20 15:27 260 查看
import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;

public class TestVector {

public static void printElement(Collection<?> c)
{
Iterator<?> it = c.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}

public static void main(String[] args) {

Vector<Student> transcript = new Vector<Student>();
transcript.addElement(new Student("Lihua",1,83));
transcript.addElement(new Student("Lilei",2,78));
transcript.addElement(new Student("Hanmeimei",3,90));

System.out.println(transcript.size());

transcript.insertElementAt(new Student("Wangping",4,78),1);
printElement(transcript);

Student s = (Student)transcript.elementAt(0);
System.out.println(s.toString());
}//end of main
}// end of TestVector

class Student{

private String sName;
private int sID;
private double sScore;

public Student(String sName, int sID, double sScore)
{
this.sName = sName;
this.sID = sID;
this.sScore = sScore;
}

public String getsName()
{
return this.sName;
}
public double getsScore()
{
return this.sScore;
}
public String toString()
{
return "NO." + this.sID + ":" + this.sName + "," +this.sScore;
}
}

返回结果:

3
NO.1:Lihua,83.0
NO.4:Wangping,78.0
NO.2:Lilei,78.0
NO.3:Hanmeimei,90.0
NO.1:Lihua,83.0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: