Cannot use Arrays.copyOfRange

It seems I can not 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 copyOfRange(int[], int, int) method is undefined for the Arrays type

I do not understand, because the link to Android Arrays includes this method for arrays.

Any ideas?

+7
source share
3 answers

The Arrays.copyOfRange() method was not introduced until API level 9 . Make sure you use this as the minimum SDK.

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

Change the code as follows:

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

If you need to use APIs older than 9, System.arraycopy and Math.min were added at 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; } 
+2
source

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

-2
source

All Articles