Note that there are some explanatory texts on larger screens.

plurals
  1. POInitializing objects at run-time
    primarykey
    data
    text
    <p><strong>EDIT:</strong></p> <p>It seems as though I have not been clear enough in what I'm asking for, I'm going to try to explain better.</p> <p>I have a class called <code>Board</code></p> <p>This class will have a 2-dimensional array filled with integers, each integer value in the array corresponds to some class (so 1 could be short-hand for String for example).</p> <p>This class will also have some means to take this 2-dimensional array, loop over it and create new instances of some classes.</p> <p>I need to be able to customize what the 2-dimensional array and what classes the different integers corresponds to.</p> <p>I did a little image to try to visualize what I want: <a href="http://i.imgur.com/bZXrw.png" rel="nofollow">http://i.imgur.com/bZXrw.png</a></p> <p>Along with some pseudo-code:</p> <pre><code>class Board: Integer[][] some_array = {{1,2,1},{1,2,1}} someStructure = {1 : createThing, 2 : createSomeOtherThing}; generateObjects(): List[Thing] container = [] for y in some_array: for x in y: container.add(someStructure[x]()) return container </code></pre> <p><strong>OLD FORMULATION:</strong></p> <p>I have a class called <code>Map</code> which is supposed to contain an <code>Integer[][]</code> object, the values inside the Integer array object corresponds to some object which will need to be initialized.</p> <p>Now, in a language like Python I could for example have a dictionary which has some integer values as keys and some functions which creates and returns new objects when called and then loop over a 2d-list and call the functions returned by the dict. How would I go about to do this with Java? </p> <p>I though about having a HashMap and doing some kind of reflection but I don't really know what to do tbh.</p>
    singulars
    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.
    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