我写了一段代码,在其中我制作了一个接口和一个实现该接口的类。现在,当我运行代码时,我给自己两个选项,即1用于输入字符串,2用于退出。现在,当我运行代码时,如果我选择1,它会让我放入字符串,但在这之后,当我按0时,它会给我错误:线程中的异常";主";java.lang。有人能帮我弄清楚我在这里缺了什么吗。以下是代码:
import java.util.ArrayList;
import java.util.List;
public interface ITimsSaveable {
List<String> write(); // this will save the data;
void read (List<String> savedValues); //this will print the data
}
import java.util.ArrayList;
import java.util.List;
public class TimsPlayers implements ITimsSaveable {
private String name;
private int hitPoints;
private int strength;
private String weapon;
public TimsPlayers(String name, int hitPoints, int strength) {
this.name = name;
this.hitPoints = hitPoints;
this.strength = strength;
this.weapon = "Sword";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHitPoints() {
return hitPoints;
}
public void setHitPoints(int hitPoints) {
this.hitPoints = hitPoints;
}
public int getStrength() {
return strength;
}
public void setStrength(int strength) {
this.strength = strength;
}
public String getWeapon() {
return weapon;
}
public void setWeapon(String weapon) {
this.weapon = weapon;
}
//now go to generate > to string method
@Override
public String toString() {
return "TimsPlayers{" +
"name='" + name + ''' +
", hitPoints=" + hitPoints +
", strength=" + strength +
", weapon='" + weapon + ''' +
'}';
}
//now let us create stubs, i.e. the empty methods from interface
@Override
public List<String> write() {
List<String > values = new ArrayList<String >();
values.add(0,this.name);
values.add(1,""+this.hitPoints); //"" is quick trick to convert int to String
values.add(2,""+this.strength);
values.add(3,this.weapon);
return values;
}
@Override
public void read(List<String> savedValues) {
if ((savedValues != null) && (savedValues.size() > 0)) {
this.name = savedValues.get(0);
this.hitPoints = Integer.parseInt(savedValues.get(1));
this.strength = Integer.parseInt(savedValues.get(2));
this.weapon = savedValues.get(3);
}
// now let us go back to our main class
}
}
public class Main {
public static void main(String[] args) {
TimsPlayers tim = new TimsPlayers("Tim",10,15);
System.out.println(tim.toString());
saveObject(tim);
tim.setHitPoints(8);
System.out.println(tim);
tim.setWeapon("Stormbringer");
saveObject(tim);
loadObject(tim);
System.out.println(tim);
}
public static ArrayList<String> readValues() {
ArrayList<String> values = new ArrayList<String>();
Scanner scanner = new Scanner(System.in);
boolean quit = false;
int index = 0;
System.out.println("Choosen" +
"1 to enter a stringn" +
"0 to quit");
while (!quit) {
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 0:
quit = true;
break;
case 1:
System.out.print("Enter a string: ");
String stringInput = scanner.nextLine();
values.add(index, stringInput);
index++;
break;
}
}
return values;
}
public static void saveObject(ITimsSaveable objectToSave) { //ITims is the interface that is being implemented by the player
//class,so we can use ITims as object type
for (int i = 0; i < objectToSave.write().size(); i++) {
System.out.println("Saving " + objectToSave.write().get(i) + " to storage device");
}
}
public static void loadObject (ITimsSaveable objectToLoad) {
ArrayList<String > values = readValues();
objectToLoad.read(values);
}
}
Stacktrace:
线程中的异常"主";java.lang.IndexOutOfBoundsException:索引1超出长度1的界限位于java.base/jdk.internal.util.Prequisitions.outOfBounds(Preconditions.java:64(位于java.base/jdk.internal.util.Prequisitions.outOfBoundsCheckIndex(Preconditions.java:70(位于java.base/jdk.internal.util.Prequisitions.checkIndex(Preconditions.java:248(位于java.base/java.util.Objects.checkIndex(Objects.java:373(位于java.base/java.util.ArrayList.get(ArrayList.java:427(网址:org.mm.ssandbox.so.TimsPlayers.read(TimsPlayers.java:77(网址:org.mm.ssandbox.so.Main.loadObject(Main.java:62(在org.mm.ssandbox.so.Main.Main(Main.java:17(
此方法
@Override
public void read(List<String> savedValues) {
if ((savedValues != null) && (savedValues.size() > 0)) {
this.name = savedValues.get(0);
this.hitPoints = Integer.parseInt(savedValues.get(1));
this.strength = Integer.parseInt(savedValues.get(2));
this.weapon = savedValues.get(3);
}
// now let us go back to our main class
}
期望savedValues
完全填充,即具有4个元素——名称、生命值、力量和武器。你做了savedValues.size() > 0
,但这并不意味着你没有只输入一个所需的,然后停止,它仍然试图访问一个不存在的元素-this.hitPoints = Integer.parseInt(savedValues.get(1));