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

L

Lew

Roedy Green
Have you read my essay? I don't think you found all of what I did.
All I saw was you are not supposed to pass an int[].
Yes I read your code example and it certainly didn't explain more than
what I did, although I agree that my explanation could be improved,

Blah, blah, blah.

Who cares who said what, as long as good information is what's said?
 
L

Lew

xen said:
Btw,

I would suggest to change
* You don't have the usual asList backing int[] array or a backing Integer[] array.

into something more understandable. What does »asList backing int[]
array« mean? asList is not backing anything, nor is it backed by
anything. And even if it was, I don't understand it.

Yes, it is.

asList() returns a List backed by the array, thus "the backing array".

The Javadocs for asList() clearly state:
Returns a fixed-size list backed by the specified array.

So the "backing" (gerundive) array is that "specified" array.

Simple.
 
X

xen

Roedy Green
Have you read my essay? I don't think you found all of what I did.
All I saw was you are not supposed to pass an int[].
Yes I read your code example and it certainly didn't explain more than
what I did, although I agree that my explanation could be improved,

Blah, blah, blah.

Who cares who said what, as long as good information is what's said?

Who said what is *not* the point is was making, as I clearly stated
but that's not the point.

I just answered Roedy's question for completeness. But anyway, it
seems that Roedy was referring to himself when he said
The error you make is
1. presuming everyone reads your every word.

so I can't blame him for anything other than not reading others'
posts. So be it.
 
X

xen

xen said:
I would suggest to change
* You don't have the usual asList backing int[] array or a backing Integer[] array.

into something more understandable. What does »asList backing int[]
array« mean? asList is not backing anything, nor is it backed by
anything. And even if it was, I don't understand it.

Yes, it is.

asList() returns a List backed by the array, thus "the backing array".

The Javadocs for asList() clearly state:
Returns a fixed-size list backed by the specified array.

So the "backing" (gerundive) array is that "specified" array.

Simple.

The List is backed by the array, not asList. asList is not backed by
anything, other than maybe ArrayList(T... t).
And backing is ambiguous. You could also read it as "the usual Josh
throwing Jim in the pond" and then it's still ambiguous.

But anyway, suit yourself.
 
R

Roedy Green

Yes I read your code example and it certainly didn't explain more than
what I did, although I agree that my explanation could be improved,
for example by stating that the int[] that is wrapped inside an array
is then used as the backing for the List, thus producing a List that
is backed by an array with one int[] element, but that's not the

Either:

1. where you said this did not appear on my machine

2. You said it in such a way I did not understand what you meant.

3. I unconsciously skipped over reading because of I find your prose
too dense.

You making a mountain out of a molehill. I discovered what I
discovered independently of what you said. That happens all the time
in science. If you are worried about credit, you get it because you
"published" first.

Even on rereading I still don't see where you explained the strange
behaviour of creating an ArrayList whose first element is an array
without error or warning.

To ensure people understand you, you pretty well have to post runnable
code. That we all understand.
 
R

Roedy Green

I would suggest to change
* You don't have the usual asList backing int[] array or a backing Integer[] array.

into something more understandable. What does »asList backing int[]
array« mean? asList is not backing anything, nor is it backed by
anything. And even if it was, I don't understand it.

I will see if I can improve the wording. Your prose is equally opaque
to me. I have always marveled at the ability of ordinary Germans to
parse those giant page-long sentences. I take it you have that skill
too since Dutch and German come from the same roots.

When I was in high school my English teachers chastised me for my
overly simple syntax. One liked it, saying it reminded him of Latin.
It just that any sort of nesting causes my brain to explode. I have a
terrible time with parenthesis nests in Java or nested ifs.

I have greater motivation that most to encapsulate, so that I don't
have to keep too many details in mind at once.
 
L

Lew

xen said:
The List is backed by the array, not asList. asList is not backed by
anything, other than maybe ArrayList(T... t).
And backing is ambiguous. You could also read it as "the usual Josh
throwing Jim in the pond" and then it's still ambiguous.

I have read and re-read your post, and I still don't know what you're trying
to say.

I agree that saying "asList" has a "backing array" is not precise,
nevertheless, it's what was meant. You asked what it meant and professed not
to understand; now I see that that was under false pretenses, and that in fact
you disagreed with the idea, and that you understood it just fine.

The poster who used the term "backing array" was using a standard term (the
usage of "backing" in this sense being well established) in a colloquial way,
using the method name as a metonymic reference to the list returned by the
method.

Nothing ambiguous at all, just imprecise. Saying "ambiguous" for something
that has only one interpretation is imprecise.
 
R

Roedy Green

* You don't have the usual asList backing int[] array or a backing Integer[] array.

into something more understandable

the comment at the head of the gotchas snippet now reads:

Demonstrate Arrays.asList( int[] ) gotcha.

