集合类处理范例
来源:互联网 发布:2016时时彩系统源码 编辑:程序博客网 时间:2024/04/29 11:07
范例一:打印ArrayList中的元素
import java.util.*;
public class ArrayListTest
{
public static void main(String args[])
{
ArrayList al = new ArrayList();
al.add("zhangsan");
al.add("lisi");
al.add("wangwu");
for(int i=0;i<al.size();i++)//使用for循环打印出al中的所有元素
{
System.out.println(al.get(i));
}
System.out.println(al);//调用ArrayList的toString()方法打印出al中的元素
}
}
范例二:想ArrayList中添加自定义对象,并打印出来
import java.util.*;
public class ArrayListTest
{
public static void main(String args[])
{
ArrayList al = new ArrayList();
al.add(new Point(3,3));
al.add(new Point(2,2));
al.add(new Point(4,4));
System.out.println(al);
}
}
class Point
{
int x,y;
Point(int x,int y)
{
this.x = x;
this.y = y;
}
public String toString()
{
return "x="+x+",y="+y;
}
}
范例三:ArrayList的toArray()方法
import java.util.*;
public class ArrayListTest
{
public static void main(String args[])
{
ArrayList al = new ArrayList();
al.add(new Point(3,3));
al.add(new Point(2,2));
al.add(new Point(4,4));
Object[] obj = al.toArray();
for(int i=0;i<obj.length;i++)
{
System.out.println(obj[i]);
}
}
}
class Point
{
int x,y;
Point(int x,int y)
{
this.x = x;
this.y = y;
}
public String toString()
{
return "x="+x+",y="+y;
}
}
范例四:Arrays.asList()方法的用法范例,Arrays.asList()方法返回一个列表
import java.util.*;
public class ArrayListTest
{
public static void main(String args[])
{
ArrayList al = new ArrayList();
al.add(new Point(3,3));
al.add(new Point(2,2));
al.add(new Point(4,4));
Object[] obj = al.toArray();
List l=Arrays.asList(obj);
System.out.println(l);
//l.add(new Point(33,33));//error!not to add elements to this list;
l.set(2,new Point(5,5));//right! can modify element
System.out.println(l);
}
}
class Point
{
int x,y;
Point(int x,int y)
{
this.x = x;
this.y = y;
}
public String toString()
{
return "x="+x+",y="+y;
}
}
范例五:迭代器的用法范例,Iterator;
import java.util.*;
public class ArrayListTest
{
public static void main(String args[])
{
ArrayList al = new ArrayList();
al.add(new Point(3,3));
al.add(new Point(2,2));
al.add(new Point(4,4));
Iterator it = al.iterator();
it.next();
it.remove();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
class Point
{
int x,y;
Point(int x,int y)
{
this.x = x;
this.y = y;
}
public String toString()
{
return "x="+x+",y="+y;
}
}
范例六:迭代器的用法范例
import java.util.*;
public class ArrayListTest
{
public static void printElements(Collection c)
{
Iterator it = c.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
public static void main(String args[])
{
ArrayList al = new ArrayList();
al.add(new Point(3,3));
al.add(new Point(2,2));
al.add(new Point(4,4));
ArrayListTest.printElements(al);
}
}
class Point
{
int x,y;
Point(int x,int y)
{
this.x = x;
this.y = y;
}
public String toString()
{
return "x="+x+",y="+y;
}
}
范例七,Collections类中的sort()和reverseOrder()用法范例
import java.util.*;
public class ArrayListTest
{
public static void printElements(Collection c)
{
Iterator it = c.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
public static void main(String args[])
{
ArrayList al = new ArrayList();
Student s1 = new Student(10,"zhangsan");
Student s2 = new Student(10,"lis");
Student s3 = new Student(17,"wangwu");
al.add(s1);
al.add(s2);
al.add(s3);
Collections.sort(al);
ArrayListTest.printElements(al);
System.out.println("=======================");
Collections.sort(al,new Student.StudentComparator());
ArrayListTest.printElements(al);
System.out.println("====================");
Collections.sort(al,Collections.reverseOrder());
ArrayListTest.printElements(al);
}
}
class Student implements Comparable
{
int age;
String name;
static class StudentComparator implements Comparator
{
public int compare(Object obj1,Object obj2)
{
Student s1 = (Student)obj1;
Student s2 = (Student)obj2;
int result = s1.age>s2.age ? 1:(s1.age==s2.age ? 0:-1);
if(result==0)
{
result = s1.name.compareTo(s2.name);
}
return result;
}
}
Student(int age,String name)
{
this.age = age;
this.name = name;
}
public int compareTo(Object o)
{
Student s = (Student)o;
return age > s.age ? 1 : (age == s.age ? 0 : -1);
}
public String toString()
{
return name+":"+age;
}
}
范例八:利用LinkedList实现stack
栈(Stack)也是一种特殊的线性表,是一种后进先出(LIFO)的结构。
栈是限定仅在表尾进行插入和删除运算的线性表,表尾称为栈顶(top),表头称为栈底(bottom)。
栈的物理存储可以用顺序存储结构,也可以用链式存储结构。
import java.util.*;
public class StackTest
{
private LinkedList l1 = new LinkedList();
public void push(Object o)
{
l1.addFirst(o);
}
public Object peek()
{
return l1.getFirst();
}
public Object pop()
{
return l1.removeFirst();
}
public boolean isEmpty()
{
return l1.isEmpty();
}
public static void main(String[] args)
{
StackTest st = new StackTest();
st.push("one");
st.push("two");
st.push("three");
System.out.println(st.pop());
System.out.println(st.peek());
System.out.println(st.pop());
System.out.println(st.isEmpty());
System.out.println(st.pop());
System.out.println(st.isEmpty());
}
}
/*运行结果:
three
two
two
false
one
true
Press any key to continue...
*/
范例九:利用LinkedList实现Quee
队列(Queue)是限定所有的插入只能在表的一端进行,而所有的删除都在表的另一端进行的线性表。
表中允许插入的一端称为队尾(Rear),允许删除的一端称为队头(Front)。
队列的操作是按先进先出(FIFO)的原则进行的。
队列的物理存储可以用顺序存储结构,也可以用链式存储结构。
import java.util.*;
public class QueueTest
{
private LinkedList l1 = new LinkedList();
public void put(Object o)
{
l1.addLast(o);
}
public Object get()
{
return l1.removeFirst();
}
public boolean isEmpty()
{
return l1.isEmpty();
}
public static void main(String[] args)
{
QueueTest qt = new QueueTest();
qt.put("one");
qt.put("two");
qt.put("three");
System.out.println(qt.get());
System.out.println(qt.get());
System.out.println(qt.isEmpty());
System.out.println(qt.get());
System.out.println(qt.isEmpty());
}
}
/*
运行结果
one
two
false
three
true
Press any key to continue...
*/
范例十:HashSet 用法范例,
import java.util.*;
public class HashSetTest
{
public static void main(String[] args)
{
HashSet hs = new HashSet();
hs.add("one");
hs.add("two");
hs.add("three");
hs.add("one");
Iterator it = hs.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
/*
运行结果
two
one
three
Press any key to continue...
*/
从上面的运行结果中我们可以看到,HashSet中不不允许有重复的元素的。
范例十一:HashSet的用法
import java.util.*;
public class HashSetTest
{
public static void main(String[] args)
{
HashSet hs = new HashSet();
hs.add(new Student(10,"zhangsan"));
hs.add(new Student(20,"lis"));
hs.add(new Student(30,"wangwu"));
hs.add(new Student(10,"zhangsan"));
Iterator it = hs.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
class Student
{
int age;
String name;
Student(int age,String name)
{
this.age = age;
this.name = name;
}
public String toString()
{
return name+":"+age;
}
}
/*
运行结果
zhangsan:10
wangwu:30
lis:20
zhangsan:10
Press any key to continue...
可以看出HashSet中出现了重复元素,
这是因为我们用new 关键字产生了两个不同的对象,
因此他们的散列码是不相同的,所以回出现了相同的元素,那么如何解决这个重复的问题那请看下面的范例*/
范例十二:HashSet的用法范例
import java.util.*;
public class HashSetTest
{
public static void main(String[] args)
{
HashSet hs = new HashSet();
hs.add(new Student(10,"zhangsan"));
hs.add(new Student(20,"lis"));
hs.add(new Student(30,"wangwu"));
hs.add(new Student(10,"zhangsan"));
Iterator it = hs.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
class Student
{
int age;
String name;
Student(int age,String name)
{
this.age = age;
this.name = name;
}
public int hashCode()
{
return age*name.hashCode();
}
public boolean equals(Object o)
{
Student s = (Student) o;
return age==s.age && name==s.name;
}
public String toString()
{
return name+":"+age;
}
}
/*wangwu:30
zhangsan:10
lis:20
Press any key to continue...
*/
范例十三:TreeSet使用范例
import java.util.*;
public class TreeSetTest
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet();
ts.add("zhangsan");
ts.add("lisi");
ts.add("wangwu");
Iterator it = ts.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
运行结果
lisi
wangwu
zhangsan
Press any key to continue...
从上述结果可以知道TreeSet在输出时是按照升序排列的
范例十四:TreeSet使用范例,想TreeSet中添加自定义对象
import java.util.*;
public class TreeSetTest
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet(new Student.StudentComparator());
ts.add(new Student("zhangsan",10));
ts.add(new Student("lisi",5));
ts.add(new Student("wangwu",30));
ts.add(new Student("mary",30));
Iterator it = ts.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
class Student implements Comparable
{
String name;
int age;
static class StudentComparator implements Comparator
{
public int compare(Object o1,Object o2)
{
Student s1 = (Student) o1;
Student s2 = (Student) o2;
int result = s1.age > s2.age ? 1 :(s1.age == s2.age ? 0 : -1);
if(result == 0)
{
result = s1.name.compareTo(s2.name);
}
return result;
}
}
Student(String name,int age)
{
this.name = name;
this.age = age;
}
public int compareTo(Object o)
{
Student s = (Student)o;
return this.age>s.age ? 1:(this.age==s.age ? 0 : -1);
}
public String toString()
{
return age+":"+name;
}
}
运行结果
5:lisi
10:zhangsan
30:mary
30:wangwu
Press any key to continue...
范例十五:HashMap的用法范例
import java.util.*;
public class HashMapTest
{
public static void printElement(Collection c)
{
Iterator it = c.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
public static void main(String[] args)
{
HashMap hm = new HashMap();
hm.put("one","zhangsan");
hm.put("two","lisi");
hm.put("three","wangwu");
System.out.println(hm.get("one"));
System.out.println(hm.get("two"));
System.out.println(hm.get("three"));
Set s = hm.keySet();
System.out.println("keys");
printElement(s);
System.out.println("values");
Collection c = hm.values();
printElement(c);
Set ss = hm.entrySet();
printElement(ss);
Iterator it = ss.iterator();
while(it.hasNext())
{
Map.Entry m = (Map.Entry) it.next();
System.out.println(m.getKey()+":"+m.getValue());
}
}
}
/*
*
*
*运行结果:
*zhangsan
lisi
wangwu
keys
two
one
three
values
lisi
zhangsan
wangwu
two=lisi
one=zhangsan
three=wangwu
two:lisi
one:zhangsan
three:wangwu
Press any key to continue...
*/
范例十六:Property的用法范例---读取文件的配置信息
import java.util.*;
import java.io.*;
public class ProTest
{
public static void main(String args[]) throws Exception
{
Properties pps = new Properties();
pps.load(new FileInputStream("win.ini"));
Enumeration e = pps.propertyNames();
while(e.hasMoreElements())
{
String key = (String)e.nextElement();
System.out.println(key+"="+pps.getProperty(key));
}
}
}
运行结果:
name3=wangwu;
name2=lisi;
name1=zhangsan
Press any key to continue...
- 集合类处理范例
- C#集合类使用范例
- opencv视频处理范例
- C#处理图片的范例
- android延时处理任务范例
- crash处理case范例说明
- 大数阶乘---处理大数范例
- Unreal 后期处理材质范例
- 常用的集合元素使用范例代码
- Atitit 图像处理 halcon类库的使用 范例边缘检测 attilax总结
- 事件处理范例(接收鼠标事件)
- javascript表单事件处理的小范例
- 利用xstream处理java中的集合类
- 利用xstream处理java中的集合类
- 集合类对null的处理
- 调用类 范例
- 范例
- 范例
- 详细介绍在tomcat中配置数据源原理
- MySQL存储过程学习笔记
- .NET 开发人员应该下载的十个必备工具
- [原创][Python 技术培训] 第三周讲义.网络编程 (第二版)
- 网站收藏
- 集合类处理范例
- IP V6
- 精华SQL语句
- problem 2969
- OLE DB 应用程序在 eVC++ 使用 Compact 方法
- 教您如何测试服务器有多少虚拟主机用户和他们站点的百度收录数
- Spring总结之AOP
- Asp.Net 2.0 TreeView的Checkbox级联操作
- 网易推出搜索服务,正式杀入互联网搜索战团