Re: generics and arrays and multi-class collections



On Sep 29, 5:38 pm, 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.

In my opinion, using Arrays is akin to the "primative obsession" anti-
pattern. You'd probably be better off using a List<Set<T>> or simply
List<T>, depending on your needs.

The little bit of overhead you incur is *far* outweighed by the
functionality gained.


.



Relevant Pages

  • Re: generics and arrays and multi-class collections
    ... java.util.ArrayList uses an array of Object to store its ... The cast generates an unchecked warning. ... Wisdome is "use Java generics only at its shallowest". ...
    (comp.lang.java.programmer)
  • 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: Performance tuning
    ... might want to try using unsafe code, as you can do direct memory management ... also try and turn off bounds checking for arrays in unsafe code if you are ... That is: unless the generics actually ... implemented heuristics on algorithms to speed things up; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Performance tuning
    ... might want to try using unsafe code, as you can do direct memory management ... also try and turn off bounds checking for arrays in unsafe code if you are ... About generics / inheritance. ... implemented heuristics on algorithms to speed things up; ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: adding data to a file before it gets regexed
    ... The reasion I am trying to add the string to the line, is so that I can use ... it is not a good idea to store entire files in memory and it is really ... the arrays are the contents of the files broken down by lines. ... Any opinion expressed in this e-mail is personal to the sender ...
    (perl.beginners)