教程集 www.jiaochengji.com
教程集 >  脚本编程  >  java  >  正文 java中Vector及与List区别

java中Vector及与List区别

发布时间:2017-12-02   编辑:jiaochengji.com
教程集为您提供java中Vector及与List区别等资源,欢迎您收藏本站,我们将为您提供最新的java中Vector及与List区别资源
本文章先是介绍了枚举就是Vector特有的取出方式,枚举和迭代一样,然后再介绍了关于vector与list区别总结。

Vector用法

枚举就是Vector特有的取出方式,枚举和迭代一样。
枚举名称以及方法的名称过长。所以,被迭代器替换。
取出集合元素的方法有三种:

迭代,遍历,for循环
使用枚举:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4042')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4042>

package com.day14.wd;


import java.util.Enumeration;
import java.util.Vector;

public class VectorDemo {
    public static void main(String[] args){
        Vector  vec=new Vector();
        vec.add("java01");
        vec.add("java02");
        vec.add("java03");
    Enumeration enu=vec.elements();
     while (enu.hasMoreElements()) {
        Object object = (Object) enu.nextElement();
         System.out.println(object);
       
    }
    }

}

list用法

 List接口定义的常用方法及功能
从表1可以看出,List接口提供的适合于自身的常用方法均与索引有关,这是因为List集合为列表类型,以线性方式存储对象,可以通过对象的索引操作对象。
List接口的常用实现类有ArrayList和LinkedList,在使用List集合时,通常情况下声明为List类型,实例化时根据实际情况的需要,实例化为ArrayList或LinkedList,例如:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3695')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3695>List<String> l = new ArrayList<String>();// 利用ArrayList类实例化List集合
List<String> l2 = new LinkedList<String>();// 利用LinkedList类实例化List集合

1.add(int index, Object obj)方法和set(int index, Object obj)方法的区别
在使用List集合时需要注意区分add(int index, Object obj)方法和set(int index, Object obj)方法,前者是向指定索引位置添加对象,而后者是修改指定索引位置的对象,例如执行下面的代码:
srccommwqTestCollection.java关键代码:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2958')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2958>public static void main(String[] args) {
String a = "A", b = "B", c = "C", d = "D", e = "E";
List<String> list = new LinkedList<String>();
list.add(a);
list.add(e);
list.add(d);
list.set(1, b);// 将索引位置为1的对象e修改为对象b
list.add(2, c);// 将对象c添加到索引位置为2的位置
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
在控制台将输出如下信息:
A
B
C
D

因为List集合可以通过索引位置访问对象,所以还可以通过for循环遍历List集合,例如遍历上面代码中的List集合的代码如下:
srccommwqTestCollection.java关键代码:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3623')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3623>for (int i = 0; i < list.size(); i ) {
System.out.println(list.get(i));// 利用get(int index)方法获得指定索引位置的对象
}
srccommwqTestCollection.java完整代码如下:
package com.mwq;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.List;
public class TestCollection {
public static void main(String[] args) {
System.out.println("开始:");
String a = "A", b = "B", c = "C", d = "D", e = "E";
List<String> list = new LinkedList<String>();
list.add(a);
list.add(e);
list.add(d);
list.set(1, b);// 将索引位置为1的对象e修改为对象b
list.add(2, c);// 将对象c添加到索引位置为2的位置
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
//                 for (int i = 0; i < list.size(); i ) {
//                       System.out.println(list.get(i));// 利用get(int index)方法获得指定索引位置的对象
//          }
System.out.println("结束!");
}
}

Vector和List的区别

如果你注意到对Vector和List的所开始支持的Java版本你就应该可以找到答案了。Java对Vector的支持since 1.0;对List则是since 1.2。这两个版本之间,sun对于java api做了很多的改动,其中的一个refactoring就是提出了所谓的Collection FrameWork,List就是在那个时候被introduced,它完全符合1.2版本的collection framework,而Vector则是在Colleciton framework出现之前就已经存在了,但java api并没有将Vector变成Deprecated,主要是backward compatiable的问题,最终JCP将vector做了refactoring的处理,让它符合所定制的Collection framework了事。另外,Hashtable和HashMap的区别是同样的道理。

结论:尽量采用List和HashMap,rather than Vector&Hashtable.

您可能感兴趣的文章:
java中Vector及与List区别
java list ArrayList用法详细
Vector在Java编程中的应用
Java中Collection遍历中删除、合并元素
Java的数据库应用
C#与C 数据类型对比基本语法区别
.net、php、java的区别是什么?
用C 实现python字符串分割函数 split()与rsplit()实例
Java入门笔记3_Datastructure
canvas与svg的区别有什么?canvas和svg的区别比较

[关闭]
~ ~