Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save andymccall/e28f2ae994a2925abc31f9d85d7584a8 to your computer and use it in GitHub Desktop.

Select an option

Save andymccall/e28f2ae994a2925abc31f9d85d7584a8 to your computer and use it in GitHub Desktop.
Pagination
public class Main {
public static void main(String[] args) {
int numberOfPosts = 45;
int pageSize = 3;
int numberOfPages = numberOfPosts / pageSize;
int lastPage = 0;
int firstPage = 0;
for (int i = 1; i < numberOfPages; i++) {
if (i > lastPage) {
lastPage = (lastPage + pageSize);
firstPage = lastPage - pageSize + 1;
if (lastPage > numberOfPages) {
lastPage = numberOfPages;
firstPage = lastPage - pageSize + 1;
}
}
System.out.println("Current Page: " + i);
System.out.println("First Page: " + firstPage);
System.out.println("Last Page: " + lastPage);
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment