根据start和limit从已有的数据列表中获取从start开始的limit个数据
代码记录(需求:根据start和limit从已有的数据列表中获取从start开始的limit个数据)
已有的数据列表:这个数据列表是经过处理的,可能是在SQL查询时无法处理的如多条件排序,而排序条件需要重新计算才能得到。
实体类Store.java
package com.zhipengs.work.test; import java.io.Serializable; /**
* 实体类Store
*
* @author zhipengs
*/
public class Store implements Serializable {
private static final long serialVersionUID = 8799004943860173845L;
private Long storeId;
private String storeName;
private String longitude;
private String latitude;
private String storeLogo; public Store(Long storeId, String storeName, String longitude,
String latitude, String storeLogo) {
super();
this.storeId = storeId;
this.storeName = storeName;
this.longitude = longitude;
this.latitude = latitude;
this.storeLogo = storeLogo;
} public Long getStoreId() {
return storeId;
} public void setStoreId(Long storeId) {
this.storeId = storeId;
} public String getStoreName() {
return storeName;
} public void setStoreName(String storeName) {
this.storeName = storeName;
} public String getLongitude() {
return longitude;
} public void setLongitude(String longitude) {
this.longitude = longitude;
} public String getLatitude() {
return latitude;
} public void setLatitude(String latitude) {
this.latitude = latitude;
} public String getStoreLogo() {
return storeLogo;
} public void setStoreLogo(String storeLogo) {
this.storeLogo = storeLogo;
} @Override
public String toString() {
return "Store [storeId=" + storeId + ", storeName=" + storeName
+ ", longitude=" + longitude + ", latitude=" + latitude
+ ", storeLogo=" + storeLogo + "]";
} }
工具类DataStartLimitUtil.java
package com.zhipengs.work.test; import java.util.ArrayList;
import java.util.List;
/**
* 写成工具类DataStartLimitUtil
*
* @author zhipengs
*/
public class DataStartLimitUtil { public static List<Object> getLimitSampleStores(Integer start,
Integer limit, List<Object> data) {
System.out.println("start=" + start + " limit=" + limit);
List<Object> dataReturn = new ArrayList<Object>();
// 如果data为null,直接返回或者获取数据的起始位置大于等于data的大小则返回null
if (null == data || data.size() <= start) {
return null;
}
// 计算需要取data数据的最后一个位置loop,start为起始位置,共取limit个
int loop = start + limit;
// 如果data的大小大于等于获取数据的其实位置的同时又小于需要取得data数据的最后一个位置数loop,则将loop重置为data的大小
if (data.size() >= start && data.size() < loop) {
loop = data.size();
}
// 循环遍历获取data中第start个位置到第loop个位置的数据,放入新的列表中并返回
for (int i = start; i < loop; i++) {
dataReturn.add(data.get(i));
}
return dataReturn;
}
}
测试类MainTest.java
package com.zhipengs.work.test; import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* 测试
*
* @author zhipengs
*/
public class MainTest {
@SuppressWarnings("unused")
public static void main(String[] args) {
List<Object> storeRerurnList = null;
List<Object> storeList = null;
Store store = null; // storeList为null
storeRerurnList = DataStartLimitUtil.getLimitSampleStores(0, 21, storeList);
System.out.println("storeList为null,返回的数据列表:"+storeRerurnList);
System.out.println("------------------------------------------------------"); // 往storeList中存入20条数据
storeList = new ArrayList<Object>();
for (int i = 0; i < 20; i++) {
store = new Store((long) i+1, "_store_", "113.85786457474","23.7635746576", "mobile/"
+ Math.abs(new Random().nextInt()) + ".jpg");
storeList.add(store);
} // 获取数据的起始位置大于等于storeList的size
storeRerurnList = DataStartLimitUtil.getLimitSampleStores(20, 1, storeList);
System.out.println("获取数据的起始位置大于等于storeList的size,返回的数据列表:"+storeRerurnList);
System.out.println("------------------------------------------------------"); // storeList的size大于等于获取数据的起始位置的同时又小于需要取得data数据的最后一个位置数
storeRerurnList = DataStartLimitUtil.getLimitSampleStores(0, 25, storeList);
System.out.println("storeList有20条数据,从0取25条,storeRerurnList.size(): " + storeRerurnList.size());
System.out.println("storeList有20条数据,从0取25条,第一条"+storeRerurnList.get(0));
System.out.println("------------------------------------------------------"); storeRerurnList = DataStartLimitUtil.getLimitSampleStores(5, 25, storeList);
System.out.println("storeList有20条数据,从5取25条,storeRerurnList.size(): " + storeRerurnList.size());
System.out.println("storeList有20条数据,从5取25条,第一条"+storeRerurnList.get(0)); // 正常获取数据
System.out.println("---------------------循环获取所有数据---------------------");
for(int i=0;i<25;i+=5) {
storeRerurnList = DataStartLimitUtil.getLimitSampleStores(i, 5, storeList);
if(null == storeRerurnList) {
System.out.println(storeRerurnList);
} else {
System.out.println("storeList有20条数据,从"+i+"取5条,storeRerurnList.size(): " + storeRerurnList.size());
System.out.println("storeList有20条数据,从"+i+"取5条,第一条"+storeRerurnList.get(0));
System.out.println("------------------------------------------------------");
} } /*
* for(Object o : storeRerurnList) { if(o instanceof Store) {
* System.out.println((Store)o); } }
*/ }
}
测试结果:
start=0 limit=21
storeList为null,返回的数据列表:null
------------------------------------------------------
start=20 limit=1
获取数据的起始位置大于等于storeList的size,返回的数据列表:null
------------------------------------------------------
start=0 limit=25
storeList有20条数据,从0取25条,storeRerurnList.size(): 20
storeList有20条数据,从0取25条,第一条Store [storeId=1, storeName=_store_, longitude=113.85786457474, latitude=23.7635746576, storeLogo=mobile/1455388787.jpg]
------------------------------------------------------
start=5 limit=25
storeList有20条数据,从5取25条,storeRerurnList.size(): 15
storeList有20条数据,从5取25条,第一条Store [storeId=6, storeName=_store_, longitude=113.85786457474, latitude=23.7635746576, storeLogo=mobile/335472957.jpg]
---------------------循环获取所有数据---------------------
start=0 limit=5
storeList有20条数据,从0取5条,storeRerurnList.size(): 5
storeList有20条数据,从0取5条,第一条Store [storeId=1, storeName=_store_, longitude=113.85786457474, latitude=23.7635746576, storeLogo=mobile/1455388787.jpg]
------------------------------------------------------
start=5 limit=5
storeList有20条数据,从5取5条,storeRerurnList.size(): 5
storeList有20条数据,从5取5条,第一条Store [storeId=6, storeName=_store_, longitude=113.85786457474, latitude=23.7635746576, storeLogo=mobile/335472957.jpg]
------------------------------------------------------
start=10 limit=5
storeList有20条数据,从10取5条,storeRerurnList.size(): 5
storeList有20条数据,从10取5条,第一条Store [storeId=11, storeName=_store_, longitude=113.85786457474, latitude=23.7635746576, storeLogo=mobile/1985193589.jpg]
------------------------------------------------------
start=15 limit=5
storeList有20条数据,从15取5条,storeRerurnList.size(): 5
storeList有20条数据,从15取5条,第一条Store [storeId=16, storeName=_store_, longitude=113.85786457474, latitude=23.7635746576, storeLogo=mobile/1439772159.jpg]
------------------------------------------------------
start=20 limit=5
null
根据start和limit从已有的数据列表中获取从start开始的limit个数据的更多相关文章
- MySQL中获取天、周、月等数据
MySQL中获取天.周.月等数据 1.今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 2.昨天 SELECT * FROM 表名 ...
- c# gridview数据列表中NamingContainer容器的用法
当在前台我们绑定了一个linkbutton型的按钮,并触发了onserverclick="delBook_Server"的服务端事件,前台代码如下: <asp:Templat ...
- 数据科学中的R和Python: 30个免费数据资源网站
1 政府数据 Data.gov:这是美国政府收集的数据资源.声称有多达40万个数据集,包括了原始数据和地理空间格式数据.使用这些数据集需要注意的是:你要进行必要的清理工作,因为许多数据是字符型的或是有 ...
- vue 在已有的购买列表中(数据库返回的数据)修改商品数量
连续加班一个月 连续通宵三天 到最后还是少了一个功能 心碎 简介:一个生成好的商品列表(数据库返回的数据) 首先拿到我们需要渲染的数组 在data中定义 我是在测试的时候 直接写了两条数据 下面开始 ...
- Winfrom treeview 如何从多个数据表中获取数据动态生成
本文转载:http://www.cnblogs.com/VincentLuo/archive/2008/03/29/1128987.html 在 汪洋怡舟的这篇文章中[http://www.cnblo ...
- 【OData】使用Odata获取数据之后再次获取可能得不到最新的数据问题记录
工作上遇到个问题是关于系统后台数据库更新了某数据后, 前台界面刷新显示的不是最新的数据.但是大约10分后再次刷新就能显示新的数据,或者重启IIS等web server host. 最开始认为可能是因为 ...
- 在 easyui中获取form表单中所有提交的数据 拼接到table列表中
form表单===== <!-- 并用药品填写信息弹框 --> <div id="usingProdctMsgDiv" style="display: ...
- 获取某个数据所在数据列表中的行数 mysql
select * from (select @rownum := @rownum+1 as rownum,goods_idfrom table_goods, (select @rownum:=0) t ...
- RadioButton在数据列表中实现单选功能
在服务器端动态生成的name总是不断变化,故需手动实现单选 ; <html xmlns="http://www.w3.org/1999/xhtml"> <head ...
随机推荐
- LOJ P10150 括号配对 题解
Analysis 区间dp裸题 初始化有点麻烦 i,j能匹配时要特判 #include<iostream> #include<cstdio> #include<cstri ...
- mongodb mongod.lock文件及oplog文件
在mongodb的启动时,在数据目录下,会生成一个mongod.lock文件.如果在正常退出时,会清除这个mongod.lock文件,若要是异常退出,在下次启动的时候,会禁止启动,从而保留一份干净的一 ...
- jQuery相关方法6----三大系列属性
一.获取和设置元素的宽和高------width( )方法和height()方法 <!-- 点击按钮,设置div的宽和高为原来的两倍 --> <script src="ht ...
- [Luogu] 受欢迎的牛
https://www.luogu.org/problemnew/show/P2341 Tarjan 缩点 + 判断出度 #include <iostream> #include < ...
- linux系列(十二):more命令
1.命令格式: more [-dlfpcsu ] [-num ] [+/ pattern] [+ linenum] [file] 2.命令功能: more命令和cat的功能一样都是查看文件里的内容,但 ...
- YII框架的类自动加载机制
YII之所以能实现快速的自动加载类文件,是因为它通过两种途径来实现. 先看文件 vendor\yiisoft\yii2\BaseYii.php 里面的 autoload 方法 public stati ...
- vue的基本用法
公共样式---pc版的404报错 动态src 这个是vue组件template部分 <div class="not-found"> <img :src=" ...
- javaSE集合---进度2
一.集合框架 1.特点 对象封装数据,对象多了也需要存储,集合用于存储对象. 对象的个数确定可以使用数组,但是不确定的话,可以用集合,因为集合是可变长度的. 2.集合和数组的区别 数组是固定长度的,集 ...
- 20175313 张黎仙《Java综合讲座》第十三周课堂测试总结
目录 一.JAVA中两大类型 二.基本类型与类类型的相互转化 三.int与Integer之间的区别 四.String.StringBuffer.StringBuilder三者之间的区别 五.Array ...
- Linux系统中rm删除命令
rm命令 1.可以删除一个目录中的一个或多个文件或目录 2.可以将某个目录及其下属的所有文件及其子目录均删除掉 3.对于链接文件,只是删除整个链接文件,而原有文件保持不变 语法 rm (选项)(参数) ...