一、常用的泛型类
1.ArrayList类
package try_a_package;
import java.util.ArrayList;
class ArrayListTest {
public static void main(String[] args)
{
ArrayList list1=new ArrayList();
list1.add("我是一个");
list1.add("憨憨");
list1.add(1);
boolean flag1=list1.contains("憨憨");
if(flag1)
System.out.println("我是憨憨");
else
System.out.println("我才不是憨憨呢!");
System.out.println(list1.get(0));
System.out.println(list1.size());
System.out.println(list1.set(0, "我是两个"));
for(Object a:list1)
{
System.out.println(a);
}
// 这里像删除的元素,而不少索引,所以增加了Object的类型转换来实现这个操作。
list1.remove((Object)1);
// 如果不加Object的话,会直接移除索引处的元素
// 如果索引处的元素,不存在,如下面这个删除Object后的情形
// 则会抛出越界的异常
list1.remove((Object)2);
for(int i=0;i<list1.size();++i)
{
Object o=list1.get(i);
System.out.println(o);
}
System.out.println(list1.isEmpty());
System.out.println(list1.indexOf("憨憨"));
list1.clear();
System.out.println(list1.isEmpty());
}
}
2.LinkedList类
package try_a_package;
import java.util.LinkedList;
public class LinkedListTest {
public static void main(String[] args) {
// 构造
LinkedList list = new LinkedList();
// 插入元素
list.add(4);
list.add("String");
list.add(1, 5);
list.addFirst(1);
list.addLast(8);
// 读取指定元素
System.out.println(list.getFirst());
System.out.println(list.getLast());
System.out.println(list.get(2));
int temp = list.indexOf(5);
System.out.println(temp);
temp = list.lastIndexOf(5);
System.out.println(temp);
// 注意,要进行强制类型
temp = (int) list.peek();
System.out.println(temp);
// 移除元素
temp = (int) list.poll();
System.out.println(temp);
temp = (int) list.remove();
System.out.println(temp);
temp = (int) list.get(0);
System.out.println(temp);
String str = (String) list.get(1);
System.out.println(str);
str = (String) list.remove(1);
System.out.println(str);
boolean flag = list.remove((Object) 5);
System.out.println(flag);
flag = list.remove((Object) 5);
System.out.println(flag);
temp = (int) list.removeFirst();
System.out.println(temp);
if (!list.isEmpty())
temp = (int) list.removeLast();
System.out.println(temp);
}
}
3.Stack类
package try_a_package;
import java.util.Stack;
import java.util.Scanner;
public class StackTest {
public static void main(String[] args)
{
Stack stack=new Stack();
Scanner sc=new Scanner(System.in,"gbk");
stack.push(1);
stack.push(2);
stack.push(5);
while(!stack.isEmpty())
{
System.out.println(stack.pop());
}
sc.close();
}
}
4.HashSet类
package try_a_package;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
public class HashSetTest {
// 输出集合中的元素
public static void print(HashSet a)
{
System.out.print("{");
Iterator it=a.iterator();
int c=0;
while(it.hasNext())
{
int t=(int)it.next();
if(c==0)
{
System.out.print(t);
}
else
{
System.out.print(","+t);
}
c++;
}
System.out.println("}");
}
// 合并两个集合
public static HashSet interSection(HashSet a,HashSet b)
{
HashSet c=new HashSet();
Iterator it=a.iterator();
while(it.hasNext())
{
int t=(int)it.next();
if(b.contains(t))
{
c.add(t);
}
}
return c;
}
// 求并集
public static HashSet unionSection(HashSet a,HashSet b)
{
HashSet c=new HashSet(a);
c.addAll(b);
return c;
}
// 随机生成元素,构成集合
public static HashSet getSet(int num,int n)
{
HashSet h=new HashSet();
Random rd=new Random();
for(int i=0;i<n;++i)
{
int now_rd=rd.nextInt(num);
h.add(now_rd);
}
return h;
}
// 主函数
public static void main(String[] args)
{
HashSet a,b,c,d;
a=getSet(10,6);
b=getSet(10,6);
print(a);
print(b);
c=interSection(a,b);
d=unionSection(a,b);
print(c);
print(d);
}
}
5.TreeSet
package try_a_package;
import java.util.Collection;
import java.util.Iterator;
import java.util.Random;
import java.util.TreeSet;
public class TreeSetTest {
// 输出集合中的所有元素(适用于List,Queue,Set)
public static void printSet(Collection a)
{
System.out.print("(");
Iterator it=a.iterator();
for(int i=0;it.hasNext();++i)
{
int t=(int)it.next();
if(i==0)
{
System.out.print(t);
}
else
{
System.out.print(","+t);
}
}
System.out.println("}");
}
// 输出ENode构成的集合中的元素
public static void print(TreeSet<ENode> ts)
{
System.out.println("输出集合中的元素:");
System.out.println("{");
Iterator it=ts.iterator();
while(it.hasNext())
{
ENode node=(ENode)it.next();
System.out.println(node);
}
System.out.println("}\n");
}
// 随机产生由n个元素构成的集合
public static TreeSet getTreeSet(int num,int n)
{
TreeSet h=new TreeSet();
Random rd=new Random();
for(int i=0;i<n;++i)
{
int tmp=rd.nextInt(num);
h.add(tmp);
}
return h;
}
public static TreeSet<ENode> getDecare(TreeSet a,TreeSet b)
{
TreeSet<ENode> ts=new TreeSet<>();
Iterator ita;
ita=a.iterator();
while(ita.hasNext())
{
int tmpa=(int)ita.next();
Iterator itb=b.iterator();
while(itb.hasNext())
{
int tmpb=(int) itb.next();
ENode node=new ENode(tmpa,tmpb);
ts.add(node);
}
}
return ts;
}
// 主函数
public static void main(String[] args)
{
TreeSet a,b;
a=getTreeSet(10,3);
b=getTreeSet(10,2);
printSet(a);
printSet(b);
TreeSet<ENode> c=getDecare(a,b);
print(c);
}
}