- 浏览: 104346 次
- 性别:
- 来自: 大连
文章分类
- 全部博客 (159)
- 前端开发 (6)
- jsp (4)
- jquery (17)
- 配置文件 (2)
- Spring (10)
- java (34)
- hibernate (9)
- VC++ win32 application (6)
- C++ (6)
- MFC文件夹对话框 (1)
- C (4)
- MFC (1)
- C# (3)
- 两个路由器怎么连接 (1)
- Maven3 (3)
- Servlet (10)
- Struts2 (6)
- jdom (1)
- Struts1 (1)
- Spring MVC (2)
- Ant (5)
- SSH (6)
- oracle 10g (1)
- ibatis (3)
- JPA (3)
- EJB (2)
- XML知识 (3)
- javascript (2)
- android (1)
- FreeMarker (1)
- dwr (1)
- Ext (10)
- EXCEL (1)
最新评论
package polymorphism.container;
import java.util.Arrays;
import java.util.List;
public class ListTest {
/**
* @param args
*/
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4,5);
System.out.println(list);
list.add(6);
}
}
这样的话,List(列表)就是固定大小的了.因为其底层表示的是静态数组 所以就不能add了
会抛异常 java.lang.UnsupportedOperationException
----------------------------------------------------------------------------------
package com.bit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
public class AddingGroups {
/**
* @param args
*/
public static void main(String[] args) {
Collection<Integer> collection = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5));
Integer[] moreInts = {6,7,8,9,10};
collection.addAll(Arrays.asList(moreInts));
Collections.addAll(collection, 11,12,13,14,15);
Collections.addAll(collection, moreInts);
System.out.println(collection);
}
}
结果:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 6, 7, 8, 9, 10]
----------------------------------------------------------------------------------
package com.bjsxt;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
public class ArrayListTest {
public static void printElements(Collection<?> c) {
Iterator<?> it = c.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
/**
* @param args
*/
public static void main(String[] args) {
Student s1 = new Student(2,"zhangsan");
Student s2 = new Student(1,"lisi");
Student s3 = new Student(3,"wangwu");
ArrayList al = new ArrayList();
al.add(s1);
al.add(s2);
al.add(s3);
Collections.sort(al);
printElements(al);
}
}
class Student implements Comparable {
int num;
String name;
Student(int num,String name) {
this.num = num;
this.name = name;
}
public int compareTo(Object o) {
Student s = (Student)o;
if(num > s.num) {
return 1;
}else if(num == s.num) {
return 0;
}else {
return -1;
}
}
public String toString() {
return num + ":" + name;
}
}
结果:
1:lisi
2:zhangsan
3:wangwu
--------------------------------------------------------------------------------
package diaodiao.shama;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
public class ArrayListTest2 {
public static void printElements(Collection<?> c) {
Iterator<?> it = c.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
/**
* @param args
*/
public static void main(String[] args) {
Student s1 = new Student(2,"zhangsan");
Student s2 = new Student(1,"lisi");
Student s3 = new Student(3,"wangwu");
Student s4 = new Student(2,"mybole");
ArrayList al = new ArrayList();
al.add(s1);
al.add(s2);
al.add(s3);
al.add(s4);
Collections.sort(al,new Student.StudentComparator());
printElements(al);
}
}
class Student {
int num;
String name;
static class StudentComparator implements Comparator {
public int compare(Object o1,Object o2) {
Student s1 = (Student)o1;
Student s2 = (Student)o2;
int result = 0;
if(s1.num > s2.num) {
result = 1;
} else if(s1.num == s2.num) {
result = 0;
} else if(s1.num < s2.num) {
result = -1;
}
if(result == 0) {
result = s1.name.compareTo(s2.name);
}
return result;
}
}
Student(int num,String name) {
this.num = num;
this.name = name;
}
public String toString() {
return "num: "+ num + " name: " + name;
}
}
num: 1 name: lisi
num: 2 name: mybole
num: 2 name: zhangsan
num: 3 name: wangwu
----------------------------------------------------------------------------------
package kayizaoku.hama;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
public class ArrayListTest3 {
public static void printElements(Collection<?> c) {
Iterator<?> it = c.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
/**
* @param args
*/
public static void main(String[] args) {
Student s1 = new Student(1,"lisi");
Student s2 = new Student(3,"wangwu");
Student s3 = new Student(2,"mybole");
ArrayList al = new ArrayList();
al.add(s1);
al.add(s2);
al.add(s3);
Collections.sort(al,Collections.reverseOrder());
printElements(al);
}
}
class Student implements Comparable{
int num;
String name;
Student(int num,String name) {
this.num = num;
this.name = name;
}
public String toString() {
return "num: "+ num + " name: " + name;
}
public int compareTo(Object o) {
Student s = (Student)o;
if(num > s.num) {
return 1;
} else if(num == s.num) {
return 0;
} else {
return -1;
}
}
}
结果:
num: 3 name: wangwu
num: 2 name: mybole
num: 1 name: lisi
import java.util.Arrays;
import java.util.List;
public class ListTest {
/**
* @param args
*/
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4,5);
System.out.println(list);
list.add(6);
}
}
这样的话,List(列表)就是固定大小的了.因为其底层表示的是静态数组 所以就不能add了
会抛异常 java.lang.UnsupportedOperationException
----------------------------------------------------------------------------------
package com.bit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
public class AddingGroups {
/**
* @param args
*/
public static void main(String[] args) {
Collection<Integer> collection = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5));
Integer[] moreInts = {6,7,8,9,10};
collection.addAll(Arrays.asList(moreInts));
Collections.addAll(collection, 11,12,13,14,15);
Collections.addAll(collection, moreInts);
System.out.println(collection);
}
}
结果:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 6, 7, 8, 9, 10]
----------------------------------------------------------------------------------
package com.bjsxt;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
public class ArrayListTest {
public static void printElements(Collection<?> c) {
Iterator<?> it = c.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
/**
* @param args
*/
public static void main(String[] args) {
Student s1 = new Student(2,"zhangsan");
Student s2 = new Student(1,"lisi");
Student s3 = new Student(3,"wangwu");
ArrayList al = new ArrayList();
al.add(s1);
al.add(s2);
al.add(s3);
Collections.sort(al);
printElements(al);
}
}
class Student implements Comparable {
int num;
String name;
Student(int num,String name) {
this.num = num;
this.name = name;
}
public int compareTo(Object o) {
Student s = (Student)o;
if(num > s.num) {
return 1;
}else if(num == s.num) {
return 0;
}else {
return -1;
}
}
public String toString() {
return num + ":" + name;
}
}
结果:
1:lisi
2:zhangsan
3:wangwu
--------------------------------------------------------------------------------
package diaodiao.shama;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
public class ArrayListTest2 {
public static void printElements(Collection<?> c) {
Iterator<?> it = c.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
/**
* @param args
*/
public static void main(String[] args) {
Student s1 = new Student(2,"zhangsan");
Student s2 = new Student(1,"lisi");
Student s3 = new Student(3,"wangwu");
Student s4 = new Student(2,"mybole");
ArrayList al = new ArrayList();
al.add(s1);
al.add(s2);
al.add(s3);
al.add(s4);
Collections.sort(al,new Student.StudentComparator());
printElements(al);
}
}
class Student {
int num;
String name;
static class StudentComparator implements Comparator {
public int compare(Object o1,Object o2) {
Student s1 = (Student)o1;
Student s2 = (Student)o2;
int result = 0;
if(s1.num > s2.num) {
result = 1;
} else if(s1.num == s2.num) {
result = 0;
} else if(s1.num < s2.num) {
result = -1;
}
if(result == 0) {
result = s1.name.compareTo(s2.name);
}
return result;
}
}
Student(int num,String name) {
this.num = num;
this.name = name;
}
public String toString() {
return "num: "+ num + " name: " + name;
}
}
num: 1 name: lisi
num: 2 name: mybole
num: 2 name: zhangsan
num: 3 name: wangwu
----------------------------------------------------------------------------------
package kayizaoku.hama;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
public class ArrayListTest3 {
public static void printElements(Collection<?> c) {
Iterator<?> it = c.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
/**
* @param args
*/
public static void main(String[] args) {
Student s1 = new Student(1,"lisi");
Student s2 = new Student(3,"wangwu");
Student s3 = new Student(2,"mybole");
ArrayList al = new ArrayList();
al.add(s1);
al.add(s2);
al.add(s3);
Collections.sort(al,Collections.reverseOrder());
printElements(al);
}
}
class Student implements Comparable{
int num;
String name;
Student(int num,String name) {
this.num = num;
this.name = name;
}
public String toString() {
return "num: "+ num + " name: " + name;
}
public int compareTo(Object o) {
Student s = (Student)o;
if(num > s.num) {
return 1;
} else if(num == s.num) {
return 0;
} else {
return -1;
}
}
}
结果:
num: 3 name: wangwu
num: 2 name: mybole
num: 1 name: lisi
发表评论
-
java的日期操作类
2014-09-16 15:00 502package cn.mldn; import ja ... -
列表框 JList
2014-08-16 19:19 443package net.mindview.util; ... -
分页类
2014-02-23 15:05 471package com.yxq.model; pub ... -
java mail
2014-02-14 15:13 364package com.util.mail; /** ... -
Map输出方式一:Iterator输出Map
2014-02-12 11:49 850package com.mldn.map; impo ... -
Comparable
2014-02-11 20:16 349package org.lxh.demo13.setdem ... -
Foreach与迭代器
2014-02-11 17:45 481到目前为止,foreach语法主要用于数组,但是它也可以应 ... -
打印流
2014-02-07 20:10 409package com.bjsxt.print; i ... -
管道流
2014-02-07 17:23 414package com.bjsxt.buffer; ... -
Tramsform IO
2014-02-07 16:15 595package com.bjsxt.transform; ... -
Buffered IO
2014-02-07 15:02 546package com.bjsxt.buffer; ... -
字符流
2014-02-07 10:32 570package com.bjsxt.reader; ... -
字节流
2014-02-06 17:57 473package com.bjsxt.stream; ... -
RandomAccessFile类
2014-02-06 13:18 567使用RandomAccessFile类写入数据 impor ... -
排序啊啊啊啊
2013-09-18 10:32 418package cn.mldn; public cl ... -
java_匿名内部类
2013-04-10 15:26 926package com.bitsoft.inner; ... -
java_内部类
2013-04-10 14:05 754package com.diaodiao.inner; ... -
JAVA----- String方面的面试举例
2013-03-31 00:55 703[img] [/img] package com.t ... -
反射机制(2)
2013-03-20 20:39 664package com.bjsxt.demo; im ... -
反射机制
2013-03-17 22:19 445package org.dcl.demo; inte ...
相关推荐
java集合 java集合思维导图 java集合总结
xmind格式的Java集合框架学习导图,包括Collection接口/Map接口以及具体实现类。 同样包含大厂面试题,也在导图中有所体现。 能学到什么: 更加成体系的知识框架,更加全面的、系统的知识。 思维导图: 思维导图具有...
键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低输出到控制台java 集合练习题
java集合基础习题及答案,
Java集合整体讲解,其中包含了Collection,Map,Iterator和一些工具类,以及集合整体大框架
java 集合 List arrayList vector map set
Java 集合排序 及java集合类 详解.pdf
该文档主要详细总结了Java集合的相关知识,包括Collection和Map接口、Collection接口的子接口List和Set接口以及具体的实现类、存储原理等;Map接口的子接口HashMap、LinkedHashMap、TreeMap、Properties等
Java 集合排序及java 集合类详解,Java里面最重要、最常用也就是集合那部分了,能够用好集合和理解好集合对于做Java程序的开发拥有无比的好处。本教程详细解释了关于Java中的集合是如何实现的, 以及他们的实现原理...
Java集合框架总结Java集合框架总结Java集合框架总结Java集合框架总结Java集合框架总结Java集合框架总结
内含大量java集合框架方面常被面试官问到的经典面试题。
Java集合排序及java集合类详解,对list,set,map等java集合进行详细讲解
集合是将多个元素组成一个单元的...Java集合框架,为我们提供了一套性能优良、使用方便的接口和类,我们不必再重新发明轮子,只需学会如何使用它们,就可以处理实际应用中出现的问题了Java集合框架位于java.util包中
关于java集合资料的整理 集合接口:6个接口,表示不同集合类型,是集合框架的基础。 抽象类:5个抽象类,对集合接口的部分实现。可扩展为自定义集合类。 实现类:8个实现类,对接口的具体实现。 在很大程度上,...
Java集合框架详解Java集合框架详解Java集合框架详解
java集合框架图java集合框架图java集合框架图java集合框架图java集合框架图
Java集合详解,详细讲解java的集合类,对java集合类的最详细的讲解。我自己的总结,保证大家看了很有收获
java集合
java 集合分组排序帮助类有好的意见可以互相交流不甚感激