- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于单词搜索程序,我将二十个单词输入到数组列表中,然后将这个单词数组列表转换为一维数组。我有一个名为createWordSearch(words) 的方法,其中words 是一维数组。
还有一些其他方法可以帮助在此方法中创建整个单词搜索,例如 location(wordArr, word, dir, pos)、placeWord(wordArr, word)、placeMessage(wordArr, message)。我在位置方法中遇到 ArrayIndexOutOfBound 异常,特别是在:if ((DIRECTIONS[dir][0] == 1 && (word.length() + c) > cols)
。
哪里for (int dir = 0; dir < DIRECTIONS.length; dir++) { dir = ( (dir) + (randDirection) % DIRECTIONS.length);
, int randDirection = rand.nextInt(DIRECTIONS.length);
和public static final int[][] DIRECTIONS = {{1,0}, {0,1}, {1,1}, {1,-1}, {-1,0}, {0,-1}, {-1,-1}, {-1,1}};
我试图理解为什么我会遇到这个异常,但我可以 t pinpoint exactly where, which is why I am having trouble fixing my code. I am assuming the error happens because of that for loop for
目录listed above, but I
我不太确定。
// Create a method that places the word letters at a certain location
public static int location (WordArray wordArr, String word, int dir, int pos) {
int r = ( (pos) / (cols)); // Where r = row
int c = ( (pos) / (cols)); // Where c = column
// Checking the bounds...
if ((DIRECTIONS[dir][0] == 1 && (word.length() + c) > cols)
|| (DIRECTIONS[dir][0] == -1 && (word.length() - 1) > c)
|| (DIRECTIONS[dir][1] == 1 && (word.length() + r) > rows)
|| (DIRECTIONS[dir][1] == -1 && (word.length() - 1) > r)
)
return 0;
int i, cc, rr, overLaps = 0;
// Checking the cells...
for (i = 0, rr = r, cc = c; i < word.length(); i++) {
if (rr < rows && cc < cols && i < word.length()) {
return 0;
}//end of if
cc += DIRECTIONS[dir][0];
rr += DIRECTIONS[dir][1];
}//end of for loop
// Placing the word...
for (i = 0, rr = r, cc = c; i < word.length(); i++) {
if (rr < rows && cc < cols && i < word.length()) {
overLaps++;
}//end of if
if (i < word.length() - 1) {
cc += DIRECTIONS[dir][0];
rr += DIRECTIONS[dir][1];
}//end of inner if
}//end of for loop 2
int lettersPlaced = ( (word.length()) - (overLaps));
if (lettersPlaced > 0)
wordArr.solutions.add(String.format("%-10s (%d,%d)(%d,%d)", word, c, r, cc, rr));
return lettersPlaced;
}//end of location(wordArr,word,dir,pos)
最佳答案
我的猜测是,它的原因是您仅对分配值的一部分而不是整个进行模运算:
for (int dir = 0; dir < DIRECTIONS.length; dir++)
{
dir = ( (dir) + (randDirection) % DIRECTIONS.length);
}
应该是:
for (int dir = 0; dir < DIRECTIONS.length; dir++)
{
dir = ( (dir) + (randDirection) ) % DIRECTIONS.length;
}
一些关键说明,您问题中的格式不太好 - 格式应将问题分成关键部分,为我们提供简单的方法来准确理解您所问的内容,并应格式化所有代码部分以区分他们与其他人不同。
您可能也应该阅读有关重构的内容 - 在当今编译器格式化我们的代码并向我们准确显示每个 block 开始和结束的位置 - 实际上没有必要对每个括号结尾进行注释。
给你的变量起一个有意义的名字,比如 r、rr、c、cc,这对于那些不知道你的代码应该做什么的人来说是相当困惑的。
您还可以将部分代码提取到单独的方法中,以使其更加清晰。
到达终点
for (i = 0, rr = r, cc = c; i < word.length(); i++) {
if (rr < rows && cc < cols && i < word.length()) {
此检查是多余的,可以更改为
for (i = 0, rr = r, cc = c; i < word.length(); i++) {
if (rr < rows && cc < cols) {
关于java - ArrayIndexOutOfBound异常该如何处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54224272/
实体类是: DeviceWithReading.java package com.fde.entity; import java.util.Set; import javax.persistence.
这个问题在这里已经有了答案: What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? (26 个
我有一个小问题,当我尝试从文件读取时,我会遇到 arrayindexoutofboundsException 。我不知道有更好或更详细的解释方法,所以我将粘贴下面的代码和错误。这一切都在我的程序的 m
我写了这个方法: public static Bitmap matrixToBitmap(int[][] slika) { int w = slika[0].length;
我的代码需要一些帮助,当我尝试运行它时,它给了我 arrayindexoutofbounds 0 异常,它指向“results[counter]=random;”行,以及我在它之前写的 system.
我需要以蛇的形式打印矩阵。因此对于这个矩阵,输出应该是: 我的问题是这段代码抛出 ArrayIndexOutofBounds。我该如何处理才能避免这种情况? int[][] mat= {{1,2,3}
我正在制作阿拉伯数字转换器并收到错误: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at ar
我不明白为什么会发生这种情况。如果没有 print 语句,代码可以正常工作,但是当我尝试打印元素时,我得到 ArrayIndexoutOfBounds。例如,如果我尝试提供 3 个元素,则会抛出异常。
我正在尝试将 Google 任务与我的应用程序同步。为此,我创建了一个类,在其中创建了需要获取任务列表和创建列表等的所有方法。 现在我想测试一下这些方法是否有效。为此,我创建了一个扩展 AsyncTa
这个问题已经有答案了: What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? (25 个回答)
所以我目前正在研究多维数组(2D),并且尝试反转二维数组中每个数组的顺序。 所以我有一个二维数组设置为:int firstArray[][] = {{5,6,7,8,9,10}, {11,12,13,
问题出在int [][]tam = new int [a][b]处。就只有那一条线。我是 Java 新手,有 C++ 背景。 //"Exercitiul" 3 Scanner input = new
这是我遇到索引越界异常的代码,我不明白为什么, int index = array.length - 1; E item = array[index]; while (item == null
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
public class Environment { //variables RoundingStage[][] land; int horizontalStreets;
我正在尝试创建一种使用动态编程计算(N 选择 R)的方法,但出现数组越界异常: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsEx
public Pasient[] finnPasient(String dato) { int j = 0; Pasient[] p = new Pasient[j]; for
所以我有一个数组 Canvas[256][256],它的随机索引 Canvas[r][r] (r 是随机的)设置为 1。然后我想循环遍历该数组以准确查看哪个索引是不是 0,然后随机选择一个点(上、下、
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我写了一个 TokenizableString 类,它对用户输入的字符串进行标记化。这是它应该如何进行的示例 我输入 "My name is methos" 我应该在控制台中看到以下内容 'My' '
我是一名优秀的程序员,十分优秀!