TechTalkz.com Logo Ask the Experts!

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > Java

Arrays.asList() doesn't work quite like I would think it should

Java

 
 
Thread Tools Display Modes
Unread 03-10-2007, 04:00 AM   #1
Ken
Guest
 
Posts: n/a
Arrays.asList() doesn't work quite like I would think it should

I've included an example of the problem I've run into. I'm trying to
randomize an array of integers, much like the following program does.
Unfortunately when I ask the system to do so, I don't get a randomized
array. Now normally this wouldn't bother me. I would just assume that
a new object is being created and then destroyed by the garbage collector
without my ever seeing it, but in this case it looks like a bug.

Isn't the whole point of the asList() interface to allow this kind of
thing?

Is this a bug in Java?

--
Kenneth P. Turvey <kt-usenet@squeakydolphin.com>

--------------------------------------------------------------------

import java.util.Arrays;
import java.util.Collections;

public class ArrayAsListTester {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int indicies[] = new int[100];

for (int index = 0; index < 100; index++) {
indicies[index] = index;
}

Collections.shuffle(Arrays.asList(indicies));

for (int index = 0; index < 100; index++) {
System.out.println(indicies[index]);
}
}
}
 
Unread 03-10-2007, 04:59 AM   #2
Stefan Ram
Guest
 
Posts: n/a
Re: Arrays.asList() doesn't work quite like I would think it should

Ken <none@nowhere.com> writes:
>--


Writing your program in front of the signature separator
will enhance the visibility of your code.

>int indicies[] = new int[100];


Use »java.lang.Integer indicies[] = new java.lang.Integer[ 100 ];« here.

Otherwise, »asList« will see an array with only one component
»indicies« because its parameter has type »T...«.

Using an English word like »indices« (instead of
»indicies«) will enhance the readability of your code.

 
Unread 03-10-2007, 04:59 AM   #3
xen
Guest
 
Posts: n/a
Re: Arrays.asList() doesn't work quite like I would think it should

On 03 Oct 2007 03:24:42 GMT, Ken <none@nowhere.com> wrote:

>I've included an example of the problem I've run into. I'm trying to
>randomize an array of integers, much like the following program does.
>Unfortunately when I ask the system to do so, I don't get a randomized
>array. Now normally this wouldn't bother me. I would just assume that
>a new object is being created and then destroyed by the garbage collector
>without my ever seeing it, but in this case it looks like a bug.
>
>Isn't the whole point of the asList() interface to allow this kind of
>thing?
>
>Is this a bug in Java?


Arrays.asList() tries to instantiate an ArrayList on the specified
array but an ArrayList can never be backed by an int[].

If you call asList() on an int[], you will get a List<int[]> with one
element: the int[] you supplied. If you call shuffle on that list,
nothing will change in the int[].

You don't get an error because the parameter for asList is a "T... a".
If you call it with a T[], that array is passed.
If you call it with an array of a primitive type, then it cannot be
treated as a T[] because generic types cannot be primitives. Instead,
because int[] itself is an object, it matches the T in the signature
and is wrapped inside an array. So what actually gets passed is not
your int[], but an array of int[] with one element.

There is no way for the compiler to generate an error.
And why should ArrayList(T... t) complain? An int[] is a valid type
parameter.

Perhaps the compiler should generate a warning if you pass an array of
primitive to a generic dynamic array parameter.

Anyway, you can only solve it by using an Integer[] instead of an
int[].

 
Unread 03-10-2007, 04:59 AM   #4
Mark Space
Guest
 
Posts: n/a
Re: Arrays.asList() doesn't work quite like I would think it should

Ken wrote:
> I've included an example of the problem I've run into. I'm trying to
> randomize an array of integers, much like the following program does.
> Unfortunately when I ask the system to do so, I don't get a randomized
> array. Now normally this wouldn't bother me. I would just assume that
> a new object is being created and then destroyed by the garbage collector
> without my ever seeing it, but in this case it looks like a bug.
>
> Isn't the whole point of the asList() interface to allow this kind of
> thing?
>
> Is this a bug in Java?
>


I doubt it. A more experienced Java programmer would spot this right away.

I didn't run your program (which is a shame since you did include a good
one), but I'm gonna guess that the output is a sorted, not randomized list.

Not a Java bug.

asList returns a NEW OBJECT. Your original array was never touched. Try
something like:

List myList = Arrays.asList(indicess);
Collections.shuffle(myList);

Then print out myList and see what you get.
 
Unread 03-10-2007, 04:59 AM   #5
Stefan Ram
Guest
 
Posts: n/a
Re: Arrays.asList() doesn't work quite like I would think it should

Mark Space <markspace@sbc.global.net> writes:
>asList returns a NEW OBJECT. Your original array was never touched.


In German, there is a saying »Wer schreit hat Unrecht.«¹. Thus,

»Changes to the returned list "write through" to the array.«

http://download.java.net/jdk7/docs/a....html#asList(T...)

