简体   繁体   English

HashMap.put在Java中导致无限循环

[英]HashMap.put causing an infinite loop in Java

So I have a class named MainControl that is ran from another class (The main one) that I am certain only runs once. 所以我有一个名为MainControl的类,它从另一个类(主要的)运行,我确定只运行一次。 Inside of MainControl I have a few things that have to be loaded, one of which being a function that populates the HashMap with the key set to the keybind (int) and the values set to a class that holds the information of the specific keybinds function (KeyDetails). 在MainControl中我有一些必须加载的东西,其中一个是用键设置为keybind(int)来填充HashMap的函数,以及设置为保存特定keybinds函数信息的类的值(KeyDetails)。

So to populate the hashmap it goes through 2 loops, the first being to loop through the list of functions, the second to check if the key should be bound to the function. 因此,要填充哈希映射,它将经历2个循环,第一个循环遍历函数列表,第二个是检查密钥是否应该绑定到函数。 If the second loop finds that it should be bound it will run Keybinds.put(KeyCode, new Details(Function, KeyCode, KeyName, false); (Just ignore the false). 如果第二个循环发现它应该被绑定,它将运行Keybinds.put(KeyCode,new Details(Function,KeyCode,KeyName,false);(只需忽略false)。

For some reason it ends up forcing MainControl(); 由于某种原因,它最终强制MainControl(); to run again once it reached Keybinds.put... for no reason at all. 一旦到达Keybinds.put就再次运行......完全没有理由。 There are no functions that should cause MainControl to run and it works when I remove the Keybinds.put line. 没有任何函数可以导致MainControl运行,并且当我删除Keybinds.put行时它可以工作。 Just by removing THAT single line it works. 只需删除它的单行即可。

public MainControl()
{   
    System.out.println("Starting System");
    LoadSession("Default");
    System.out.println("Ended System - Never Reached");
}

public static void LoadSession(String s)
{
    Keybinds = new HashMap();

    for (int i = 0; i < FunctionStringList.length; i++)
    {
        String Key = "";
        int KeyVal = 0;

        try
        {                           
            for (int a = 0; a < KeyBindingList.length; a++)
            {
                if (KeyBindingList[a].KeyName.equalsIgnoreCase(FunctionStringList[i]))
                {
                    Key = KeyBindingList[a].KeyName
                    KeyVal = KeyBindingList[a].KeyCode
                }
            }            


            Keybinds.put(KeyVal, new Details(FunctionStringList[i], KeyVal, Key, false));

            System.out.println("Key: " + Key + " Val: " + KeyVal + " Hack: " + FunctionStringList[i]);      
        }
        catch (Exception E) { E.printStackTrace(); }        
    }
}

public static String FunctionStringList[] =
{
    "Forward", "Backwards", "StrafeLeft", "StrafeRight", "Jump", "Sneak"
};

Details Class: 详情类别:

public class Details extends MainControl
{
public Details(String Name, int KeyCode, String KeyName2, boolean Bool)
{       
    FunctionName = Name;
    Code = KeyCode;
    KeyName = KeyName2 != null ? KeyName2 : "None";
    State = Bool;
}

public boolean Toggle()
{
    State = !State;
    return State;
}

public void SendChat(String s)
{
    Console.AddChat(s);
}

public String FunctionName;
public String KeyName;
public int Code;
public boolean State;
}

Your Details class is-a MainControl ; 您的Details是一个 MainControl ; it's a subclass. 它是一个子类。

When you extend a class, the child class' constructor is calling the parent object's no-arg constructor which is causing an infinite recursion. 扩展类时,子类的构造函数正在调用父对象的无参数构造函数,这会导致无限递归。

Edit to add from the comment below: Your "offending line" is: 编辑以添加以下评论:您的“违规行”是:

Keybinds.put(KeyVal, new Details(FunctionStringList[i], KeyVal, Key, false));

When the Details constructor executes, it then calls MainControl() ... which then calls LoadSession() ... which then creates a new Details ... which then calls MainControl() .. etc, etc. Infinite recursion until you get a Stack Overflow. Details构造函数执行时,它然后调用MainControl() ...然后调用LoadSession() ...然后创建一个新的Details ...然后调用MainControl() .. etc等。无限递归直到你得到堆栈溢出。

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

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