Re: generics and arrays and multi-class collections



On Sep 30, 9:38 am, xen <x...@xxxxxxxxxx> wrote:
Yo peoples of the earth,

What is the proper use of generics when you want to use arrays as
well?
Is there anybody out there that writes code that does NOT generate
unchecked warnings?

For example, java.util.ArrayList uses an array of Object to store its
elements, and uses a cast (E)elements[i] to retrieve them, which
generates an unchecked warning. I myself have wanted to use an array
of generified sets, so I just create a Set[] and assign it to a
Set<E>[], which generates a warning. I can trim down on the warnings
so that I only get "unchecked conversion" and "unchecked cast"
warnings, but it seems I'll have to live with those.

My last addition was a Map that can store Lists that store different
classes that are all decendants of a superclass:

Map<Class<? extends Feature>, List<? extends Feature>> stores;
stores = new HashMap<Class<? extends Feature>, List<? extends
Feature>>();

I want a method that retrieves each store, and creates one if not
present.

public <F extends Feature> List<F> getStore(Class<F> c) {
List<F> L = (List<F>)stores.get(c); // unchecked cast
if (L == null) {
L = new ArrayList<F>();
stores.put(c, L);
}
return L;
}

The cast generates an unchecked warning.

Now given
class Block extends Feature {}
I can do
List<Block> = getStore(Block.class);
which is what I wanted.

What are your experiences, any advice?

greetings, xen.

I'll have to live with those.
Yes. And the SuppressWarnings annotation may be our only feeble
solace. Wisdome is "use Java generics only at its shallowest".
Anything deeper can become confusing and unworkable.

.



Relevant Pages

  • generics and arrays and multi-class collections
    ... What is the proper use of generics when you want to use arrays as ... java.util.ArrayList uses an array of Object to store its ... The cast generates an unchecked warning. ...
    (comp.lang.java.programmer)
  • Re: generics and arrays and multi-class collections
    ... What is the proper use of generics when you want to use arrays as ... java.util.ArrayList uses an array of Object to store its ... The cast generates an unchecked warning. ... using Arrays is akin to the "primative obsession" anti- ...
    (comp.lang.java.programmer)
  • Re: Finding the nearest match without reusing results
    ... comparing an array with a value generates an array of Trues and ... Any diff with wrong State or with used Store is implicitly zeroed out. ... Each must be larger than the absolute value of the maximum Sales ... go.....it just needs to return the closest match. ...
    (microsoft.public.excel.programming)
  • Re: read keyboard input and storing in an array?
    ... > I'm trying to store user input in an array, ... You have the beginnings of that logic already since your prompt tells the ... 201st value since the array only has room for 200 values. ... This will force you to store the int values as Integer ('Integer' ...
    (comp.lang.java.help)
  • Re: Challenge: reading ascii data
    ... to store all the data before producing any output. ... would be bad practice in terms of memory consumption to use a standard ... So I use hashes to create a two-level "sparse array", ... Well the original problem definition was: ...
    (comp.lang.fortran)

Loading