午睡过后写了个实现List分页的小DD 不复杂 不过有点小小疑惑 代码如下:package test;import java.util.Collections;
import java.util.Comparator;
import java.util.List;/**
* 分页类
*
* @author Lv9
* @since 2010.1.28
*/
public class ListSplitPage<T> {
private final List<T> list; // 记录
private final int pageSize;// 每页显示的记录数
private final int pageNum;// 总页数 private int currentPage = 1;// 当前页
private int fromIndex = 0;// 记录起始索引
private int toIndex = 0;// 记录结束索引 public ListSplitPage(List<T> list, Comparator<T> comparator, int pageSize) {
this.list = list;
this.pageSize = pageSize; if (this.pageSize < 0) {
throw new RuntimeException("每页显示的记录数不能小于0");
} else {
if (list.size() % pageSize == 0)
this.pageNum = list.size() / pageSize;
else
this.pageNum = list.size() / pageSize + 1;
}
Collections.sort(list, comparator);
} /**
* 分页
*
* @param currentPage
* 当前页数
*/
public List<T> getList(int currentPage) {
if (currentPage > pageNum || currentPage < 1) {
throw new RuntimeException("当前页数不能大于总页数并且不能小于1");
} else {
this.currentPage = currentPage;
this.fromIndex = (this.currentPage - 1) * pageSize; if (this.currentPage == pageNum) {
this.toIndex = list.size();
} else {
this.toIndex = this.currentPage * pageSize;
}
return list.subList(fromIndex, toIndex);
}
} /**
* 获得记录起始索引
*/
public int getFromIndex() {
return fromIndex;
} /**
* 获得记录结束索引
*/
public int getToIndex() {
return toIndex;
} /**
* 获得总页数
*/
public int getPageNum() {
return pageNum;
} /**
* 获得每页显示的记录数
*/
public int getPageSize() {
return pageSize;
} /**
* 获得当前页
*/
public int getCurrentPage() {
return currentPage;
} /**
* 获得记录集合
*/
public List<T> getList() {
return this.list;
}
}/**
* Comparator工厂
*
* @author Lv9
* @since 2010.1.28
*/
class GameObjectComparatorFactory { private static Comparator<GameObject> idComparator = new Comparator<GameObject>() {
public int compare(GameObject o1, GameObject o2) {
if (o1.getId() > o2.getId())
return 1;
else if (o1.getId() == o2.getId())
return 0;
else
return -1;
}
}; private static Comparator<GameObject> leaveComparator = new Comparator<GameObject>() {
public int compare(GameObject o1, GameObject o2) {
if (o1.getLeave() > o2.getLeave())
return 1;
else if (o1.getLeave() == o2.getLeave())
return 0;
else
return -1;
}
}; public static Comparator<GameObject> getIdComparator() {
return idComparator;
} public static Comparator<GameObject> getLeaveComparator() {
return leaveComparator;
}
}class GameObject {
private Integer id;
private Integer leave; public GameObject() { } public GameObject(Integer id, Integer leave) {
this.id = id;
this.leave = leave;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public Integer getLeave() {
return leave;
} public void setLeave(Integer leave) {
this.leave = leave;
} public String toString() {
return "id:" + id + ",leave:" + leave;
}
}运行这个测试package test;import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;public class Test {
public static void main(String[] args) {
List<GameObject> list = new ArrayList<GameObject>();
Random random = new Random(); for (int i = 0; i < 50; i++) {
list.add(new GameObject(i, random.nextInt(100)));
}
ListSplitPage<GameObject> pagelist = new ListSplitPage<GameObject>(
list, GameObjectComparatorFactory.getIdComparator(), 8);
System.out.println("当前页为:" + pagelist.getCurrentPage() + "\n总页数为:"
+ pagelist.getPageNum() + "\n总记录数为:" + list.size());
while (true) {
displayList(pagelist.getList(getPage()));
}
} public static int getPage() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入当前页码:");
return scanner.nextInt();
} public static void displayList(List<?> list) {
for (Object object : list) {
System.out.println(object);
}
}
}小的代码写的不咋地 不过我想到个问题 用LIST分页的话 如果说在分页过程中 在外面把这个LIST里面的元素稍微搞那么一下 里面肯定会出错 这个该咋处理..
import java.util.Comparator;
import java.util.List;/**
* 分页类
*
* @author Lv9
* @since 2010.1.28
*/
public class ListSplitPage<T> {
private final List<T> list; // 记录
private final int pageSize;// 每页显示的记录数
private final int pageNum;// 总页数 private int currentPage = 1;// 当前页
private int fromIndex = 0;// 记录起始索引
private int toIndex = 0;// 记录结束索引 public ListSplitPage(List<T> list, Comparator<T> comparator, int pageSize) {
this.list = list;
this.pageSize = pageSize; if (this.pageSize < 0) {
throw new RuntimeException("每页显示的记录数不能小于0");
} else {
if (list.size() % pageSize == 0)
this.pageNum = list.size() / pageSize;
else
this.pageNum = list.size() / pageSize + 1;
}
Collections.sort(list, comparator);
} /**
* 分页
*
* @param currentPage
* 当前页数
*/
public List<T> getList(int currentPage) {
if (currentPage > pageNum || currentPage < 1) {
throw new RuntimeException("当前页数不能大于总页数并且不能小于1");
} else {
this.currentPage = currentPage;
this.fromIndex = (this.currentPage - 1) * pageSize; if (this.currentPage == pageNum) {
this.toIndex = list.size();
} else {
this.toIndex = this.currentPage * pageSize;
}
return list.subList(fromIndex, toIndex);
}
} /**
* 获得记录起始索引
*/
public int getFromIndex() {
return fromIndex;
} /**
* 获得记录结束索引
*/
public int getToIndex() {
return toIndex;
} /**
* 获得总页数
*/
public int getPageNum() {
return pageNum;
} /**
* 获得每页显示的记录数
*/
public int getPageSize() {
return pageSize;
} /**
* 获得当前页
*/
public int getCurrentPage() {
return currentPage;
} /**
* 获得记录集合
*/
public List<T> getList() {
return this.list;
}
}/**
* Comparator工厂
*
* @author Lv9
* @since 2010.1.28
*/
class GameObjectComparatorFactory { private static Comparator<GameObject> idComparator = new Comparator<GameObject>() {
public int compare(GameObject o1, GameObject o2) {
if (o1.getId() > o2.getId())
return 1;
else if (o1.getId() == o2.getId())
return 0;
else
return -1;
}
}; private static Comparator<GameObject> leaveComparator = new Comparator<GameObject>() {
public int compare(GameObject o1, GameObject o2) {
if (o1.getLeave() > o2.getLeave())
return 1;
else if (o1.getLeave() == o2.getLeave())
return 0;
else
return -1;
}
}; public static Comparator<GameObject> getIdComparator() {
return idComparator;
} public static Comparator<GameObject> getLeaveComparator() {
return leaveComparator;
}
}class GameObject {
private Integer id;
private Integer leave; public GameObject() { } public GameObject(Integer id, Integer leave) {
this.id = id;
this.leave = leave;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public Integer getLeave() {
return leave;
} public void setLeave(Integer leave) {
this.leave = leave;
} public String toString() {
return "id:" + id + ",leave:" + leave;
}
}运行这个测试package test;import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;public class Test {
public static void main(String[] args) {
List<GameObject> list = new ArrayList<GameObject>();
Random random = new Random(); for (int i = 0; i < 50; i++) {
list.add(new GameObject(i, random.nextInt(100)));
}
ListSplitPage<GameObject> pagelist = new ListSplitPage<GameObject>(
list, GameObjectComparatorFactory.getIdComparator(), 8);
System.out.println("当前页为:" + pagelist.getCurrentPage() + "\n总页数为:"
+ pagelist.getPageNum() + "\n总记录数为:" + list.size());
while (true) {
displayList(pagelist.getList(getPage()));
}
} public static int getPage() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入当前页码:");
return scanner.nextInt();
} public static void displayList(List<?> list) {
for (Object object : list) {
System.out.println(object);
}
}
}小的代码写的不咋地 不过我想到个问题 用LIST分页的话 如果说在分页过程中 在外面把这个LIST里面的元素稍微搞那么一下 里面肯定会出错 这个该咋处理..
public class Pageable {
protected int rowsCount;
protected int pageSize;
protected int curPage;
private int curRow;
private int begin;
public int getBegin() {
return begin;
} public int getEnd() {
return end;
} private int end;
public Pageable(int size ,int pageSize,int curPage) {
this.rowsCount=size;//设置总的纪录条数
this.pageSize = pageSize;//设置页的大小
this.curPage = curPage;//当前是选择的页数
this.gotoPage(curPage);
}
public int getCurRow() {
return curRow;
}
public int getRowsCount() {
return rowsCount;
} public void setRowsCount(int rowsCount) {
this.rowsCount = rowsCount;
} public void setCurPage(int curPage) {
this.curPage = curPage;
} public void setCurRow(int curRow) {
this.curRow = curRow;
} public int getCurPage() {
return this.curPage;
}
public int getPageCount() {
if(rowsCount==0) return 0;
if(pageSize==0) return 1;
double tmpD=(double)rowsCount/pageSize;
int tmpI=(int)tmpD;
if(tmpD>tmpI) tmpI++;
return tmpI;
}
public int getPageRowsCount() {
if (pageSize == 0)
return rowsCount;
if (rowsCount == 0)
return 0;
if (curPage != getPageCount())
return pageSize;
return rowsCount - (getPageCount() - 1) * pageSize;
}
public int getPageSize() {
return pageSize;
}
public void gotoPage(int page) {
if (page < 1)
page = 1;
if (page > getPageCount())
page = getPageCount();
int row = (page - 1) * pageSize;
curPage = page;
this.curRow = row;
curPage = page;
this.begin = this.curRow;
this.end = this.begin+this.getPageRowsCount()-1;
}
public void setPageSize(int pageSize) {
if (pageSize >= 0) {
this.pageSize = pageSize;
this.curRow = 0;
}
}}
public int compare(GameObject o1, GameObject o2) {
if (o1.getId() > o2.getId())
return 1;
else if (o1.getId() == o2.getId())
return 0;
else
return -1;
}
}; private static Comparator<GameObject> leaveComparator = new Comparator<GameObject>() {
public int compare(GameObject o1, GameObject o2) {
if (o1.getLeave() > o2.getLeave())
return 1;
else if (o1.getLeave() == o2.getLeave())
return 0;
else
return -1;
}
};
这两个方法完全可以用java的反射写成一个方法
public class DVDComparator implements java.util.Comparator<DVD> {
private String compareMethod;
private boolean oppsiteOrder;
public DVDComparator(String compareMethod,boolean oppsiteOrder) {
this.compareMethod = "get"+compareMethod;//判断比较的方法名
this.oppsiteOrder = oppsiteOrder;//升序还是降序
}
public DVDComparator(String compareMethod) {
this.compareMethod = "get"+compareMethod;
this.oppsiteOrder = false;
} public int compare(DVD o1, DVD o2) {
// TODO Auto-generated method stub
try {
//反射机制
Object result1 = o1.getClass().getMethod(compareMethod).invoke(o1);
Object result2 = o1.getClass().getMethod(compareMethod).invoke(o2);
if(result1 instanceof Integer){
Integer ob1=(Integer)result1;
Integer ob2=(Integer)result2;
if(oppsiteOrder){
return ob1.compareTo(ob2)*(-1);
}
return ob1.compareTo(ob2);
} else if(result1 instanceof String){
String ob1=(String)result1;
String ob2=(String)result2;
if(oppsiteOrder){
return ob1.compareTo(ob2)*(-1);
}
return ob1.compareTo(ob2);
}else if(result1 instanceof Date){
Date ob1 = (Date)result1;
Date ob2 = (Date)result2;
if(oppsiteOrder){
return ob1.compareTo(ob2)*(-1);
}
return ob1.compareTo(ob2);
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
}
private static Comparator <GameObject> idComparator = new Comparator <GameObject>() {
public int compare(GameObject o1, GameObject o2) {
if (o1.getId() > o2.getId())
return 1;
else if (o1.getId() == o2.getId())
return 0;
else
return -1;
}
}; private static Comparator <GameObject> leaveComparator = new Comparator <GameObject>() {
public int compare(GameObject o1, GameObject o2) {
if (o1.getLeave() > o2.getLeave())
return 1;
else if (o1.getLeave() == o2.getLeave())
return 0;
else
return -1;
}
}; 你先定义两个比较器干吗?
不是做不同的比较吗?
分别为按Id,levae字段进行比较
这里用反射的话就是根据你指定的字段名,前面再加个get,得到getId或者getLevae方法名,有了方法名,就可以根据反射调用生成的方法了
list中的元素属性被改了顶多是排序错误吧,除非连排序都一次性排好以后直接输出。晚上回家再看代码,等等我啊
说明bk的代码可读性还是不错的就看bk想要啥方面的意见了。
给我点分吧,咱不bs你了。
我套了CODE 所以看起来比较好看(好看..)技术分 不能乱给 拿出意见 我给..
1.list等实例变量是靠唯一的构造方法进行赋值,不存在引用再次改变的情况,因此这里用final我实在没想出来他有啥意义。
2.排序方法似乎还可以写活一点,扩展一下参数列表,接收一个字段名,和一个排序方向,这样就能根据调用者指定的排序字段和顺序排序,工厂也可以省了。
3.注释中的参数我个人一般喜欢写参数类型(@param String 啥啥啥),而不是参数名(@param name 啥啥啥),因为我觉得有时候参数列表长了,而eclipse也提示不周,编码时就很难快速的为调用的方法填入准确类型的参数。另外,代码中注释不全。
4.对于设计来说,我觉得既然罗列出来的数据是可能被修改的,且数据存于此分页工具对象中是可供多次查询和暂存一段时间的,因此在多个人同一时间(甚至无需并发)访问同样的数据并修改,那很可能看到的都是与数据源中不符的数据。所以这个东东我看没啥用途
另外我一直怀疑那种新式的for循环写法在对应集合类时性能不如传统for循环写法,一测试,性能还真有些区别。可惜我只能测试一百万个,两百万个我jvm就再也没内存创建新对象了,否则数据更准确。List<String> string = new ArrayList<String>();
for (int i=0;i<1000000;i++) {
string.add(new String());
}
long begin = System.currentTimeMillis();
for (String str : string) {
str.toString();
}
System.out.println("for 循环1 遍历百万个String用去:" + (System.currentTimeMillis() - begin) + "s");
begin = System.currentTimeMillis();
int size = string.size();
for (int i=0;i<size;i++) {
string.get(i).toString();
}
System.out.println("for 循环2 遍历百万个String用去:" + (System.currentTimeMillis() - begin) + "s");
----------------------
一家之言啊,水平不佳,能卖的都卖出来了,不妥之处别忘了提出。更别忘了给分,我很穷。
2.接受(坦白说我更想随便输入个值 在方法内部一判断 然后自动生成排序代码 不是根据一个参数选择排哪个..)
3.平时偶是不写注释的...下回我会改正的
4.需求!
5.我更想看看你怎么写的 新的for each重点不是在性能 而是在简化 我们平时做一个循环需要写一些多余的代码(并且写的都那一个样) 所以 这个纯粹就是简化..恩 给你分分
package test;public class HelloWorld {
static {
System.out.println("Hello Word");
System.exit(0);
}
}
HelloWorld..
别用ARRAYLIST你用 LINKEDLIST看看? 这个for each默认是用Iterator实现 说不定你用ARRAYLIST的GET方法比较快呢..
从上面的代码来看,好像你把未分页前的信息都放到list中了,这样好像不太合适吧,失去了分页的意义