Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java generic issue in array

Reply
Thread Tools

Java generic issue in array

 
 
jacksu
Guest
Posts: n/a
 
      05-25-2006
I could create:

ArrayList<Node> myarr = new ArrayList<Node>();

but can't

ArrayList<Node> myarr[] = new ArrayList<Node>[n];
I got error "Cannot create a generic array of ArrayList<Node>

any suggsetion?

Thanks.

 
Reply With Quote
 
 
 
 
nkalagarla@gmail.com
Guest
Posts: n/a
 
      05-25-2006
generic array creation is not allowed.Try following work-around.

ArrayList<Node>[] myarr = (ArrayList<Node>[])
Array.newInstance(ArrayList.class,n);

jacksu wrote:
> I could create:
>
> ArrayList<Node> myarr = new ArrayList<Node>();
>
> but can't
>
> ArrayList<Node> myarr[] = new ArrayList<Node>[n];
> I got error "Cannot create a generic array of ArrayList<Node>
>
> any suggsetion?
>
> Thanks.


 
Reply With Quote
 
 
 
 
Luc The Perverse
Guest
Posts: n/a
 
      05-25-2006
<> wrote in message
news: oups.com...
> generic array creation is not allowed.Try following work-around.
>
> ArrayList<Node>[] myarr = (ArrayList<Node>[])
> Array.newInstance(ArrayList.class,n);


Eh?

What in the world is that?

I think the more common way is (at least what I have seen):

Where n is some integer:

ArrayList<Node>[] myarr = new ArrayList[n];
for(int i=0;i<n;i++)
myarr[i] = new ArrayList<Node>(); //initialize each individually

--
LTP




 
Reply With Quote
 
nkalagarla@gmail.com
Guest
Posts: n/a
 
      05-25-2006
Well, with my approach you won't get any warning. With yours, you will
see one warning and to suppress it you have to specify
@SuppressWarnings("unchecked") annotation.



 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      05-25-2006
<> wrote:
> Well, with my approach you won't get any warning. With yours, you will
> see one warning and to suppress it you have to specify
> @SuppressWarnings("unchecked") annotation.
>
>


Didn't you mean that the other way around? Casting from Object (the
return type of Array.newInstance) to ArrayList<Node>[] ought to result
in an unchecked cast warning. Nothing in Luc's response seemed to
require this warning.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      05-25-2006

"Chris Smith" <> wrote in message
news:. ..
> <> wrote:
>> Well, with my approach you won't get any warning. With yours, you will
>> see one warning and to suppress it you have to specify
>> @SuppressWarnings("unchecked") annotation.
>>
>>

>
> Didn't you mean that the other way around? Casting from Object (the
> return type of Array.newInstance) to ArrayList<Node>[] ought to result
> in an unchecked cast warning. Nothing in Luc's response seemed to
> require this warning.



Luc's

ArrayList<Node>[] myarr = new ArrayList[n];

causes a warning:

Warnings.java:8: warning: [unchecked] unchecked conversion
found : java.util.ArrayList[]
required: java.util.ArrayList<java.lang.String>[]
ArrayList<String>[] myarr = new ArrayList[12];




 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      05-25-2006
Mike Schilling <> wrote:
> ArrayList<Node>[] myarr = new ArrayList[n];
>


Yep, you're right. I picked up that confusion from an early version of
the Eclipse generics compiler. It appears that nkalagarla also did the
same, since the warning from his/her post was apparently missing in
Eclipse up to 3.2M6, and fixed there. With the current 3.2 milestone,
both options correctly give warnings.

That'll teach me to read the spec before replying with something I don't
understand.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      05-26-2006
"jacksu" <> wrote in message
news: ups.com...
>I could create:
>
> ArrayList<Node> myarr = new ArrayList<Node>();
>
> but can't
>
> ArrayList<Node> myarr[] = new ArrayList<Node>[n];
> I got error "Cannot create a generic array of ArrayList<Node>
>
> any suggsetion?
>
> Thanks.
>


You cannot do that.
Ponder the thought: you cannot write a 1.5 java.util.ArrayList
implementation without generating a compile-time warning. Some say generics
are broken, others (fewer) say arrays are broken - it's all guff on top of a
flawed premise though.

--
Tony Morris
http://tmorris.net/


 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      05-26-2006
jacksu wrote:
> I could create:
>
> ArrayList<Node> myarr = new ArrayList<Node>();
>
> but can't
>
> ArrayList<Node> myarr[] = new ArrayList<Node>[n];
> I got error "Cannot create a generic array of ArrayList<Node>


Don't use arrays of references...

Instead use:

List<List<Node>> nodeLists = new ArrayList<List<node>>();

IIRC, the Joshua Bloch's Effective Java Reloaded session at JavaONE
covered that sort of thing. </appeal-to-authority>

Alternatively add a class for the concept of the particular use of a
List of Nodes:

class NodeString {
private final List<Node> nodes;
}
....
List<NodeString> nodeStrings = new ArrayList<NodeString>();

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      05-26-2006
"Tony Morris" <> wrote in message
news:WHsdg.10755$...

> Ponder the thought: you cannot write a 1.5 java.util.ArrayList
> implementation without generating a compile-time warning. Some say
> generics are broken, others (fewer) say arrays are broken - it's all guff
> on top of a flawed premise though.


Nitpick: you can do it in later versions of 1.5 by using an annotation that
specifically turns that warning off. Only a slight improvement, I agree.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
not just generic type programming,but also parallism generic syntaxprogramming?? minlearn C++ 2 03-13-2009 05:17 PM
generic interfaces with generic methods Murat Tasan Java 1 02-03-2009 12:17 PM
Odd ObjectDataSource Event Issue with Generic List of objects fig000 ASP .Net 0 11-30-2007 07:44 PM
Generic class in a non generic class nramnath@gmail.com Java 2 07-04-2006 07:24 AM
Why Didn't The Creators Of Java Allow For Generic Array Intialization? res7cxbi@verizon.net Java 1 01-02-2006 06:20 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57