java - ArrayIndexOutOfBound异常该如何处理?

标签 java arrays indexoutofboundsexception wordsearch

对于单词搜索程序,我将二十个单词输入到数组列表中,然后将这个单词数组列表转换为一维数组。我有一个名为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/

相关文章:

java - 等待 Executor 中的所有线程完成?

java - Gson: [Class] 声明了多个名为 [property] 的 JSON 字段

java - Google 云端硬盘 SDK 异常

javascript - 如何在 Javascript 中使用 JSON 文件作为对象数组

Conway 的生命游戏 (C) - 坐标不对应于 2D 数组索引?

java - 从 JList 中删除项目

java - 如何在 NetBeans 上编辑库源文件?

PHP Array 在同一个键上合并两个数组

java - 使用 SecureRandom 填充数组 : Why not out of bounds?

java - RecyclerView 和 java.lang.IndexOutOfBoundsException 无效的 View 持有者适配器 positionViewHolder