3

I have an array that I need to split in different arrays. I have an array of Strings and need to split it in different pages (different arrays).

At first, I get the length of the array, using

int size = array.length;

And then, I get the number of pages I need, knowing that each page should only have 10 Strings

int numberOfPages = (int) Math.floor(size/10);

The user then select which page he wants to see

int pageSelected = 2;

After that, I tried to split the array, but got some exceptions. I tried:

Arrays.copyOfRange(array,(0+10*(pageSelected-1),10*10+(pageSelected-1)));

I get an exception when I try to print the values of the new array.

Is there anyway to split an array in 'pages', and display these 'pages' as requestes?

@Edit1 I get a Nullpointer Exception

8
  • What exception do you get? Commented Dec 30, 2014 at 23:45
  • What Exception do you get. Where do you get it? Commented Dec 30, 2014 at 23:45
  • Could you paste stacktrace? Commented Dec 30, 2014 at 23:46
  • The exception is a Nullpointer The stacktrace is useless, as it was generated by Bukkit, and has no information regarding the code Commented Dec 30, 2014 at 23:49
  • 2
    @j_v_wow_d: it should be Math.ceil((double) size/10) because an integer division already floors. Commented Dec 31, 2014 at 0:43

4 Answers 4

3

The error probably occurs in this line:

Arrays.copyOfRange(array,(0+10*(pageSelected-1),10*10+(pageSelected-1)));

Where there is an error with the brackets (the method requires three arguments, but from the method's perspective, you only provide two: the last two are grouped by brackets). You can use:

Arrays.copyOfRange(array,10*(pageSelected-1),10*10+(pageSelected-1));

(removed 0+ since this has no use).

Furthermore you made a semantical error: 10*10+(pageSelected-1) should be replaced by: 10+10*(pageSelected-1

So the full line reads:

Arrays.copyOfRange(array,10*(pageSelected-1),10+10*(pageSelected-1));

Although a better guideline would be to use small steps:

int i = pageSelected-1;
int g = 10*i;
Arrays.copyOfRange(array,g,g+10);//do something with the result

And to do it perfect, you better use variables for constants such that - if you change your mind - you can easily modify the number of items per page:

int i = pageSelected-1;
int perpage = 10;
int g = perpage*i;
Arrays.copyOfRange(array,g,g+perpage);//do something with the result

Finally a small remark: as @j_v_wow_d says, you should ceil the division, otherwise you will generate one page for 11 items. The correct code for numberOfPages is thus:

int numberOfPages = (int) Math.ceil((double) size/perpage);
Sign up to request clarification or add additional context in comments.

5 Comments

for my learning goal, can you explain what 10*(pageSelected-1) means how did the op come up with it?
10 is the number of items per page. The OP uses an offset of 1 of pageSelected. Thus you decrement it to get a Java array-index. To get the real page (pageSelected-1). Then you multiply it by 10 because every page has 10 items. To get the offset item.
and each page is in each index of an array or from zero to nine counts as a page?
I guess the OP processes the subarray by printing a webpage or something showing these items. Now of course one can also do the math by using indices with the calculated offset. By copying it however, things can be done a bit simpler.
great explanation I think you should add this to your post to be more perfect thank you
2

If the pageSelected is indexed so that 0 is the first:

String[] array = {"a", "b", "c", "d", "e", "f"};
int pageSize = 2;
int pageSelected = 2;
final String[] pageData =
        Arrays.copyOfRange(
                array,
                (pageSelected * pageSize),
                (pageSelected * pageSize) + pageSize);

The outcome of this is that pageData contains ["e", "f"].

Comments

1

You could just do size / 10; no need to floor or cast it.

Also, the from expression 10*10+(pageSelected-1) should be 10 + (10 * (pageSelected - 1)).

1 Comment

And a small brackets error... But that's indeed not the important part.
1
Arrays.copyOfRange(array,(0+10*(pageSelected-1),10*10+(pageSelected-1)));

I think you mean this :)

Arrays.copyOfRange(array,0+10*(pageSelected-1),10+10*(pageSelected-1));

Look at the position of the + and * after the second comma.

But, it would be easier to read if it was not a one-liner

int offset = 10 * (pageSelected - 1);
Arrays.copyOfRange(array, offset, offset + 10);

Or even better

public static final int PER_PAGE = 10; 

/* (...) */

int offset = PER_PAGE * (pageSelected - 1);
Arrays.copyOfRange(array, offset, offset + PER_PAGE);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.