java - 在 for 循环中将项目分配给二维数组对象很困难

标签 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 类。鉴于我所拥有的,我不明白如何传递 new MapTile(mapTiles[i]) 。我正在尝试将我的注意力集中在二维数组及其类和实例的使用上。

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() {
}

}

最佳答案

这是因为 mapTilesMapTile 类型的二维数组,而 mapCols 类型的一维数组字符串。您不能将字符串值分配给 MapTile 类型的变量。正如错误消息所述,它们是不兼容的类型

你可能想做这样的事情

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

关于java - 在 for 循环中将项目分配给二维数组对象很困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19804935/

相关文章:

java - 如何使用 JBOSS 配置摆脱谷歌浏览器中过时的密码套件问题

arrays - 如何将字符串数组转换为 D 中的字符串?

python - 在 Python 中将数组(numpy 数据类型)转换为元组( native 数据类型)

c - 在 c : when do I have to use malloc 中实现字符串队列

arrays - 在 Bash 中将命令行参数转换为数组

java - 使用 Solr 索引文件会阻塞文件

java - 纹理化顶点缓冲区对象

java - 排序 ObservableList 理解比较器和谓词

java - 在java和sql server中的存储过程中传递列名?

java - 如何根据 String 类型方法(java)中的 if 语句返回多个值?