简体   繁体   English

为什么我的数组索引0出现stackoverflow错误

[英]Why am I getting a stackoverflow error on index 0 in my array

This is my warrior class. 这是我的战士班。 I wanted to have an array of warriors in this class. 我想在这个班上有一系列的战士。 The idea is to be able to call a method like this --> warrior.select(1) where it would get the warrior created at index 1. Hope that makes sense. 这个想法是能够调用这样的方法-> warrior.select(1),在该方法中将在索引1处创建warrior。 Please explain to me why this error is happening. 请向我解释为什么会发生此错误。

Exception in thread "main" java.lang.StackOverflowError
    at pking.Warrior.<init>(Warrior.java:42)

Code

package pking;

public class Warrior {

    private String name;
    private int age;
    private String call;
    private int attackPower;
    private String weapon;

    public Warrior(String myName, int myAge, String myCall, int myAttackPower) {
        name = myName;
        age = myAge;
        call = myCall;
        attackPower = myAttackPower;
        weapon = "";
        Warrior[] warriors = new Warrior[4];
        warriors[0] = new Warrior("Spartacus", 40, "I AM SPARTACUS!", 9000);
        warriors[1] = new Warrior("Crixus", 35, "CHAMPION OF CAPUA", 8000);
        warriors[2] = new Warrior("Gannicus", 30, "SLAYER", 8000);
        warriors[3] = new Warrior("Alexander", 21, "I AM ALEXANDER, THE CODER", 0);
    }

    //Prints warriors name
    public void name() {
        System.out.println(name);
    }

    //Prints warriors age
    public void age() {
        System.out.println(age);
    }

    //Prints warriors call
    public void warriorsCall() {
        System.out.println(call);
    }

    //Prints warriors attack power
    public void attackPower() {
        System.out.println(attackPower);
    }

    //Equips warriors weapon and prints message
    public void equip(String myWeapon) {
        weapon = myWeapon;
        System.out.println("Equiped the: " + weapon);
    }

    //Prints warriors weapon
    public void weapon() {
        System.out.println(weapon);
    }

}

Your constructor is running an infinite loop as you are calling it recursively. 递归调用时,构造函数正在运行一个无限循环。 Check these lines in your constructor : 在构造函数中检查以下几行:

    Warrior[] warriors = new Warrior[4];
    warriors[0] = new Warrior("Spartacus", 40, "I AM SPARTACUS!", 9000);
    warriors[1] = new Warrior("Crixus", 35, "CHAMPION OF CAPUA", 8000);
    warriors[2] = new Warrior("Gannicus", 30, "SLAYER OF VAGINAS", 8000);
    warriors[3] = new Warrior("Alexander", 21, "I AM ALEXANDER, THE CODER", 0);

for each Warrior array item instantiation you call the same constructor, which again try to create the array and initialise items, this keeps going on until stack overflows. 对于每个Warrior数组项实例化,您调用相同的构造函数,该构造函数再次尝试创建数组并初始化项,此过程一直进行到堆栈溢出为止。

A better design strategy would be to create a new class, say Legion , which will contain a collection of Warrior s: 更好的设计策略是创建一个新类,如Legion ,它将包含Warrior的集合:

public class Legion {
    Warrior[] warriors;

    public Legion() {
        warriors = new Warrior[4];
        warriors[0] = new Warrior("Spartacus", 40, "I AM SPARTACUS!", 9000);
        warriors[1] = new Warrior("Crixus", 35, "CHAMPION OF CAPUA", 8000);
        warriors[2] = new Warrior("Gannicus", 30, "SLAYER OF VAGINAS", 8000);
        warriors[3] = new Warrior("Alexander", 21, "I AM ALEXANDER, THE CODER", 0);
    }

    // getters and setters
}

I am not going to repeat the reason for the StackOverflow exception, as that has already been answered (recursion problem in your constructor). 我将不重复出现StackOverflow异常的原因,因为该异常已经得到解决(构造函数中的递归问题)。

But, I think that what you probably wanted was to create those 4 special warriors once and not for every instance of a Warrior . 但是,我认为您可能想要的是一次创建这四个特殊战士而不是每个Warrior实例。 And maybe you wanted to have them as constants somehow. 也许您想以某种方式将它们作为常量。

So maybe something like this: 所以也许是这样的:

public class Warrior {

    private static final List<Warrior> MEGA_WARRIORS =
        Collections.unmodifiableList(Arrays.asList(
            new Warrior("Spartacus", 40, "I AM SPARTACUS!", 9000),
            new Warrior("Crixus", 35, "CHAMPION OF CAPUA", 8000),
            new Warrior("Gannicus", 30, "SLAYER OF *censored*", 8000),
            new Warrior("Alexander", 21, "I AM ALEXANDER, THE CODER", 0)
        ));

    public static List<Warrior> getMegaWarriors() {
        return MEGA_WARRIORS;
    }

    private String name;
    private int age;
    private String call;
    private int attackPower;
    private String weapon;

    public Warrior(String myName, int myAge, String myCall, int myAttackPower) {
        name = myName;
        age = myAge;
        call = myCall;
        attackPower = myAttackPower;
        weapon = "";
    }

    //Prints warriors name
    public void name() {
        System.out.println(name);
    }

    //Prints warriors age
    public void age() {
        System.out.println(age);
    }

    //Prints warriors call
    public void warriorsCall() {
        System.out.println(call);
    }

    //Prints warriors attack power
    public void attackPower() {
        System.out.println(attackPower);
    }

    //Equips warriors weapon and prints message
    public void equip(String myWeapon) {
        weapon = myWeapon;
        System.out.println("Equiped the: " + weapon);
    }

    //Prints warriors weapon
    public void weapon() {
        System.out.println(weapon);
    }

}

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

相关问题 为什么我会出现stackoverflow? - Why am I getting stackoverflow? 为什么在加载 fxml 时会出现 stackoverflow? - Why am I getting a stackoverflow when loading my fxml? 为什么我得到NoClassDefFoundError异常而不是StackOverflow错误? - Why am I getting a NoClassDefFoundError exception rather than a StackOverflow error? 为什么我得到这个错误数组索引越界 - Why am I getting this error array index out of bounds 我正在尝试在Java的多维数组中对角地向上搜索。 为什么我出现索引超出范围错误? - I am trying to search up to the right diagonally in my multidimensional array in java. Why am I getting index out of bounds error? 为什么我的数组出现NullPointerException? - Why am I getting a NullPointerException with my array? 为什么我的for循环出现此错误? - Why am I getting this error with my for loop? 为什么在解析行时出现数组索引超出界限异常错误? - Why am I getting an Array Index Out Of Bounds Exception error when parsing lines? 我收到以下代码超出范围的数组索引错误……为什么? - i am getting an error of array index out of bounds for the code below…why? 为什么我得到数组索引超出范围异常? - Why am I getting an array index out of bounds exception?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM