简体   繁体   English

将代码行从 C++ 转换为 JAVA

[英]Converting lines of code from C++ to JAVA

I have come up with the below code.我想出了下面的代码。 Managed to resolve most of the errors except the one related to Map.设法解决了大多数错误,除了与 Map 相关的错误。

I understand that the below line of code belongs to C++.我知道下面这行代码属于 C++。 Tried a lot to convert it to JAVA since couple of days, unable to figure out a way:几天以来尝试了很多将其转换为JAVA,无法想出办法:

Below lines of code in C++下面的 C++ 代码行

map<Character,Integer> enc = new map<Character,Integer>();

Note:Upon changing the above syntax to HashMap/Map and after importing Java.Util, lines of code marked with 3 stars in the below code displays the following error "The type of the expression must be an array type but it resolved to Map"注意:将上面的语法改为HashMap/Map,并导入Java.Util后,下面代码中标有3星的代码行显示以下错误“表达式的类型必须是数组类型,但已解析为Map”

1) enc[input.charAt(i)] = i; 1) enc[input.charAt(i)] = i; 2) int pos = enc[msg.charAt(i) - 32]; 2) int pos = enc[msg.charAt(i) - 32]; 3) int pos = enc[msg.charAt(i)]; 3) int pos = enc[msg.charAt(i)];

// This function will decipher any input message // 此函数将解密任何输入消息

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;
        }
map<Character,Integer> enc = new map<Character,Integer>();

Map is an interface type in Java, and cannot be instantated [as anything other than an anonymous inner class]. Map是 Java 中的一种接口类型,不能被实例化 [作为匿名内部类以外的任何东西]。 You need to instantiate one of the types that implements Map , like TreeMap or HashMap .您需要实例化实现Map的类型之一,例如TreeMapHashMap

//Since Java 7, <> infers the type arguments
Map<Character, Integer> enc = new HashMap<>();
 enc[input.charAt(i)] = i;

The brackets operator syntax you're using ( enc[input.charAt(i)] ) is native to C++, and is not overloadable in Java;您使用的方括号运算符语法( enc[input.charAt(i)] )是 C++ 原生的,在 Java 中不可重载; thus, the only time these kinds of brackets are allowed in Java is when using an array.因此,Java 中唯一允许使用这些类型的括号是在使用数组时。

You need to use get() and put() with java maps.您需要将get()put()与 java 映射一起使用。

enc.put(input.charAt(i), i);
//...
int pos = enc.get(msg.charAt(i) - 32);

Old question, but for future reference, here's the solution:老问题,但为了将来参考,这是解决方案:

String plaintext = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static String ABC(String msg, String input)
    {
        // Hold the position of every character (A-Z) from encoded string
        Map<Character, Integer> enc = new HashMap<>();
        for (int i = 0; i < input.length(); i++) 
        {
        des.put(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.get((char)(msg.charAt(i)-32));
                decipher += plaintext.charAt(pos);
            }
            else if (msg.charAt(i) >= 'A' && msg.charAt(i) <= 'Z')
            {
                int pos = enc.get(mensaje.charAt(i));
                decipher += plaintext.charAt(pos);
            }
            else
            {
                decipher += msg.charAt(i);
            }
        }
        return decipher;
    }

Basically you have to use put when mapping, and then get when using it.基本上你映射的时候要用put ,用的时候get Oh and casting char when substracting from it.哦,从它减去时铸造 char 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM