java中利用List的subList方法实现对List分页(简单易学)

以下是介绍利用List的subList方法实现对List分页,废话不多说了,直接看代码把

java" id="highlighter_915564">

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

/** *//**

* List分页

* 实现:利用List的获取子List方法,实现对List的分页

* @author 显武

* @date 2010-1-8 16:27:31

*

*/

import java.util.ArrayList;

import java.util.List;

public class PageModel {

private int page = 1; // 当前页

public int totalPages = 0; // 总页数

private int pageRecorders;// 每页5条数据

private int totalRows = 0; // 总数据数

private int pageStartRow = 0;// 每页的起始数

private int pageEndRow = 0; // 每页显示数据的终止数

private boolean hasNextPage = false; // 是否有下一页

private boolean hasPreviousPage = false; // 是否有前一页

private List list;

// private Iterator it;

public PageModel(List list, int pageRecorders) {

init(list, pageRecorders);// 通过对象集,记录总数划分

}

/** *//**

* 初始化list,并告之该list每页的记录数

* @param list

* @param pageRecorders

*/

public void init(List list, int pageRecorders) {

this.pageRecorders = pageRecorders;

this.list = list;

totalRows = list.size();

// it = list.iterator();

hasPreviousPage = false;

if ((totalRows % pageRecorders) == 0) {

totalPages = totalRows / pageRecorders;

} else {

totalPages = totalRows / pageRecorders + 1;

}

if (page >= totalPages) {

hasNextPage = false;

} else {

hasNextPage = true;

}

if (totalRows < pageRecorders) {

this.pageStartRow = 0;

this.pageEndRow = totalRows;

} else {

this.pageStartRow = 0;

this.pageEndRow = pageRecorders;

}

}

// 判断要不要分页

public boolean isNext() {

return list.size() > 5;

}

public void setHasPreviousPage(boolean hasPreviousPage) {

this.hasPreviousPage = hasPreviousPage;

}

public String toString(int temp) {

String str = Integer.toString(temp);

return str;

}

public void description() {

String description = "共有数据数:" + this.getTotalRows() +

"共有页数: " + this.getTotalPages() +

"当前页数为:" + this.getPage() +

" 是否有前一页: " + this.isHasPreviousPage() +

" 是否有下一页:" + this.isHasNextPage() +

" 开始行数:" + this.getPageStartRow() +

" 终止行数:" + this.getPageEndRow();

System.out.println(description);

}

public List getNextPage() {

page = page + 1;

disposePage();

System.out.println("用户凋用的是第" + page + "页");

this.description();

return getObjects(page);

}

/** *//**

* 处理分页

*/

private void disposePage() {

if (page == 0) {

page = 1;

}

if ((page - 1) > 0) {

hasPreviousPage = true;

} else {

hasPreviousPage = false;

}

if (page >= totalPages) {

hasNextPage = false;

} else {

hasNextPage = true;

}

}

public List getPreviousPage() {

page = page - 1;

if ((page - 1) > 0) {

hasPreviousPage = true;

} else {

hasPreviousPage = false;

}

if (page >= totalPages) {

hasNextPage = false;

} else {

hasNextPage = true;

}

this.description();

return getObjects(page);

}

/** *//**

* 获取第几页的内容

*

* @param page

* @return

*/

public List getObjects(int page) {

if (page == 0)

this.setPage(1);

else

this.setPage(page);

this.disposePage();

if (page * pageRecorders < totalRows) {// 判断是否为最后一页

pageEndRow = page * pageRecorders;

pageStartRow = pageEndRow - pageRecorders;

} else {

pageEndRow = totalRows;

pageStartRow = pageRecorders * (totalPages - 1);

}

List objects = null;

if (!list.isEmpty()) {

objects = list.subList(pageStartRow, pageEndRow);

}

//this.description();

return objects;

}

public List getFistPage() {

if (this.isNext()) {

return list.subList(0, pageRecorders);

} else {

return list;

}

}

public boolean isHasNextPage() {

return hasNextPage;

}

public void setHasNextPage(boolean hasNextPage) {

this.hasNextPage = hasNextPage;

}

public List getList() {

return list;

}

public void setList(List list) {

this.list = list;

}

public int getPage() {

return page;

}

public void setPage(int page) {

this.page = page;

}

public int getPageEndRow() {

return pageEndRow;

}

public void setPageEndRow(int pageEndRow) {

this.pageEndRow = pageEndRow;

}

public int getPageRecorders() {

return pageRecorders;

}

public void setPageRecorders(int pageRecorders) {

this.pageRecorders = pageRecorders;

}

public int getPageStartRow() {

return pageStartRow;

}

public void setPageStartRow(int pageStartRow) {

this.pageStartRow = pageStartRow;

}

public int getTotalPages() {

return totalPages;

}

public void setTotalPages(int totalPages) {

this.totalPages = totalPages;

}

public int getTotalRows() {

return totalRows;

}

public void setTotalRows(int totalRows) {

this.totalRows = totalRows;

}

public boolean isHasPreviousPage() {

return hasPreviousPage;

}

}

//测试类:

package com.Gavin.tools.util;

import java.util.ArrayList;

import java.util.List;

public class TestPagerModel {

public static void main(String args[]) {

List<String> list = new ArrayList<String>();

list.add("a");

list.add("b");

list.add("c");

list.add("d");

list.add("e");

list.add("f");

list.add("g");

list.add("h");

list.add("h");

list.add("i");

list.add("j");

list.add("k");

list.add("l");

list.add("m");

PageModel pm = new PageModel(list, 5);

List sublist = pm.getObjects(3);

for(int i = 0; i < sublist.size(); i++) {

System.out.println(sublist.get(i));

}

System.out.println(sublist.get(0));

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

本文链接:https://my.lmcjl.com/post/13444.html

展开阅读全文

4 评论

留下您的评论.