Created
March 7, 2017 23:16
-
-
Save andymccall/e28f2ae994a2925abc31f9d85d7584a8 to your computer and use it in GitHub Desktop.
Pagination
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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