1) »He who shouts is wrong.«. In Usenet, using
all caps sometimes is considered to resemble shouting.

 
Unread 03-10-2007, 05:59 AM   #6
Roedy Green
Guest
 
Posts: n/a
Re: Arrays.asList() doesn't work quite like I would think it should

On 03 Oct 2007 03:24:42 GMT, Ken <none@nowhere.com> wrote, quoted or
indirectly quoted someone who said :

> Collections.shuffle(Arrays.asList(indicies));


asList makes a copy. So you have shuffled a new copy of the data in a
List then thrown it away. Your code does not shuffle the original
array.


--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Unread 03-10-2007, 05:59 AM   #7
Roedy Green
Guest
 
Posts: n/a
Re: Arrays.asList() doesn't work quite like I would think it should

On 3 Oct 2007 04:30:51 GMT, ram@zedat.fu-berlin.de (Stefan Ram) wrote,
quoted or indirectly quoted someone who said :

> »Changes to the returned list "write through" to the array.«


That was a surprise. I looked up Arrays.asList and there it was.

It also says .

This method also provides a convenient way to create a fixed-size list
initialized to contain several elements.

This means insert and delete would be suppressed. Shuffle just might
try to use those methods.

I got caught because I always use these idioms:

ArrayList <=> array

// NEW STYLE with generics

// converting an array to an ArrayList, with Generics
String[] animals = { "bear", "cougar", "wolverine"};
ArrayList<String> al = new ArrayList<String>( Arrays.asList( animals )
);

// converting an ArrayList to an array with generics.
String[] predators = al.toArray( new String[ al.size() ] );

// OLD STYLE, without generics

// converting an array to an ArrayList, without Generics
String[] animals = { "bear", "cougar", "wolverine"};
ArrayList al = new ArrayList( Arrays.asList( animals ) );

// converting an ArrayList to an array without generics,
String[] predators = (String[])al.toArray( new String[ al.size() ] );
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Unread 03-10-2007, 05:59 AM   #8
Daniel Pitts
Guest
 
Posts: n/a
Re: Arrays.asList() doesn't work quite like I would think it should

Roedy Green wrote:
> On 03 Oct 2007 03:24:42 GMT, Ken <none@nowhere.com> wrote, quoted or
> indirectly quoted someone who said :
>
>> Collections.shuffle(Arrays.asList(indicies));

>
> asList makes a copy. So you have shuffled a new copy of the data in a
> List then thrown it away. Your code does not shuffle the original
> array.
>
>

Actually, it does *not* make a copy. It makes a List instance that
"wraps" the array.

The problem with the OP's code is the fact that you can't use
Arrays.asList on an array of primatives.

Many people have pointed this out, and have suggested that the OP use
Integer[] instead. It is a pity that Sun didn't add an Arrays.shuffle
for the primitive array types.

 
Unread 03-10-2007, 07:59 AM   #9
Piotr Kobzda
Guest
 
Posts: n/a
Re: Arrays.asList() doesn't work quite like I would think it should

Daniel Pitts wrote:

> It is a pity that Sun didn't add an Arrays.shuffle
> for the primitive array types.


Maybe lack of boxing of primitive type arrays is bigger pity?

Hopefully, we can easily do something similar, see example below.


piotr


import java.util.*;

public class ArrayBoxingTest {

public static void main(String[] args) {
final int[] a = new int[] { 1, 2, 3, 4, 5 };
Collections.shuffle(new AbstractList<Integer>() {

public int size() { return a.length; }

public Integer get(int index) { return a[index]; }

public Integer set(int index, Integer element) {
Integer r = a[index];
a[index] = element;
return r;
}

});
System.out.println(Arrays.toString(a));
}
}
 
Unread 03-10-2007, 07:59 AM   #10
Roedy Green
Guest
 
Posts: n/a
Re: Arrays.asList() doesn't work quite like I would think it should

On 03 Oct 2007 03:24:42 GMT, Ken <none@nowhere.com> wrote, quoted or
indirectly quoted someone who said :

> Collections.shuffle(Arrays.asList(indicies));


This code puzzled the heck out of me.

I have done experiments and I think I now know what is going on. It
is far stranger than you would imagine.

I have posted my findings at
http://mindprod.com/jgloss/array.html#GOTCHAS

I think it fair to say OP DID find a bug in Java, or at least the
mother of all gotchas.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
 

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Initializing Arrays Peter Oliphant VC.NET 2 20-09-2007 03:05 PM
Sorting arrays Jon Slaughter C#(C Sharp) 2 15-09-2007 08:04 AM
Question about arrays QDL VB.NET 3 15-09-2007 05:08 AM
Arrays by ref (yes, yet another ?) mr peanut C#(C Sharp) 2 09-09-2007 01:02 AM
Sorting Arrays cmdolcet69 VB.NET 7 06-09-2007 08:01 PM


< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT. The time now is 10:45 PM.


vBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2013, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional