Skip to content

Commit 62332ee

Browse files
author
Gonzalo Diaz
committed
[BUGFIX] sonarcloud: Fix this access on a collection that may trigger an 'ArrayIndexOutOfBoundsException'.
Accessing an array element should not trigger an ArrayIndexOutOfBoundsException javabugs:S6466
1 parent 6559d02 commit 62332ee

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

algorithm-exercises-java/src/main/java/ae/projecteuler/Problem0015.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ private Problem0015() {}
1616
*/
1717
public static Long problem0015(Integer gridSide) {
1818

19-
Long[][] grid = new Long[gridSide + 1][gridSide + 1];
19+
int limit = gridSide + 1;
20+
Long[][] grid = new Long[limit][limit];
2021

2122
// initialization
2223
for (int i = 0; i <= gridSide; i++) {
@@ -26,16 +27,16 @@ public static Long problem0015(Integer gridSide) {
2627
}
2728

2829
// vertex computing
29-
for (int i = 1; i <= gridSide; i++) {
30-
for (int j = 1; j <= gridSide; j++) {
30+
for (int i = 1; i < limit; i++) {
31+
for (int j = 1; j < limit; j++) {
3132
long upperParent = grid[i - 1][j];
3233
long leftParent = grid[i][j - 1];
3334

3435
grid[i][j] = upperParent + leftParent;
3536
}
3637
}
3738

38-
Long result = grid[gridSide][gridSide];
39+
Long result = grid[grid.length - 1][grid.length - 1];
3940

4041
String log;
4142
log = String.format("Problem 00015 solved: %d", result);

0 commit comments

Comments
 (0)