Array of generic List?

B

blue.striped.cat

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);
}
}
 
R

Roedy Green

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
 
G

googmeister

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).
 
R

Roedy Green

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
 
T

Thomas Hawtin

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
 
T

Thomas Hawtin

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
 

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,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top