tags:

views:

137

answers:

3

Let's say I have array of bytes:

byte[] arr = new byte[] { 0, 1, 2, 3, 4 };

Does platform has functions that I can use to play with this array - for example, how to invert it (get 4,3,2,1,0)? Or, how to invert part of it (2,1,0,3,4)? Get part of array (0,1,2,3)?

I know I can manually write functions but I am curious if I'm missing useful util functions in platform that I should know about (and couldn't find any useful guide using google).

Thanks!

+2  A: 

There are some static methods in the Arrays class (which i don't know availability of on dalvik/android)

Riduidel
Arrays class is available in Android. However it doesn't have an invert() or reverse() method like kape123 is asking for. The Collections class does have this however, if you are working with Collections instead of arrays.
mbaird
So, calling Arrays.asList will reveal to be useful ;-)
Riduidel
+1  A: 

java.util.Arrays contains a couple of helper methods (sort(), equals(), fill(), toString()). If that isn't sufficient, you can convert the byte primitive to a Byte object (create a new array) and use new ArrayList (Arrays.asList()) to get a collection which you can modify with the many methods from java.util.Collections.

If you look outside of the RT, there is Commons Primitives:

Commons Primitives provides a library of collections and utilities specially designed for use with primitive types.

Another option is the guava-libraries project which has some support for primitives as well:

This project contains several of Google's core Java libraries that we use in our myriad Java projects.

Aaron Digulla
+1  A: 

java.util.Arrays has some array-related utility methods in the standard API. ArrayUtils from Apache Commons Lang has a lot more.

Note however, that Java does not have the concept of array slices as "views", i.e. a "part of an array" returned by such a method will always be a copy.

Michael Borgwardt