当前位置:网站首页 > Java基础 > 正文

java自动增长教程



VectorApp.Java [java] view plain copy print? import java.util.Vector; import java.lang.*; import java.util.Enumeration; public class VectorApp { public static void main(String args[]) { Vector v1 = new Vector(); Integer integer1= new Integer(1); //加入为字符串对象  v1.addElement(“one”); //加入的为integer的对象  v1.addElement(integer1); v1.addElement(integer1); v1.addElement(“two”); v1.addElement(new Integer(2)); v1.addElement(integer1); v1.addElement(integer1); //转为字符串并打印  System.out.println(“The Vector v1 is:

"+<span style="color: rgba(0, 0, 0, 1)">v1); 

//向指定位置插入新对象 v1.insertElementAt(“three”,2); v1.insertElementAt(new Float(3.9),3); System.out.println(“The Vector v1(used method insertElementAt()is:

)"+v1); 

//将指定位置的对象设置为新的对象 //指定位置后的对象依次往后顺延 v1.setElementAt(“four”,2); System.out.println(“The vector v1 cused method setElmentAt()is:

"+<span style="color: rgba(0, 0, 0, 1)">v1); 

v1.removeElement(integer1); //从向量对象v1中删除对象integer1 //由于存在多个integer1,所以从头开始。 //找删除找到的第一个integer1. Enumeration enum = v1.elements(); System.out.println(“The vector v1 (used method removeElememt()is”); while(enum.hasMoreElements()) System.out.println(enum.nextElement()+“”); System.out.println(); //使用枚举类(Enumeration)的方法取得向量对象的每个元素。 System.out.println(“The position of Object1(top-to-botton):”+v1.indexOf(integer1)); System.out.println(“The position of Object1(tottom-to-top):”+v1.lastIndexOf(integer1)); //按不同的方向查找对象integer1所处的位置 v1.setSize(4); System.out.println(“The new Vector(resized the vector)is:”+v1); //重新设置v1的大小,多余的元素被抛弃 } }

运行结果: E:java01&gt;java VectorApp The vector v1 is:[one,1,1,two,2,1,1] The vector v1(used method insetElementAt()) is: [one,1,three,3.9,1,two,2,1,1] The vector v1(used method setElementAt()) is: [one,1,four,3.9,1,two,2,1,1] The vector v1(useed method removeElement()) is: one four 3.9 1 two 2 1 1 The position of object1(top-to-botton):3 The position of object1(botton-to-top):7 The new Vector(resized the vector) is: [one,four,3.9,1]

  • 上一篇: java工作流activity教程
  • 下一篇: java202教程
  • 版权声明


    相关文章:

  • java工作流activity教程2026-02-18 22:02:05
  • java14教程2026-02-18 22:02:05
  • 遵义java语言教程2026-02-18 22:02:05
  • java357集教程2026-02-18 22:02:05
  • 坦克大战 java教程2026-02-18 22:02:05
  • java202教程2026-02-18 22:02:05
  • java 加密 教程2026-02-18 22:02:05
  • java 破解 教程2026-02-18 22:02:05
  • 毕向东java全套教程视频2026-02-18 22:02:05
  • java网站搭建教程2026-02-18 22:02:05