`
modabobo
  • 浏览: 509346 次
文章分类
社区版块
存档分类
最新评论

Java 中 Vector的用法

 
阅读更多

import java.util.*;

/**
<wbr>* 演示<strong style="background-color:#a0ffff; color:black">Vector</strong>的使用。包括<strong style="background-color:#a0ffff; color:black">Vector</strong>的创建、向<strong style="background-color:#a0ffff; color:black">Vector</strong>中添加元素、从<strong style="background-color:#a0ffff; color:black">Vector</strong>中删除元素、<br><wbr>* 统计<strong style="background-color:#a0ffff; color:black">Vector</strong>中元素的个数和遍历<strong style="background-color:#a0ffff; color:black">Vector</strong>中的元素。<br><wbr>*/</wbr></wbr></wbr>


public class VectorDemo{
<wbr>public static void main(String[] args){<br><wbr><wbr><br><wbr><wbr>//<strong style="background-color:#a0ffff; color:black">Vector</strong>的创建<br><wbr><wbr>//使用<strong style="background-color:#a0ffff; color:black">Vector</strong>的构造方法进行创建<br><wbr><wbr><strong style="background-color:#a0ffff; color:black">Vector</strong> v = new <strong style="background-color:#a0ffff; color:black">Vector</strong>(4);<br><wbr><wbr><br><wbr><wbr>//向<strong style="background-color:#a0ffff; color:black">Vector</strong>中添加元素<br><wbr><wbr>//使用add方法直接添加元素<br><wbr><wbr>v.add("Test0");<br><wbr><wbr>v.add("Test1");<br><wbr><wbr>v.add("Test0");<br><wbr><wbr>v.add("Test2");<br><wbr><wbr>v.add("Test2");<br><wbr><wbr><br><wbr><wbr>//从<strong style="background-color:#a0ffff; color:black">Vector</strong>中删除元素<br><wbr><wbr>v.remove("Test0"); //删除指定内容的元素<br><wbr><wbr>v.remove(0);<wbr><wbr><wbr><wbr><wbr><wbr> //按照索引号删除元素<br><wbr><wbr><br><wbr><wbr>//获得<strong style="background-color:#a0ffff; color:black">Vector</strong>中已有元素的个数<br><wbr><wbr>int size = v.size();<br><wbr><wbr>System.out.println("size:" + size);<br><wbr><wbr><br><wbr><wbr>//遍历<strong style="background-color:#a0ffff; color:black">Vector</strong>中的元素<br><wbr><wbr>for(int i = 0;i &lt; v.size();i++){<br><wbr><wbr><wbr>System.out.println(v.get(i));<br><wbr><wbr>}<br><wbr>}<br> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

-------------

Vector 类提供了实现可增长数组的功能,随着更多元素加入其中,数组变的更大。在删除一些元素之后,数组变小。
Vector 有三个构造函数,
public Vector(int initialCapacity,int capacityIncrement)
         public Vector(int initialCapacity)
         public Vector()
  Vector 运行时创建一个初始的存储容量initialCapacity,存储容量以capacityIncrement 变量定义的增量增长。初始的存储容量和capacityIncrement 可以在Vector 的构造函数中定义。第二个构造函数只创建初始存储容量。第三个构造函数既不指定初始的存储容量也不指定capacityIncrement。
  Vector 类提供的访问方法支持类似数组运算和与Vector 大小相关的运算。类似数组的运算允许向量中增加,删除和插入元素。它们也允许测试矢量的内容和检索指定的元素,与大小相关的运算允许判定字节大小和矢量中元素不数目。
  现针对经常用到的对向量增,删,插功能举例描述:
addElement(Object obj)  
  把组件加到向量尾部,同时大小加1,向量容量比以前大1
 
insertElementAt(Object obj, int index)  
  把组件加到所定索引处,此后的内容向后移动1 个单位
 
setElementAt(Object obj, int index)
  把组件加到所定索引处,此处的内容被代替。
  removeElement(Object obj) 把向量中含有本组件内容移走。
  removeAllElements() 把向量中所有组件移走,向量大小为0。
  例如:
 
     import java.lang.System;
     import java.util.Vector;
     import java.util.Emumeration;
     public class Avector{
                 public static void main(String args[])
                    {
0.   Vector v=new Vector();
1. v.addElement("one");
2. addElement("two");
3. v.addElement("three");
4. v.insertElementAt("zero",0);
5. v.insertElementAt("oop",3);
6. v.setElementAt("three",3);
7. v.setElementAt("four",4);
8. v.removeAllElements();
}
}
Vector中的变化情况:
1. one   2. one   3. one   4. zero   5.zero   6. zero  7. zero<wbr><wbr><wbr><wbr> 8.<br><br>      <wbr><wbr><wbr> two  <wbr><wbr><wbr><wbr><wbr><wbr> two <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> one  <wbr><wbr><wbr><wbr> one  <wbr><wbr><wbr><wbr><wbr><wbr><wbr> one  <wbr><wbr><wbr><wbr>one<br>            three  <wbr><wbr><wbr><wbr> two  <wbr><wbr><wbr><wbr> two  <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> two   two<br>                 three  <wbr><wbr><wbr> oop  <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> three  three<br>                      three  <wbr><wbr><wbr><wbr><wbr><wbr><wbr> three  four<br>  <br>   另外,<strong style="background-color:#a0ffff; color:black">Vector</strong> 在参数传递中发挥着举足轻重的作用。<br>   在Applet 中有一块画布(Canvas) 和一个(Panel), 而Panel 中放着用户要输入的信息,根据这些信息把参数传递到canvas 中,这时在<strong style="background-color:#ffff66; color:black">Java</strong> 中用一个接口(Interface), 而在接口中需用一个<strong style="background-color:#a0ffff; color:black">Vector</strong> 去传递这些参数。另外,在一个类向另一个类参数传递就可以用这种方法。<br>   例如:<br>  <br> import <strong style="background-color:#ffff66; color:black">java</strong>.util.<strong style="background-color:#a0ffff; color:black">Vector</strong><br> interface codeselect{<br>            <strong style="background-color:#a0ffff; color:black">Vector</strong> codeselect=new <strong style="background-color:#a0ffff; color:black">Vector</strong>();<br>              }<br> 显示数学信息<br><strong style="background-color:#a0ffff; color:black">Vector</strong>(0)存入学生编号<br><strong style="background-color:#a0ffff; color:black">Vector</strong>(1)存入学科<br>  <br>   在Panel 中当用户在TextField 和Choice 中选择自己所要求的内容,程序中<br>   通过事件响应把值传到向量<strong style="background-color:#a0ffff; color:black">Vector</strong> 中。<br>   假若在Panel 类中:<br>  <br> public void  codepanel extends Panel{<br>   public void init()<br>   {<br>     **.<br> TextField  s=new TextField();<br> Choice c=new Choice();<br> c. addItem("语文");<br> c.addItem("数学");<br> c.addItem("政治");<br> add(s);<br> add (c);<br> **<br> }<br>  <br> public boolean handleEvent(Event event){<br> if(event.id==Event.ACTION_EVENT){<br> if(event.target.instanceof Textfield)<br> {<br> coderesult.setElementAt(s.getText(),0);<br> }<br> else if(event.target intanceof Choice)<br> {<br> coderesult.setElementAt(new Integer(c.getSelectedIndex()),1);<br> }<br> }<br> }<br> }<br>  <br>  <br>  <br>   这时,向量中已经存入学生编号和学科索引号(0 为语文,1 为数学,2 为政治)。<br>   而在Canvas 中得到此值,<br>  <br> public class codecanvas extends Canvas{<br>  <br> public void code{<br>             }<br> public void paint{<br>  <br> String str;<br> int t;<br> str=(String)coderesult.elementAt(0);<br> t=(new Integer(codeselect.elementAt(1).toString())).intValue();<br> if(t==0)<br> {<br> 显示语文信息<br> }<br> else if(t==1)<br> {<br> 显示数学信息<br>  <br> }<br> else if(t==2)<br> {<br> 显示政治信息<br> }<br> }<br> } </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics