|
|
 |
| Author |
Message |
blue.striped.cat

|
Posted: 7/17/2005 6:31:00 AM |
Top |
java-programmer, Array of generic List?
Hello,
I am new to 1.5 and thus generics. I have read the generic tutorial and
a decent number of websites/newsgroup posts on the subject. But I still
cannot find a satisfactory solution to the following (as of now the
best I've got is an unchecked conversion warning)
The code below does not compile, but I think it shows to a human what I
want to accomplish. I can't seem to find the proper syntax and use of
generics to accomplish this without a warning. Any help and reasoning
why I need to do whatever the solution is would be great. Thanks.
public class MyClass {
private List<Foo>[] myFoos;
public MyClass() {
myFoos = new List<Foo>[5];
}
public void addFoo(int index, Foo foo) {
myFoos[index].add(foo);
}
}
|
| |
|
| |
 |
Roedy Green

|
Posted: 7/17/2005 7:51:00 AM |
Top |
java-programmer >> Array of generic List?
On 16 Jul 2005 15:31:12 -0700, email***@***.com wrote or
quoted :
>I am new to 1.5 and thus generics. I have read the generic tutorial and
>a decent number of websites/newsgroup posts on the subject. But I still
>cannot find a satisfactory solution to the following (as of now the
>best I've got is an unchecked conversion warning)
<Object> is type checked, but does nothing.
--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm
Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
|
| |
|
| |
 |
googmeister

|
Posted: 7/17/2005 10:47:00 AM |
Top |
java-programmer >> Array of generic List?
> public class MyClass {
> private List<Foo>[] myFoos;
> public MyClass() {
> myFoos = new List<Foo>[5];
> }
Unfortunately, Java disallows generic array creation.
The best work-around is to do something like the
following (which I suspect is similar to your solution):
public class MyClass {
private List[] myFoos;
public MyClass() {
myFoos = new List[5];
}
Then, when it comes time to remove a list element,
you'll need to cast it back to type Foo.
Sorry, Java screwed this one up pretty badly
(in my opinion).
|
| |
|
| |
 |
Roedy Green

|
Posted: 7/17/2005 4:01:00 PM |
Top |
java-programmer >> Array of generic List?
On 16 Jul 2005 15:31:12 -0700, email***@***.com wrote or
quoted :
> myFoos = new List<Foo>[5];
Check the source code for ArrayList to see how the allocate the
new<T>[n]. It can't be done, but you can pare it down to just a
warning message.
Here is sample of the cheating technique:
package com.mindprod.jdisplay;
/**
* A simple pushdows stack.
*
* @author Roedy Green
*
*/
public class Stack<T> {
/**
* Constructor for a pushdown stack.
*
* @param size
* maxiumum stack depth. Cannot grow beyond this depth.
Allocates RAM
* for this deep.
*/
public Stack( int size )
{
// in unchecked version it said stack = new Object[ size ];
// can't say stack = new T[size];
// This is how ArrayList cheats.
stack = (T[]) new Object[size];
this.size = size;
}
/**
* How many elements are on the stack right now.
*/
private int depth = 0;
/**
* How many elements the stack can hold
*/
private int size;
/**
* array to hold the stack elements. The top of the stack is
stored in the
* highest index.
*/
private T[] stack;
/**
* add an element to the stack
*
* @param item
* Item to add.
*/
public void push( T item )
{
if (depth >= size)
{
throw new IllegalArgumentException("stack overflow");
}
stack[depth++] = item;
}
/**
* remove and element from the stack
*
* @return the element formerly on the top of stack.
*/
public T pop()
{
if (depth <= 0)
{
throw new IllegalArgumentException("stack underflow");
}
return stack[--depth];
}
/**
* drop the top element from the top of stack
*
*/
public void drop()
{
if (depth <= 0)
{
throw new IllegalArgumentException("stack underflow");
}
depth--;
}
/**
* empty the stack.
*
*/
public void clear()
{
depth = 0;
}
}
--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm
Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
|
| |
|
| |
 |
Thomas Hawtin

|
Posted: 7/17/2005 5:48:00 PM |
Top |
java-programmer >> Array of generic List?
email***@***.com wrote:
>
> I am new to 1.5 and thus generics. I have read the generic tutorial and
> a decent number of websites/newsgroup posts on the subject. But I still
> cannot find a satisfactory solution to the following (as of now the
> best I've got is an unchecked conversion warning)
> public class MyClass {
> private List<Foo>[] myFoos;
>
> public MyClass() {
> myFoos = new List<Foo>[5];
> }
>
> public void addFoo(int index, Foo foo) {
> myFoos[index].add(foo);
> }
> }
What's wrong with:
public class MyClass {
private final List<List<Foo>> myFoos;
public MyClass() {
final int num = 5;
this.myFoos = new java.util.ArrayList<List<Foo>>(num);
for (int ct=0; ct<num; ++ct) {
myFoos.add(null);
}
}
public void addFoo(int index, Foo foo) {
myFoos.get(index).add(foo);
}
}
Then just sort out you NPEs.
With generics there's less to be gained from arrays of references.
Tom Hawtin
--
Unemployed English Java programmer
|
| |
|
| |
 |
Thomas Hawtin

|
Posted: 7/18/2005 10:05:00 PM |
Top |
java-programmer >> Array of generic List?
email***@***.com wrote:
>
> public MyClass() {
> myFoos = new List<Foo>[5];
> }
It occurs to me that you can actually do something like this. It's only
the creation of arrays with generic parameters that is illegal. So
create the erased counterpart and then case.
@SuppressWarnings("unchecked")
public MyClass() {
myFoos = (List<Foo>[])new List[5];
}
But I think you're probably still best of with a List of Lists, or similar.
Tom Hawtin
--
Unemployed English Java programmer
|
| |
|
| |
 |
| |
|