代码记录(需求:根据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个数据的更多相关文章

  1. MySQL中获取天、周、月等数据

    MySQL中获取天.周.月等数据 1.今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 2.昨天 SELECT * FROM 表名 ...

  2. c# gridview数据列表中NamingContainer容器的用法

    当在前台我们绑定了一个linkbutton型的按钮,并触发了onserverclick="delBook_Server"的服务端事件,前台代码如下: <asp:Templat ...

  3. 数据科学中的R和Python: 30个免费数据资源网站

    1 政府数据 Data.gov:这是美国政府收集的数据资源.声称有多达40万个数据集,包括了原始数据和地理空间格式数据.使用这些数据集需要注意的是:你要进行必要的清理工作,因为许多数据是字符型的或是有 ...

  4. vue 在已有的购买列表中(数据库返回的数据)修改商品数量

    连续加班一个月  连续通宵三天 到最后还是少了一个功能 心碎 简介:一个生成好的商品列表(数据库返回的数据) 首先拿到我们需要渲染的数组 在data中定义 我是在测试的时候 直接写了两条数据 下面开始 ...

  5. Winfrom treeview 如何从多个数据表中获取数据动态生成

    本文转载:http://www.cnblogs.com/VincentLuo/archive/2008/03/29/1128987.html 在 汪洋怡舟的这篇文章中[http://www.cnblo ...

  6. 【OData】使用Odata获取数据之后再次获取可能得不到最新的数据问题记录

    工作上遇到个问题是关于系统后台数据库更新了某数据后, 前台界面刷新显示的不是最新的数据.但是大约10分后再次刷新就能显示新的数据,或者重启IIS等web server host. 最开始认为可能是因为 ...

  7. 在 easyui中获取form表单中所有提交的数据 拼接到table列表中

    form表单===== <!-- 并用药品填写信息弹框 --> <div id="usingProdctMsgDiv" style="display: ...

  8. 获取某个数据所在数据列表中的行数 mysql

    select * from (select @rownum := @rownum+1 as rownum,goods_idfrom table_goods, (select @rownum:=0) t ...

  9. RadioButton在数据列表中实现单选功能

    在服务器端动态生成的name总是不断变化,故需手动实现单选 ; <html xmlns="http://www.w3.org/1999/xhtml"> <head ...

随机推荐

  1. 三十一.MySQL存储引擎 、 数据导入导出 管理表记录 匹配条件

    1.MySQL存储引擎的配置 查看服务支持的存储引擎 查看默认存储类型 更改表的存储引擎 设置数据库服务默认使用的存储引擎 1.1 查看存储引擎信息 mysql> SHOW ENGINES\G ...

  2. LibreOJ #115. 无源汇有上下界可行流

    二次联通门 : LibreOJ #115. 无源汇有上下界可行流 /* LibreOJ #115. 无源汇有上下界可行流 板子题 我也就会写写板子题了.. */ #include <cstdio ...

  3. 永远不会被卡的Dinic

    78,79行是精髓 61,148,149行是当前弧优化 #include <cstring> #include <cstdio> #include <queue> ...

  4. NetworkX系列教程(10)-算法之四:拓扑排序与最大流问题

    小书匠Graph图论 重头戏部分来了,写到这里我感觉得仔细认真点了,可能在NetworkX中,实现某些算法就一句话的事,但是这个算法是做什么的,用在什么地方,原理是怎么样的,不清除,所以,我决定先把图 ...

  5. properties的编码问题

    1.在eclipse中,如果不专门设置,properties文件的编码是ISO-8859-1,最好将其改为UTF-8 2.当properties文件的编码改为UTF-8还不够,Spring的conte ...

  6. GB∕T 35658平台过检 已通过最新的部标JT/T 808-2019, JT/T 809-2019标准检测

    2019年交通部GPS平台检测标准发生了重大变化, 原来的796平台功能标准, 变更为GB/T35658标准, 这个标准其实2017年就公布了, 实际上还是796标准, 但是检测项目,以前是可选的, ...

  7. HttpClient学习(一)—— 基本使用

    HttpClient是支持Http协议的客户端编程工具包. 一.简单使用 1.1 引入依赖 <dependency> <groupId>org.apache.httpcompo ...

  8. gacutil.exe的位置

    如果我们需要用gacutil去注册dll ,就需要使用Visual Studio的Command Prompt,前提是需要安装Visual Studio,但是客户端上一般是没有安装VS的,所以你就需要 ...

  9. BAT文件运行时不显示命令窗口的方法

    可以编一个VBS文件调用BAT文件,使运行BAT文件时不显示命令窗口. 新建一个记事本文件,保存为abc.vbs,在文件中加入如下代码: Set shell = Wscript.createobjec ...

  10. matlab中x.^2与x^2有什么区别?

    .^2是矩阵中的每个元素都求平方,^2是求矩阵的平方或两个相同的矩阵相乘,因此要求矩阵为方阵,且看下面的例子x=1:4x = 1 2 3 4 x.^2 ans = 1 4 9 16 x^2 Error ...