[英]trying to convert a code block from C# to Java (Android)
I'm trying to convert a method that I have written in C# to Java for Android. 我正在尝试将用C#编写的方法转换为Java for Android。
The following coding is what I'm trying to convert from C# to 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();
}
My attempt to conversion is the following code: 我尝试进行转换的是以下代码:
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();
}
It seems everything is correct except for this line that drops this error: "the type of the expression Must Be an array type string But It resolved to string" 似乎一切正确,除了出现此错误的这一行:“表达式的类型必须是数组类型的字符串,但已解析为字符串”
mapa[rowCurrentLevel][colCurrentLevel][currentRow][currentCol] = line[colCurrentLevel*screenCols + currentCol]
; mapa[rowCurrentLevel][colCurrentLevel][currentRow][currentCol] = line[colCurrentLevel*screenCols + currentCol]
;
Can anyone tell me how to fix it? 谁能告诉我如何解决? I think that it will be silly, forgive me but I am a begginer in Java. 我认为这很愚蠢,请原谅,但我是Java的初学者。
Thanks in advance. 提前致谢。
You don't say where it is but I am betting it is here... 您没有说它在哪里,但我敢打赌它在这里...
line[colCurrentLevel*screenCols + currentCol]
Which should be using String's charAt() since lines are not treated as char arrays in Java... 应该使用String的charAt(),因为在Java 中行不被视为char数组...
line.charAt(colCurrentLevel*screenCols + currentCol)
我认为问题是在Java中,String类不支持[]运算符来访问特定的char,因此您需要调用line.charAt(colCurrentLevel*screenCols + currentCol)
而不是line[colCurrentLevel*screenCols + currentCol]
You can't acess to character in stirng with []
Use line.charAt(colCurrentLevel*screenCols + currentCol);
您不能使用[]
来打动字符。使用line.charAt(colCurrentLevel*screenCols + currentCol);
instead 代替
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.