See the goofy thing happening with Arrays.asList( int[] ).

int[] becomes the one and only element of a List of <int[]> objects!!
Arrays.asList is advertised to support the original array as a backing
array so that when you change the List (actually an ArrayList)
produced by asList, the original array is also supposed to change.
However, if you attempt to do Arrays.asList( int[] ), this won't work.
There is no invisible backing int[] or Integer[] created either.
All I can say is don't feed an int[] to asList. Use
Arrays.asList(Integer[]) boxed manually.
 
M

Mark Space

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

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

That *is* surprising. I assumed a deep copy. It looks like it's
actually just a wrapper, and the "list" is implemented as the original
array. Kinda cool.
http://download.java.net/jdk7/docs/api/java/util/Arrays.html#asList(T...)

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

Yes, but I learned something. "Squeaky wheel gets the grease." ;-)
 
X

xen

I agree that saying "asList" has a "backing array" is not precise,
nevertheless, it's what was meant. You asked what it meant and professed not
to understand; now I see that that was under false pretenses, and that in fact
you disagreed with the idea, and that you understood it just fine.

The poster who used the term "backing array" was using a standard term (the
usage of "backing" in this sense being well established) in a colloquial way,
using the method name as a metonymic reference to the list returned by the
method.

Nothing ambiguous at all, just imprecise. Saying "ambiguous" for something
that has only one interpretation is imprecise.

Look. Of course I knew in a way what he was trying to say, because I
already knew what was going on regarding this phenomenon, so I didn't
need him to explain it to me.

But, and maybe this is because I'm not a native English speaker, when
I read that sentence, the machinery of my grammar recognition faculty
just grinded to a halt. I couldn't process that part of the sentence.
I just couldn't make anything out of it. I had to 'manually' take
"asList backing int[] array" out of context, interpret in terms of the
gerundive as you call it, and then reinsert it in the sentence. If I
just read the sentence as it is, I automatically interpret "backing"
as a verb of which "asList" is the subject, and then it doesn't make
sense. So, to me, it is one hell of a difficult sentence to interpret
correctly. If you'd insert one small "an" it would become "the usual
asList backing an array" and the whole meaning changes.

So, whether it is correct English or not, it is clearly not suitable
for all readers, and it's not as if I'm so bad at English.
 
X

xen

* You don't have the usual asList backing int[] array or a backing Integer[] array.

into something more understandable

the comment at the head of the gotchas snippet now reads:

Demonstrate Arrays.asList( int[] ) gotcha.

See the goofy thing happening with Arrays.asList( int[] ).

int[] becomes the one and only element of a List of <int[]> objects!!
Arrays.asList is advertised to support the original array as a backing
array so that when you change the List (actually an ArrayList)
produced by asList, the original array is also supposed to change.
However, if you attempt to do Arrays.asList( int[] ), this won't work.
There is no invisible backing int[] or Integer[] created either.
All I can say is don't feed an int[] to asList. Use
Arrays.asList(Integer[]) boxed manually.

Well Roedy, maybe it's just a matter of personal opinion or
preference, but.... I think what you wrote is just incorrect.
There is no invisible backing int[] or Integer[] created either.

Actually, there IS a backing array. It's just that it is an int[][]
with one element: the int[] the caller supplied. This means that all
the List operations will index this array by way of the first of the
indeces, like elements[2], whereas to get an element of the original
int[] you'd use elements[0][x] where x is an index of the original
int[]. But I don't think I need to explain this to you (but just to
make sure ;)).

So while my prose may be hard to digest (and I'm wanting to look into
that, thank you for telling me) I find your explanation to be just not
very insightful or insight-giving.

Thanks for discussing this with me anyway, I appreciate it.
 
X

xen

Either:

1. where you said this did not appear on my machine

2. You said it in such a way I did not understand what you meant.

3. I unconsciously skipped over reading because of I find your prose
too dense.

Aye aye, that can happen.
You making a mountain out of a molehill. I discovered what I
discovered independently of what you said. That happens all the time
in science. If you are worried about credit, you get it because you
"published" first.

Well, I questioned you because I felt ignored, and after doing that I
couldn't back out now could I? :) I'm not worried about credit, I just
hate being ignored. Maybe it's like when you're with friends and you
say hey let's go to the movies and they all look at you with blank
eyes for a second and then continue discussing what they were
discussing and then five minutes later one of them says, hey let's go
to the movies and they all say yeah great idea. Something like that.
Even on rereading I still don't see where you explained the strange
behaviour of creating an ArrayList whose first element is an array
without error or warning.

Okay, I'll see if I can improve things, given that you said
Your prose is equally opaque
to me. I have always marveled at the ability of ordinary Germans to
parse those giant page-long sentences. I take it you have that skill
too since Dutch and German come from the same roots.

