reverse an array

Z

zero

Hi everyone,

I'm trying to reverse an array of bytes using Collections.reverse:

<code>
byte[] theBytes = { 1, 2, 3 };
System.out.println( Arrays.toString( theBytes ) );
Collections.reverse( Arrays.asList( theBytes ) );
System.out.println( Arrays.toString( theBytes ) );
</code>

However, his prints
[1, 2, 3]
[1, 2, 3]
ie, the array is not reversed at all. When however I do the exact same
thing with a String array, it works:

<code>
String[] theStrings = { "Hello", "World" };
System.out.println( Arrays.toString( theStrings ) );
Collections.reverse( Arrays.asList( theStrings ) );
System.out.println( Arrays.toString( theStrings ) );
</code>

This gives the (expected) output:
[Hello, World]
[World, Hello]

What am I missing here?

TIA,
zero
 
R

Roland de Ruiter

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.

Compare it with the following, except you had just one element in
theByteArrays:

byte[] theBytes = { 1, 2, 3 };
byte[] theBytes2 = { 4, 5, 6 };
Object[] theByteArrays = {theBytes, theBytes2};
System.out.println(Arrays.deepToString(theByteArrays));
// similar to:
// System.out.println(Arrays.deepToString(theBytes, theBytes2));
Collections.reverse(Arrays.asList(theByteArrays));
// similar to:
// Collections.reverse(Arrays.asList(theBytes, theBytes2));
System.out.println(Arrays.asList(theByteArrays).size());
System.out.println(Arrays.deepToString(theByteArrays));

And print the size of the lists:
System.out.println(Arrays.asList(theBytes).size());
System.out.println(Arrays.asList(theStrings).size());
 
Z

zero

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
 
M

Mike Schilling

zero said:
Hi everyone,

I'm trying to reverse an array of bytes using Collections.reverse:

<code>
byte[] theBytes = { 1, 2, 3 };
System.out.println( Arrays.toString( theBytes ) );
Collections.reverse( Arrays.asList( theBytes ) );
System.out.println( Arrays.toString( theBytes ) );
</code>

However, his prints
[1, 2, 3]
[1, 2, 3]
ie, the array is not reversed at all. When however I do the exact
same thing with a String array, it works:

<code>
String[] theStrings = { "Hello", "World" };
System.out.println( Arrays.toString( theStrings ) );
Collections.reverse( Arrays.asList( theStrings ) );
System.out.println( Arrays.toString( theStrings ) );
</code>

This gives the (expected) output:
[Hello, World]
[World, Hello]

What am I missing here?

Arrays.asList() taken an object arry, not an array of scalars. Your program
doesn't even compile in JDK 1.4.

What's happening is apparently that, since theBytes is not the right sort of
array to be the argument to asList, variable-length argument semantics are
being applied, as if you'd coded

Arrays.asList(o, p, q, r, s);

This would construct an array and then call the method, as if you'd coded

Object[] arr = new Object[] {o, p, q, r, s};
Arays.asList(arr);

Similarly, your code becomes (effectively)

Object[] arr = new Object[] {theBytes};
Arrays.asList(arr);

Obviously, sorting a one-member array won't do much.

You've found something truly ugly, by the way. In 1.4, the compiler
correctly tells you that Arrays.asList can't be applied to an array of
scalars. In 1.5, it silently does the wrong thing.
 
R

Roland de Ruiter

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

The actual type argument cannot be a primitive type, only a reference
type (or a wildcard), see
<http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.5.1>
<http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.3>


So you cannot substitute T by int. A list of int, such as
List<int> intList = new ArrayList<int>();
is invalid and won't compile.

Further, it is incorrect that a method *declaration* with a variable
number of arguments is equivalent to a method declaration with the
varargs replaced by the matching array type. For example
public void foo(int... a) {} // *A*
is /not/ equivalent to
public void foo(int[] a) {} // *B*
because the call of method *B* only can take an int[] as actual
parameter (or null), whereas the call of method *A* can take an int[] or
a variable number of int's (possibly 0) as actual parameter:
this.foo(); // valid for *A* invalid for *B*
this.foo(1); // valid for *A* invalid for *B*
this.foo(1,2,3); // valid for *A* invalid for *B*
this.foo(new int[]{1,2,3}); // valid for *A* valid for *B*
this.foo(new int[0]); // valid for *A* valid for *B*

In your original example,
byte[] theBytes = { 1, 2, 3 };
Arrays.asList( theBytes );
the actual type argument T in
public static <T> List<T> asList(T... a)
is not byte, but byte[]. The call
Arrays.asList( theBytes );
therefore has only one argument (like this.foo(1)).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top