【发布时间】:2018-03-19 12:30:28
【问题描述】:
我有一个触发异常IndexOutOfBoundsException 的打印语句。这是代码:
主要方法
public class AdvDotComLauncher {
public static void main(String[] args) {
AdvDotComTable table = new AdvDotComTable();
table.createTable(5,5);
}
}
表格类
import java.util.ArrayList;
public class AdvDotComTable {
public boolean createTable(int rows, int columns) {
if (rows == 26) {
//When true is returned, the program will let the user know they have to choose a lower number
return true;
}
String[] dotComs = {"Pets.com", "Amazon.com", "Target.com", "Apple.com", "Microsoft.com", "Steampowered.com"};
ArrayList<Character> row = new ArrayList<Character>();
ArrayList<Integer> column = new ArrayList<Integer>();
char[] letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
int number = 0;
int rows2 = rows;
int columns2 = columns;
while (rows2 != 0 && columns2 != 0) {
row.add(letters[number]);
if(number == columns) {
column.add(number);
number = 0;
columns2--;
}
System.out.println(row.get(number) + "" + column.get(number));
number++;
rows2--;
}
return false;
}
}
错误
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 0 out-of-bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:439)
at AdvDotComTable.createTable(AdvDotComTable.java:23)
at AdvDotComLauncher.main(AdvDotComLauncher.java:4)
我不知道是什么导致了这个问题,我们将不胜感激。我是一名业余开发人员,只是想学习,所以这可能是一个愚蠢的错误(对此感到抱歉)。
在此先感谢,
生活
【问题讨论】:
标签: java indexoutofboundsexception