一、常用的泛型類
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);
}
list1.remove((Object)1);
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 {
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("}");
}
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");
}
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);
}
}