码迷,mamicode.com
首页 > 编程语言 > 详细

java 动态数组

时间:2019-10-19 18:54:00      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:int   ext   isp   nta   ini   import   oat   elements   spl   

 

package testjavapro;
import java.util.*;

public class testjava {

   public static void main(String args[]) {
      // initial size is 3, Capacity increment is 2
      Vector<Number> v = new Vector<Number>(3, 2);
      System.out.println("Initial size: " + v.size());
      System.out.println("Initial capacity: " +
      v.capacity());
      v.addElement(new Integer(1));
      v.addElement(new Integer(2));
      v.addElement(new Integer(3));
      v.addElement(new Integer(4));
      System.out.println("Capacity after four additions: " +
          v.capacity());

      v.addElement(new Double(5.45));
      System.out.println("Current capacity: " +
      v.capacity());
      v.addElement(new Double(6.08));
      v.addElement(new Integer(7));
      System.out.println("Current capacity: " +
      v.capacity());
      v.addElement(new Float(9.4));
      v.addElement(new Integer(10));
      System.out.println("Current capacity: " +
      v.capacity());
      v.addElement(new Integer(11));
      v.addElement(new Integer(12));
      System.out.println("First element: " +
         (Integer)v.firstElement());
      System.out.println("Last element: " +
         (Integer)v.lastElement());
      if(v.contains(new Integer(3)))
         System.out.println("Vector contains 3.");
      // enumerate the elements in the vector.
      Enumeration<Number> vEnum = v.elements();
      System.out.println("\nElements in vector:"+v.size());
      while(vEnum.hasMoreElements())
         System.out.print(vEnum.nextElement() + " ");
      System.out.println();
   }
}

输出

Initial size: 0
Initial capacity: 3
Capacity after four additions: 5
Current capacity: 5
Current capacity: 7
Current capacity: 9
First element: 1
Last element: 12
Vector contains 3.

Elements in vector:11
1 2 3 4 5.45 6.08 7 9.4 10 11 12 

 

 

 

 

 

java 动态数组

标签:int   ext   isp   nta   ini   import   oat   elements   spl   

原文地址:https://www.cnblogs.com/sea-stream/p/11704767.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!