Is there any method in java to point the cursor upto 10th record in first page like JDBC methods

presently  array size is 23 it will increase dynamically;

i want to display this numbers in 3 pages.

in first page i will display from 1 to 10 numbers ,when click next it should display from 11 to 20 and finally click next remaining 3 records at the last page(if user clicks previous it should show previous records).these records are not coming from the database.so i cant use scrollable resultset in this case.


Is there any method in java to point the cursor upto 10th record in first page(because i will display only 10 records in first page;).when user clicks next, cursor should point from 11 record onwards.(like JDBC absolute() scrollable result methods)
LVL 20
chaitu chaituAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vsubram2Commented:
>> presently  array size is 23 it will increase dynamically.
I don't think any java object provides you the functionality as u expected.
but
you can keep it as  a java collection ,say List and store all ur objects,say 23 objects in this case .

you can have pageStartIndex which u can calculate from the pageNumber.

since the page size is
 ie)numberOfRecords= 10;
pageStartIndex=(pageNumbe-1)r*numberOfRecords;

to check the limit ,that is we should not exceed the existing objects size
if(pageStartIndex+numberOfRecords <= list.size())
endIndex=pageStartIndex+numberOfRecords;
else
endIndex=list.size();
and u can the sublist with listObject.sublist(pageStartIndex,);


you are at the page 1 >>>>>> pageStartIndex=0, listObject.sublist(0,10);
page 2>>>> > pageStartIndex=10, listObject.sublist(10,20);
page 3>>>>>pageStartIndex=20,listObject.sublist(20,23); since  your endindex exceeds objects size make the endindex to objects size.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
>>pageStartIndex=(pageNumbe-1)r*numberOfRecords;

I think you meant what i've given below and i don't think the following could be right

>>
else
endIndex=list.size();
>>

This gives the index

      int listIndex = (pageNumber - 1) * pageSize;

and if you exceed or fall below a possible index, you can wrap:

      listIndex = (listIndex >= 0 && listIndex < numberOfRecords)? listIndex : 0;

Also, bear in mind that using a List may not scale well, so you may like to manipulate your objects without holding them all in memory. A RandomAccessFile may be OK
vsubram2Commented:
pageStartIndex=(pageNumbe-1)r*numberOfRecords;
yeah the above is really typo.sorry.

remaining things looks ok ,and i tested .
ie)numberOfRecords= 10;  ---->no of records to be displayed in a page.
if it is constant ,you can keep it java program or u can get it from a properties file.

pageStartIndex=(pageNumber-1)*numberOfRecords;

to check the limit ,that is we should not exceed the existing objects size
this check is for the last page ,3rd page here, which hold only three records .

if(pageStartIndex+numberOfRecords <= list.size())
endIndex=pageStartIndex+numberOfRecords;
else
endIndex=list.size(); // it is the size of the list .
and u can the sublist with
 listObject.sublist(pageStartIndex,endIndex); which will actually give u the list of all elements from pageStartIndex to endIndex-1.


>>A RandomAccessFile may be OK .yeah that's true.if your number of resultant objects is more ,it is not good to keep in memory. we can use random acess file and move the cursor with  RAF.seek.

Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Kannan EkanathCommented:
This is a problem called "pagination" of database results. Here, you have a page in the UI showing 40 rows or more. You want to show only 10 rows at a time with previous and next buttons. There are code snippets available for that, you just have to give the datasource and query and parameters like 10(number of rows in a page)

Check these links,

http://www.javaworld.com/javaworld/jw-07-2004/jw-0726-pagination.html
http://developers.sun.com/learning/javaoneonline/2004/javatechweb/TS-1341.pdf
Common frameworks like Common Controls, strutslogic
Kannan EkanathCommented:
In case you are willing to *actually* get the results from db into memory some fwks like
http://struts.application-servers.com/ 

have tags which will directly give you what you want

Kannan EkanathCommented:
In case you have the complete list in memory, you just have to write a TLD and a Tag action class, which is the most *non intrusive* of achieving this.

The url i pointed above has some sample code on how to write such tag libraries
chaitu chaituAuthor Commented:
vsubram2,

thanks for introducing the new method in List.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.