Java 金字塔最小和路径2D ArrayList IndexOutOfBounds
我试图计算金字塔的最小自底向上路径和。我正在从文本文件读取输入 我的文本文件示例:Java 金字塔最小和路径2D ArrayList IndexOutOfBounds,java,arraylist,indexoutofboundsexception,Java,Arraylist,Indexoutofboundsexception,我试图计算金字塔的最小自底向上路径和。我正在从文本文件读取输入 我的文本文件示例: 5 6 6 5 3 5 8 3 9 2 1 4 7 9 2 7 第一行通知程序金字塔的大小,其他行正在组装金字塔。(在本例中,大小为5) 这是我的主要方法: public static void main(String[] args) throws FileNotFoundException { if(args.length > 0) { File file1 = new
5
6
6 5
3 5 8
3 9 2 1
4 7 9 2 7
第一行通知程序金字塔的大小,其他行正在组装金字塔。(在本例中,大小为5)
这是我的主要方法:
public static void main(String[] args) throws FileNotFoundException {
if(args.length > 0) {
File file1 = new File(args[0]);
Scanner scanner1 = new Scanner(file1); // The Scanner that is going to read file1.
int pyramidSize = scanner1.nextInt();
scanner1.next();
System.out.println("Pyramid Size = " + pyramidSize);
ArrayList<ArrayList <Integer>> solution1List = new ArrayList<ArrayList <Integer>>(pyramidSize);
// Reads all integers to an array list.
while(scanner1.hasNext()) {
int row = 1;
if(scanner1.hasNextInt()) {
int temporaryForSolution1Integer = scanner1.nextInt();
solution1List.get(row).add(temporaryForSolution1Integer);
}
else {
scanner1.next();
row++;
}
scanner1.close(); // Closed the scanner in order to prevent resource leak.
}
System.out.println("The minimum sum path is = " + minimumSumPath(solution1List));
我认为我的计算方法是正确的
如果有人能帮我解决这个错误,我真的很乐意帮助他/她。solution1List.get(row).add(temporaryForSolution1Integer)代码>该索引处没有元素
创建一个二维列表,如下所示:
ArrayList solution1List=新的ArrayList(金字塔大小)代码>,但您从不向其中添加实际的列表元素
然后才能执行solution1List.get(0)
您必须执行solution1List.add(newarraylist())代码>
请注意,列表
以及数组
都是基于0的。这意味着第一个元素实际上是用0访问的,而不是用1访问的代码>该索引处没有元素
创建一个二维列表,如下所示:
ArrayList solution1List=新的ArrayList(金字塔大小)代码>,但您从不向其中添加实际的列表元素
然后才能执行solution1List.get(0)
您必须执行solution1List.add(newarraylist())代码>
请注意,列表
以及数组
都是基于0的。这意味着第一个元素实际上是用0访问的,而不是用1访问的我也用索引0访问过。我对索引0、大小0有相同的错误。我会尝试你的解决方案,谢谢。@BerkUtkuYenisey如前所述,你首先必须在索引中添加一个新列表ArrayList
不会初始化所有的子列表,因为您可以拥有无限,这会立即使您的程序崩溃。非常感谢您的建议。我已经用我的代码修复了几个错误,现在我的代码可以正常工作了。我也尝试了索引0。我对索引0、大小0有相同的错误。我会尝试你的解决方案,谢谢。@BerkUtkuYenisey如前所述,你首先必须在索引中添加一个新列表ArrayList
不会初始化所有的子列表,因为您可以拥有无限,这会立即使您的程序崩溃。非常感谢您的建议。我已经用我的代码修复了几个错误,现在我的代码可以正常工作了。
public static int minimumSumPath(ArrayList<ArrayList<Integer>> triangle) {
if (triangle.size() == 0)
return 0;
int size = triangle.size();
int min = Integer.MAX_VALUE;
int[] sum = new int[size];
sum[0] = triangle.get(0).get(0);
for(int current = 1; current <= size - 1; current++){
int next_size = triangle.get(current).size();
// it has to start with the end of the array
// because the further one need the clean sum
// value than the newer one.
for(int next = next_size - 1; next >= 0; next--) {
if (next == 0) {
sum[0] = sum[0] + triangle.get(current).get(next);
} else if (next == (next_size - 1)) { // Reaches to the rightmost element of that iteration
sum[next] = sum[next-1] + triangle.get(current).get(next);
} else { // Provides sum[next] to be the minimal sum that can come there
sum[next] = Math.min(sum[next-1], sum[next]) + triangle.get(current).get(next);
}
}
}
for(int i = 0; i < size; i++){
if(sum[i] < min){
min = sum[i];
}
}
return min;
}
PS C:\Users\berku\Desktop\BERKSOL> javac .\berkSol.java
PS C:\Users\berku\Desktop\BERKSOL> java berkSol input1.txt input2.txt
Pyramid Size = 5
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at berkSol.main(berkSol.java:23)