现在的位置: 首页 > 综合 > 正文

常用的集合元素使用范例代码

2013年10月08日 ⁄ 综合 ⁄ 共 3598字 ⁄ 字号 评论关闭
import java.util.Vector;

/**
* Created by IntelliJ IDEA.
* User: LG.gao
* Date: 2005-8-5
* Time: 23:13:33
* To change this template use File | Settings | File Templates.
*/
public class TestVector {
/**
* @param Vector 向量Vector不要求每个元素的类型相同,向量中可以混合多种数据类型。
* @param Vector 向量可以在增加元素时动态增大。
* @param args
*/
public static void main(String[] args) {
String names[] = {"aaa", "bbb", "ccc", "ddd"};
Vector planets = new Vector();
for (int i = 0; i < names.length; i++) {
planets.addElement(names[i]);
}
for (int i = 0; i < planets.size(); i++) {
System.out.println(planets.elementAt(i));
}
}
}
//---------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.List;

/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2005-8-6
* Time: 1:11:06
* To change this template use File | Settings | File Templates.
*/
public class TestList {
/**
* @param List 接口是一种可含有重复元素的,有序的收集,也称序列,用户可以控制序列中
* @param List 元素插入的位置,可以排序。
*/
public static void main(String[] args) {
ArrayList h = new ArrayList();
h.add("1st");
h.add("2nd");
h.add("2nd");
h.add(new Integer(3));
h.add(new Double(4.0));
mi(h);
}

public static void mi(List i) {
System.out.println(i);
}
}
//--------------------------------------------------------------------------------
import java.util.HashSet;
import java.util.Set;

/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2005-8-6
* Time: 1:01:54
* To change this template use File | Settings | File Templates.
*/
public class TestSet {
/**
* @param Set 集合是由不重复的元素组成的,Set集合接口中的元素不重复,且至多包含一个null元素。
* @param args
*/
public static void main(String[] args) {
HashSet h = new HashSet();
h.add("aaa");
h.add("2nd");
h.add(new Integer(3));
h.add(new Double(4.0));
h.add("2nd");//重复元素,未被加入
h.add(new Integer(3));
mi(h);
}

/**
* @param s
*/
public static void mi(Set s) {
System.out.println("s:" + s);
}
}
//---------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.Iterator;

/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2005-8-6
* Time: 1:18:15
* To change this template use File | Settings | File Templates.
*/
public class TestIterator {
/**
* @param Iterator 通过Collection接口定义的iterator()方法获得一个iterator对象。
* @param Set 对象对应的iterator仍然是无序的。
* @param args
*/
public static void main(String[] args) {
ArrayList h = new ArrayList();
h.add("1st");
h.add("2nd");
h.add("2nd");
h.add(new Integer(3));
h.add(new Double(4.0));
Iterator it = h.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
//---------------------------------------------------------------------------------
import java.util.Hashtable;
import java.util.Enumeration;

/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2005-8-6
* Time: 0:47:23
* To change this template use File | Settings | File Templates.
*/
public class TestHashtable {
/**
* @param Hashtable 数组向量提供了集合内容的访问,哈西表可以对集合内容进行随机访问。
* @param args
*/
public static void main(String[] args) {
String names[] = {"aaa", "bbb", "ccc", "ddd"};
String name[] = {"东", "南", "西", "北"};
Hashtable hash = new Hashtable();
for (int i = 0; i < names.length; i++) {
hash.put(names[i], name[i]);
}
Enumeration enum = hash.keys();
Object obj;
while (enum.hasMoreElements()) {
obj = enum.nextElement();
System.out.println(obj + ":" + hash.get(obj));
}
}
}
//---------------------------------------------------------------------------------
import java.util.Vector;
import java.util.Enumeration;

/**
* Created by IntelliJ IDEA.
* User: LG.gao
* Date: 2005-8-6
* Time: 0:34:08
* To change this template use File | Settings | File Templates.
*/
public class TestEnumeration {
/**
* @param Enumeration 枚举也可以存放许多元素,两个常用的方法,
* @param Enumeration hasMoreElements()和nextElement()
* @param args
*/
public static void main(String[] args) {
String names[] = {"aaa", "bbb", "ccc", "ddd"};
Vector planets = new Vector();
for (int i = 0; i < names.length; i++) {
planets.addElement(names[i]);
}
Enumeration enum = planets.elements();
String name = null;
while (enum.hasMoreElements()) {
System.out.println((String) enum.nextElement());
}
}
}

抱歉!评论已关闭.