Array of stacks

Is it possible to create an array of stacks without having to drop stacks when they exit? Eclipse gives me a warning about the impossibility of creating a shared Stack array when I do something like this:

Stack<Card>[] cards = new Stack<Card>[52]; 
+4
source share
6 answers

Joshua Bloch does a great job describing this issue in Effective Java, Second Edition. Check out the relevant section in Google Book Search .

The advice he offers is to prefer lists in arrays. Then your code might look something like this:

 List<Stack<Card>> cards = new ArrayList<Stack<Card>>(); 
+7
source

You can do the following, although this gives you a warning about an uncontrolled compiler.

 Stack<Card>[] cards = (Stack<Card>[]) new Stack[52]; 
+2
source

Why are you using arrays anyway?

This is a low-level programming structure.

Use List or Set instead (e.g. org.apache.commons.collections.list.LazyList ) if you don't want to worry about initialization.

Or at least

Arrays.asList(new Stack[52]) to wrap an array in a list.

I could not reproduce anywany .. jour error: :( perchaps it, because another warning / error level was set.

+1
source
 Stack<Card>[] decks = new Stack[9]; // Declare Card c = decks[5].pop(); // This compiles - java 'knows' the type Integer i = decks[4].pop(); // This will not compile 
+1
source

Well, an array does not have to be shared, because it is always defined like this. Why do you think you need to quit? I think the eclipse is somewhat confusing.

0
source

Do this with type erasure. In principle, generic types exist only at compile time and have no presence at runtime

Check out this forum post for a better explanation.

0
source

All Articles