Re: reverse an array
Roland de Ruiter <roland.de.ruiter@example.invalid> wrote in
news:44e82b8f$0$4515$e4fe514c@news.xs4all.nl:
On 20-8-2006 10:47, zero wrote:
Hi everyone,
I'm trying to reverse an array of bytes using Collections.reverse:
byte[] theBytes = { 1, 2, 3 };
System.out.println( Arrays.toString( theBytes ) );
Collections.reverse( Arrays.asList( theBytes ) );
System.out.println( Arrays.toString( theBytes ) );
[...]
String[] theStrings = { "Hello", "World" };
System.out.println( Arrays.toString( theStrings ) );
// similar to:
// System.out.println( Arrays.toString( "Hello", "World" ));
Collections.reverse( Arrays.asList( theStrings ) );
// similar to:
// Collections.reverse( Arrays.asList( "Hello", "World" ) );
System.out.println( Arrays.toString( theStrings ) );
[...]
What am I missing here?
The Arrays.toString and Arrays.asList methods take a variable number of
arguments (varargs) of *reference types*, not of primitive types.
Seems to me like the 1.5 release broke the Arrays.asList function.
Compare the following two functions:
void test( int... list )
{
System.out.println( list.getClass() );
}
void test( int[] list )
{
System.out.println( list.getClass() );
}
Both will give the same output, indicating that a vararg of ints is
exactly the same as an array of ints, which is consistent with the
language guide documentation(1) which states: "It is still true that
multiple arguments must be passed in an array, but the varargs feature
automates and hides the process."
The signature of the Arrays.asList function is:
public static <T> List<T> asList(T... a)
Or, if T = int:
public static List<int> asList(int...a)
which should be completely equivalent with
public static List<int> asList(int[] a)
However, as my original post shows, passing an array of primitive values
results in a list of arrays, as if the signature were:
public static List<int[]> asList(int[] a)
or, equivalently:
public static List<int[]> asList(int... a)
and thus:
public static <T> List<T[]> asList(T... a)
If there is a flaw in my reasoning please let me know. If not, this is
clearly a bug.
The documentation of the Arrays.asList function also states that its sole
argument is an array, consistent with the fact that varargs and arrays
are the same. There is no mention of a difference between reference and
primitive types - and there should be no such difference.
(1) http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html