67,512
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
}
package test;
public class HelloWorld {
static {
System.out.println("Hello Word");
System.exit(0);
}
}
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");