难以在for循环中将项目分配给2d数组对象

时间:2013-11-06 05:41:18

标签: java arrays

我正试图通过像这样的循环将一个项目分配给mapTiles [x] [i]:mapTiles [x] [i] = mapCols [i];但它不起作用。这是我得到的错误:

incompatible types - found java.lang.String but expected MapTile

我的代码:

public class Map {

MapTile[][] mapTiles;
String imageMap;
String rawMap;

// constructor 
public Map() {
    imageMap = "Map_DragonShrine.jpg";
    rawMap = "Dragon_Shrine.map";
    mapTiles = new MapTile[34][22];
}


// methods
public void loadMapFile() {

    rawMap = file2String(rawMap);

    // array used to hold columns in a row after spliting by space
    String[] mapCols = null;
    // split map using 'bitmap' as delimiter
    String[] mapLines = rawMap.split("bitmap");  
    // assign whatever is after 'bitmap'
    rawMap = mapLines[1];
    // split string to remove comment on the bottom of the file
    mapLines = rawMap.split("#");
    // assign final map
    rawMap = mapLines[0].trim();
    mapLines = rawMap.split("\\n+");

    for(int x = 0; x < mapLines.length; x++) {
        rawMap = mapLines[x] ;
        mapCols = rawMap.split("\\s+");
        for(int i = 0; i < mapCols.length; i++) {
            mapTiles[x][i] = mapCols[i];   
        }            
    }   
}     
}

这是mapTiles是其对象的MapTile类。鉴于我所拥有的,我不知道如何传递新的MapTile(mapTiles [i])。我试图围绕使用带有类和实例的2d数组。

MapTile类:

public class MapTile {
/**Solid rock that is impassable**/
boolean SOLID, 

/** Difficult terrain. Slow to travel over*/
DIFFICULT, 

/** Statue. */
STATUE,

/** Sacred Circle.  Meelee and ranged attacks are +2 and
    damage is considered magic.  */
SACRED_CIRCLE, 

/** Summoning Circle. */
SUMMONING_CIRCLE,

/** Spike Stones. */
SPIKE_STONES,

/** Blood Rock.Melee attacks score critical hits on a natural 19 or 20. */
BLOOD_ROCK,

/** Zone of Death.Must make a morale check if hit by a melee attack. */
ZONE_OF_DEATH,

/** Haunted.-2 to all saves. */
HAUNTED,

/** Risky terrain. */
RISKY,

/** A pit or chasm. */
PIT,

/** Steep slope. */
STEEP_SLOPE,

/** Lava. */
LAVA,

/** Smoke. Blocks LOS. */
SMOKE,

/** Forest. Blocks LOS. */
FOREST,

/** Teleporter. */
TELEPORTER,

/** Waterfall. */
WATERFALL,

/** Start area A. */
START_A,

/** Start Area B. */
START_B,

/** Exit A. */
EXIT_A,

/** Exit B. */
EXIT_B,

/** Victory Area A. */
VICTORY_A,

/** Victory Area B. */
VICTORY_B,

/** Temporary wall terrain created using an Elemental Wall or other means. */
ELEMENTAL_WALL;       

public MapTile() {
}

}

1 个答案:

答案 0 :(得分:2)

这是因为mapTiles是类型MapTile的二维数组,而mapCols是类型为String的一维数组。您不能将String值分配给MapTile类型的变量。它们是incompatible types,如错误消息所示。

你可能想要做这样的事情

mapTiles[x][i] = new MapTile(mapCols[i]);
// where the MapTile constructor takes a String parameter.