我正在尝试将我用C#编写的方法转换为适用于Android的Java。
以下编码是我试图从C#转换为Java:
public Map()
{
string line;
int maxRowLevels, maxColLevels;
StreamReader file;
file = File.OpenText("map.txt");
line = file.ReadLine();
line = file.ReadLine();
maxRowLevels = System.Convert.ToInt32(line) - 1;
line = file.ReadLine();
line = file.ReadLine();
maxColLevels = System.Convert.ToInt32(line) - 1;
line = file.ReadLine();
map = new char[maxRowLevels+1,maxColLevels+1,screenRows,screenCols];
for (int rowCurrentLevel = 0; rowCurrentLevel<=maxRowLevels; rowCurrentLevel++)
{
for (int currentRow = 0; currentRow<screenRows; currentRow++)
{
line = file.ReadLine();
for (int colCurrentLevel = 0; colCurrentLevel<=maxColLevels; colCurrentLevel++)
{
for (int currentCol = 0; currentCol<screenCols; currentCol++)
{
map[rowCurrentLevel, colCurrentLevel,currentRow, currentCol] = line[colCurrentLevel*screenCols + currentCol];
}
}
}
}
file.Close();
}
我尝试转换的代码如下:
public Map(Context context)
{
String line;
int maxRowLevels, maxColLevels;
int maxRowLevels, maxColLevels;
InputStream inputFile;
BufferedReader file;
inputFile = context.getAssets().open("map.txt");
file = new BufferedReader(new InputStreamReader(inputFile, "UTF-8"));
line = file.readLine();
line = file.readLine();
maxRowLevels = Integer.parseInt(line)-1;
line = file.readLine();
line = file.readLine();
maxColLevels = Integer.parseInt(line)-1;
line = file.readLine();
mapa = new char[maxRowLevels+1][maxColLevels+1][screenRows][screenCols];
for (int rowCurrentLevel = 0; rowCurrentLevel<=maxRowLevels; rowCurrentLevel++)
{
for (int currentRow = 0; currentRow<screenRows; currentRow++)
{
line = file.readLine();
for (int colCurrentLevel = 0; colCurrentLevel<=maxColLevels; colCurrentLevel++)
{
for (int currentCol = 0; currentCol<screenCols; currentCol++)
{
mapa[rowCurrentLevel][colCurrentLevel][currentRow][currentCol] = line[colCurrentLevel*screenCols + currentCol];
}
}
}
}
inputFile.close();
file.close();
}
似乎一切都是正确的,除了这行丢弃了这个错误:“表达式的类型必须是数组类型字符串但它已解析为字符串”
mapa[rowCurrentLevel][colCurrentLevel][currentRow][currentCol] = line[colCurrentLevel*screenCols + currentCol]
;
谁能告诉我如何修复它? 我认为这将是愚蠢的,原谅我,但我是Java的初学者。
提前致谢。
答案 0 :(得分:3)
你没有说它在哪里,但我打赌它在这里......
line[colCurrentLevel*screenCols + currentCol]
哪个应该使用String's charAt(),因为Java中的行不被视为char数组...
line.charAt(colCurrentLevel*screenCols + currentCol)
答案 1 :(得分:2)
我认为问题是在java中String类不支持[]运算符访问特定的char,因此您需要调用line.charAt(colCurrentLevel*screenCols + currentCol)
而不是line[colCurrentLevel*screenCols + currentCol]
答案 2 :(得分:1)
您无法使用[]
访问字符串中的字符
请改用line.charAt(colCurrentLevel*screenCols + currentCol);