Java学习之路(9)----数据结构(向量)

文章目录

Java Vector 类

Vector
该类和ArrayList非常相似,但是该类是同步的,可以用在多线程的情况,该类允许设置默认的增长长度==,默认扩容方式为原来的2倍==。

  • Vector 类实现了一个动态数组。和 ArrayList 很相似,但是两者是不同的:
  • Vector 是同步访问的。
  • Vector 包含了许多传统的方法,这些方法不属于集合框架。
  • Vector 主要用在事先不知道数组的大小,或者只是需要一个可以改变大小的数组的情况。
  • Vector 类支持 4 种构造方法。

第一种构造方法创建一个默认的向量,默认大小为 10:

Vector()

第二种构造方法创建指定大小的向量。

Vector(int size)

第三种构造方法创建指定大小的向量,并且增量用 incr 指定。增量表示向量每次增加的元素数目。

Vector(int size,int incr)

第四种构造方法创建一个包含集合 c 元素的向量:

Vector(Collection c)
  • void add(int index, Object element)
    在此向量的指定位置插入指定的元素。

  • boolean add(Object o)
    将指定元素添加到此向量的末尾。

  • boolean addAll(Collection c)
    将指定 Collection 中的所有元素添加到此向量的末尾,按照指定 collection 的迭代器所返回的顺序添加这些元素。

  • boolean addAll(int index, Collection c)
    在指定位置将指定 Collection 中的所有元素插入到此向量中。

  • void addElement(Object obj)
    将指定的组件添加到此向量的末尾,将其大小增加 1。

  • int capacity()
    返回此向量的当前容量。


import java.util.*;

public class VectorTest{
	
	
	public static void main(String[] args){
		//默认值测试
		
		Vector v1 = new Vector();
		
		System.out.println(v1);//[]
		System.out.println(v1.capacity());//返回此向量的当前容量。
		System.out.println(v1.isEmpty());
		System.out.println(v1.size());//0返回此向量中的组件数。
		//System.out.println(v1.subList(1,8));运行出错
		//System.out.println(v1.get(1));//运行出错,返回向量中指定位置的元素。
		System.out.println("----v2-----");
		Vector v2 = new Vector(4);
		System.out.println("v2.size:"+v2.size());
		System.out.println("v2.capacity():"+v2.capacity());
		v2.add("net");
		// v1.addElement(1);
		System.out.println("v2.add(1):"+v2);
		// System.out.println("v1.addElement(1):"+v1);
		
		
	}
}

出现警告

E:\java_code\code>javac VectorTest.java
注: VectorTest.java使用了未经检查或不安全的操作。
注: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。

解决:

1、无视警告

2、采用Java泛型

以下程序用于更好理解向量的使用

import java.util.*;

 
public class VectorTest{
	
	
	public static void main(String[] args){
		//默认值测试
		
		Vector<Integer> v1 = new Vector<Integer>();//Java泛型
		//Vector<String> v2 = new Vector<String>(3,2);
		
		System.out.println(v1);//[]
		System.out.println(v1.capacity());//返回此向量的当前容量。
		System.out.println(v1.isEmpty());
		System.out.println(v1.size());//0返回此向量中的组件数。
		//System.out.println(v1.subList(1,8));运行出错
		//System.out.println(v1.get(1));//运行出错,返回向量中指定位置的元素。
		v1.add(1);
		System.out.println("v1.add(1):"+v1);
		
		System.out.println("----v2-----");
		Vector v2 = new Vector(3,2);
		System.out.println("v2.size:"+v2.size());//0
		System.out.println("v2.capacity():"+v2.capacity());//3
		
		v2.addElement(new Integer(1));
		//v2.addElement(new Float(1.3));
		System.out.println("v2.addElement(new Integer(1)):"+v2);//[1]
		System.out.println("v2.size:"+v2.size());//1
		System.out.println("v2.capacity():"+v2.capacity());//3
		System.out.println("----v2-----");
		v2.addElement(new Integer(2));
		v2.addElement(new Integer(3));
		
		System.out.println("v2.size:"+v2.size());//3
		System.out.println("v2.capacity():"+v2.capacity());//3
		
		System.out.println("----v2integer-----");
		v2.addElement(new Integer(10));
		System.out.println("v2.size:"+v2.size());//4
		System.out.println("v2.capacity():"+v2.capacity());//5
		
		System.out.println("----v2float-----");
		v2.addElement(new Float(10.3));
		System.out.println("v2.size:"+v2.size());//5
		System.out.println("v2.capacity():"+v2.capacity());//5
		
		System.out.println("----v2double-----");
		v2.addElement(new Double(10.25));
		System.out.println("v2.size:"+v2.size());//6
		System.out.println("v2.capacity():"+v2.capacity());//7
		
		System.out.println("----v2string-----");
		v2.addElement(new String("11"));
		System.out.println("v2.size:"+v2.size());//7
		System.out.println("v2.capacity():"+v2.capacity());//7
		System.out.println(v2);//[1,2,3,10,10.3,10.25,"11"]
		//System.out.println((Integer)v2.firstElement());//当第一个为Double、Float执行会出错,反之也不可以
		System.out.println(v2.get(5));//[10.25]
		System.out.println(v2.lastElement());//["11"]
		
		System.out.println(v2.contains(11));//false
		System.out.println(v2.contains("11"));//true
		
		
		System.out.println("----v2数组-----");
		System.out.println(v2.toArray());//[1,2,3,10,10.0,10.0,"11"]11是字符串
	
		
		
		System.out.println(v2.toString());//"1","2","3","10","10.3","10.25","11"
		
		Enumeration<Integer> e = v2.elements();
		System.out.println("----v2 enumeration-----");
		
		while(e.hasMoreElements()){
			
			System.out.print(e.nextElement()+" ");//1,2,3,10,10.3,10.25,11
		}
		
		
		
		
		
		
		
		
	}
}

stem.out.println("----v2 enumeration-----");

	while(e.hasMoreElements()){
		
		System.out.print(e.nextElement()+" ");//1,2,3,10,10.3,10.25,11
	}
	
	
	
	
	
	
	
	
}

}

发布了53 篇原创文章 · 获赞 4 · 访问量 1317

猜你喜欢

转载自blog.csdn.net/weixin_43351473/article/details/104425921
今日推荐