I must say I have read Kant in German and I found it equally hard to
digest his sentences. Especially because I didn't understand many of
the words so I had to look them up. But when I had looked them up, I
had lost track of the rest of the sentence, so I had to start all over
again! I don't really know if Dutch people also do that, I've never
had trouble with anyone's sentence, but I guess it is very well
possible (am I doing it right now? :p).

Okay, back to my explanation.

I don't see any long sentences in it, actually. I will refrase it, and
then I want you to give your opinion, if you will.
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[].

What you are trying to do with the call to asList() is to create a
List that internally uses the array that you supply, as its data
store. That way, you can modify the array by using List operations,
or, put differently, modify the List and have the changes 'write
through' to your original array.

This always works for arrays of any Object type, such as arrays of
Integer. The actual method signature for asList is »asList(T... a)«.
This means you can pass a variable amount of arguments to this
function. You could call it with asList("boy","girl","sister") or
asList("just one argument"). Internally however, all of these
parameters are wrapped inside an array, and the actual call becomes:
asList( new String[] { "boy", "girl", "sister" } ). You can also just
pass such a array directly, which is most likely how you'd use
asList(). There is only one caveat: the type of the parameter, and as
such, the type of the elements of the array, must conform to the
generic type variable »T«. But only object types can match a generic
type. You cannot match a »T« with any primitive type, such as »int«.

So what happens when you call asList() on an int array? First the
compiler tries to match T with int, but that doesn't work. However,
there is another option open to the compiler: it can match T with
int[]. That's right, array of int becomes the type T. As such, this
int[] is the single parameter to asList(), and it gets wrapped inside
an array, like all single parameters that are not already an array of
an object-type. This single element array with type signature int[][]
is then used as the backing for a new ArrayList. That means that the
List that you get, has a single element: the int[] you originally
supplied!

It should be clear that calling shuffle() on this List will only
effect the encapsulating array and will do nothing to the int[] that
is kept inside.
 
S

Stefan Ram

xen said:
hate being ignored

But then, in Usenet, there is a history and tradition of using
a »filter« or »scoring system«.

No one is obliged to read all articles posted to a group.

Moreover, Usenet is not real-time communication. Someone
might answer to an article and not have yet read another
answer to the same article that might not have arrived at his
newsserver yet.

There is also a tradition to read and answer Usenet articles
off-line, after a client has fetched some articles from a
Usenet server. In this case, some more recent articles also
might not be read yet when answering to an article.
I must say I have read Kant in German and I found it equally hard to

Hausser wrote a computer program that can parse German
sentences, which most native German speakers will have
a hard time to understand:

»Der Mann hat dem Mädchen das Buch geben
dürfen können wollen sollen mögen.«

This should mean: »The man wanted to be required to want
to be able to be allowed to give the book to the girl.«

[Hausser86]
Lecture Notes in Computer Science Vol.231
NEWCAT, Parsing Natural Language Using Left-Associative Grammar
Roland Hausser
ISBN: 3-540-16781-1; Springer; 1986; Berlin, New York
 
X

xen

No one is obliged to read all articles posted to a group.

No, of course. But for a thread you participate in it is slightly
different. My friends are not obliged to listen to me when I speak,
but it would be nice of them nonetheless.
Moreover, Usenet is not real-time communication. Someone
might answer to an article and not have yet read another
answer to the same article that might not have arrived at his
newsserver yet.

There is also a tradition to read and answer Usenet articles
off-line, after a client has fetched some articles from a
Usenet server. In this case, some more recent articles also
might not be read yet when answering to an article.

Aye, that's all possible but not very likely nowadays. When I post a
message it's almost instantly available on Google. But, granted, I can
never be sure someone has (had the opportunity to) read me.
 
L

Lasse Reichstein Nielsen

xen said:
Aye, that's all possible but not very likely nowadays. When I post a
message it's almost instantly available on Google. But, granted, I can
never be sure someone has (had the opportunity to) read me.

What's Google got to do with it? I read news in a newsreader, retrieving
the messages from my ISP's nntp-server. My posts are also sent to that
server. That means that other people reading news using the same server
will see my messages sooner than other people. I will also see messages
from servers that propagate directly to my ISP's server sooner than
messages posted to servers requireing more hops to reach me.

You can, usually, assume that someone seeing a response has also had
the chance to seeing the message being responded to, but I bet there
can be cases where even that isn't true. Other responses to the same
message cannot be assumed to have arrived when a response is written.

/L 'but at least Usenet distribution is no longer magnetic tape on a plane'
 
X

xen

What's Google got to do with it?

I just mentioned Google because it is a source of news other than my
own ISP's newsserver, so if it has propagated to Google Groups, there
is I think a reasonable chance that it is has progapaged or will
propagate in a reasonable amount of time to Your Favourite Newsserver
as well.
 

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
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top