从C++到java的代码转换
我已经想出了下面的代码。已设法解决除与Map相关的错误外的大多数错误从C++到java的代码转换,java,c++,Java,C++,我已经想出了下面的代码。已设法解决除与Map相关的错误外的大多数错误 < >我理解下面的代码行属于C++。几天以来,我一直在尝试将其转换为JAVA,但无法找到一种方法: 下面是C语言中的代码行++ map<Character,Integer> enc = new map<Character,Integer>(); 注意:将上述语法更改为HashMap/Map并导入Java.Util后,下面代码中标记为3星的代码行显示以下错误表达式的类型必须是数组类型,但解析为Map 1
< >我理解下面的代码行属于C++。几天以来,我一直在尝试将其转换为JAVA,但无法找到一种方法: 下面是C语言中的代码行++
map<Character,Integer> enc = new map<Character,Integer>();
注意:将上述语法更改为HashMap/Map并导入Java.Util后,下面代码中标记为3星的代码行显示以下错误表达式的类型必须是数组类型,但解析为Map
1 enc[input.charAti]=i;2 int pos=enc[msg.charAti-32];3.
int pos=enc[msg.charAti]
//此函数将破译任何输入消息
public static String ABC(String msg, String input)
{
// Hold the position of every character (A-Z) from encoded string
map<Character,Integer> enc = new map<Character,Integer>();
for (int i = 0; i < input.length(); i++)
{
***enc[input.charAt(i)] = i;***
}
String decipher = "";
// This loop deciphered the message.
// Spaces, special characters and numbers remain same.
for (int i = 0; i < msg.length(); i++)
{
if (msg.charAt(i) >= 'a' && msg.charAt(i) <= 'z')
{
***int pos = enc[msg.charAt(i) - 32];***
decipher += plaintext.charAt(pos);
}
else if (msg.charAt(i) >= 'A' && msg.charAt(i) <= 'Z')
{
***int pos = enc[msg.charAt(i)];***
decipher += plaintext.charAt(pos);
}
else
{
decipher += msg.charAt(i);
}
}
return decipher;
}
使用Enc[Pur.CARATI]的括号运算符语法是原生的C++,在爪哇中不可重载;因此,Java中只允许在使用数组时使用这些括号
您需要在java映射中使用get和putenc.put(input.charAt(i), i);
//...
int pos = enc.get(msg.charAt(i) - 32);
它应该是Map enc=newhashmap;或者其他类型的地图。然后您将需要使用put和get方法。艺龙网的语法是为了让ARIE'SI理解下面的代码行属于C++——它不是有效的C++。在java中没有[]操作符用于映射…@席夫XOC웃Пepeúpaツ 意识到了这一点。谢谢。非常感谢您的清晰解释和快速解决。作为一名新成员,它不允许我投票。我请求其他有权投票的人。我现在明白,方括号也是一个错误。我最近尝试了get和put方法,但没有意识到JAVA中不允许[]用于映射。
//Since Java 7, <> infers the type arguments
Map<Character, Integer> enc = new HashMap<>();
enc[input.charAt(i)] = i;
enc.put(input.charAt(i), i);
//...
int pos = enc.get(msg.charAt(i) - 32);