Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating Arrays of Generic Types in Java
    primarykey
    data
    text
    <p>So I know that you cannot "easily" create an array of a generic type in Java (but you can create collections). I recently ran across a situation where I needed a 2 dimensional array of objects (that were Generic). Here's a "rough" idea of what it looked like (not complete but I'm trying to be as brief as possible):</p> <pre><code>class Outer&lt;T&gt; { private Foo[][] foo; abstract class Foo extends Blah&lt;T&gt; { public List&lt;T&gt; getContents (); } abstract class Bar extends Foo { ... } } </code></pre> <p>So somewhere in the code I needed an array as such:</p> <pre><code>foo = new Foo[width][height]; </code></pre> <p>(which we know can't happen). However, I tried this:</p> <pre><code>foo = (Foo[][])Array.newInstance (Foo.class, new int[]{getWidth (), getHeight ()}); </code></pre> <p>which the compiler accepted though I had to suppress warnings. I guess my question is "Is this going to nip me in the bud somewhere down the line? The member "foo" is never exposed to the outside (though the types Foo and Bar are). I know it's ugly but it definitely works and saved me from having to create some other "psedu-kludge" the would probably have caused classes overriding the "Outer" class more headaches. Thanks in advance!</p> <hr/> <h3>This might make things easier to visualize</h3> <p>This is closer to what I'm actually doing; realizing of course that there are many support methods and other logic inside of the Map class that I've left out for brevity.</p> <pre><code> import java.lang.reflect.Array; import java.util.ArrayList; import java.util.List; interface Cell&lt;T&gt; { public void add (T t); public boolean remove (T t); public List&lt;T&gt; getAll (); public Map&lt;T&gt; getMap (); } class Map&lt;T&gt; { protected BaseCell map[][]; public abstract class BaseCell implements Cell&lt;T&gt; { private List&lt;T&gt; contents; public BaseCell () { this.contents = new ArrayList&lt;T&gt; (); } public void add (T t) { this.contents.add (t); } public boolean remove (T t) { return this.contents.remove (t); } public List&lt;T&gt; getAll () { return this.contents; } public Map&lt;T&gt; getMap () { return Map.this; } abstract public boolean test (); } public class SpecialCell extends BaseCell { @Override public boolean test() { return true; } } public class SpecialCell2 extends BaseCell { @Override public boolean test() { return false; } } @SuppressWarnings("unchecked") public Map (int width, int height) { this.map = (BaseCell[][])Array.newInstance(BaseCell.class, new int[] {width, height}); for (int y = 0; y &lt; height; y++) { for (int x = 0; x &lt; width; x++) { if (Math.random() &lt; .5) { this.map[x][y] = new SpecialCell (); } else { this.map[x][y] = new SpecialCell2 (); } } } } public BaseCell getCellAt (int x, int y) { return this.map[x][y]; } } public class Junk { /** * @param args */ public static void main(String[] args) { class Occupant { } Map&lt;Occupant&gt; map = new Map&lt;Occupant&gt; (50, 50); map.getCellAt(10, 10).add(new Occupant ()); map.getCellAt(10, 10).getMap (); for (int y = 0; y &lt; 50; y++) { for (int x = 0; x &lt; 50; x++) { System.out.print (map.getCellAt (x, y).test () ? "1" : "0"); } System.out.println (); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload