aez
aez

Reputation: 2396

can't use Arrays.copyOfRange

I don't seem to be able to access Arrays.copyOfRange in my Android project in Eclipse Indigo 3.7.1 On Ubuntu 11.10.

My JRE is java-6-openjdk which I thought included Arrays.copyOfRange

For example, if I have this code:

int[] debug = new int[5];
int[] x = Arrays.copyOfRange(debug,0,4);

Eclipse tells me

The method copyOfRange(int[], int, int) is undefined for the type Arrays

I don't understand because the Android reference Arrays includes this method for Arrays.

Any ideas?

Upvotes: 7

Views: 6947

Answers (3)

PbxMan
PbxMan

Reputation: 7623

If you need to use an API older than 9, System.arraycopy and Math.min was added in API level 1, so you can copy the copyOf function and use it in your code.

public static byte[] copyOf(byte[] original, int newLength) {
    byte[] copy = new byte[newLength];
    System.arraycopy(original, 0, copy, 0,
                     Math.min(original.length, newLength));
    return copy;
}

Upvotes: 4

slayton
slayton

Reputation: 20309

The method Arrays.copyOfRange() wasn't introduced until API level 9. Make sure you are using that as the minimum SDK.

Also, you are indexing incorrectly. In java if you have an array of size 5 the indices range from 0->4

Change your code to this:

int[] debug = new int[5];
int[] x = Arrays.copyOfRange(debug,0,4); // use 4 instead of 5

Upvotes: 7

Stefan
Stefan

Reputation: 4705

Arrays.copyOfRange has been introduced to the Arrays class with Java 6. Android is based on Java 5. You cannot use Java 6 methods or classes with Android.

Upvotes: -2

Related Questions