[英]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.