user2005938
user2005938

Reputation: 179

Creating Arrays with automated names?

I have a method in which I load tiles from a text file. The tiles are placed in an array when created, so that they can be cleared later. This has began to cause problems and I am wondering if there would be a way to create an array with a name that corresponds to the text file loaded. For example, I call

loadMap("map1");

With "map1" being the name of the txt file that the map is stored in. And if I were to call the loadMap method with the argument of "map1" how can I create an array titled something like "map1TileArray", or if the argument is "finalMap" I would want an array called "finalMapTileArray". Is it possible to do something like this, and if so, how?

EDIT:

I'm getting a NPE.

I declare my Map like this:

Map<String, ArrayList<Tile>> tileMap = new HashMap<String, ArrayList<Tile>>();

I then store an ArrayList in the tileMap with a string of the current map:

tileMap.put(map, tilearray);

But I get an error at this line:

if(tileMap.get(currentMap).size()>0) {

Which is the start of my unloadTiles method. currentMap is just the String for the map the program is on.

Upvotes: 0

Views: 107

Answers (4)

WyssMan
WyssMan

Reputation: 66

As others have said, a map will work for this.

What others have not said is that you would probably also benefit from using a class to represent your tiles as well.

This way, any array logic you have for manipulating the tiles can be nicely encapsulated in one place. I would imagine something like this:

public class Tiles{
    private int[] tiles;
    private String name;
    private Tile(int[] tiles, String name){
        this.tiles = tiles;
    }

    public static Tiles getTiles(Map<String, Tiles> tilesCache, String tileName){
        if (tilesCache.containsKey(tileName)){
            return tilesCache.get(tileName);
        }
        // load from file
        return tile;
    }

    public void clear(Map<String, Tiles> tilesCache){
        tilesCache.remove(this.name);
        this.tiles = null;
    }

    //Other logic about tiles
}

Upvotes: 1

OscarRyz
OscarRyz

Reputation: 199234

Use a java.util.Map and assign the value to a variable. Probably you will be better if use a List instead of an array

List<Integer> currentArray = loadMap("map1");

.... 
// inside
private List<Integer> loadMap( String fileName ) { 
    List<Integer> result = allTheMaps.get( fileName );
    if ( result == null ) { 
       // load it from file... 
       result = .... 
       allTheMaps.put( fileName, result ); 
    }
    return result;
}

Upvotes: 1

Jay Estrella
Jay Estrella

Reputation: 24

You might want to consider using a HashMap with a String as the key and an Integer[] for the value.

    Map<String, Integer[]> maps = new HashMap<String, Integer[]>();

and when you call your loadMap function you could do something like this.

    public Integer[] loadMap(String name) {
        if (maps.contains(name)) {
            return maps.get(name);
        }
        // Falls through if map is not loaded
        int[] mapData = new int[##];

        // load map

        maps.put(name, mapData);
        return mapData;
    }

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You will want to use a Map such as a HashMap, perhaps a Map<String, Integer[]>. This will allow you to create an array of Integer (or whatever) and associate it with a String.

For example:

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Foo {
   public static void main(String[] args) {
      Map<String, Integer[]> myMap = new HashMap<>();
      myMap.put("foo", new Integer[] { 1, 2, 3 });
      myMap.put("bar", new Integer[] { 3, 4, 5 });
      myMap.put("spam", new Integer[] { 100, 200, 300 });

      for (String key : myMap.keySet()) {
         System.out.printf("%8s: %s%n", key, Arrays.toString(myMap.get(key)));
      }
   }
}

Upvotes: 6

Related Questions