0

我的 comp 182 课程中有一个项目,我们正在研究矢量,但我坚持制作“有序矢量”。当我尝试运行它时,我得到一个 ArrayOutofBounds 错误。

(“howMany”变量是数组“theWords”中字符串大小的计数,
代码从另一个类运行,该类读取一个包含 10 个单词的输入文件,使用此“addWord”方法添加单词从文件到“theWords”数组。)

这是我到目前为止的代码:
[顺便说一句,我们不允许只使用“数组”方法“compareTo”]

 public void addWord(String newWord) {
  //adds in words
     if (howMany < theWords.length) {
        theWords[howMany]= newWord; 
        howMany++;
     }  
     else {
        String t [] = new String[capacity+10];
        for (int i=0; i <capacity; i++){
           t[i] = theWords[i];
        }
        theWords = t;
        theWords[howMany] = newWord; 
        howMany++;
     }

    //ordering words
     for(int g = howMany - 1, z = howMany ; g < howMany; g--, z--) {
        if(newWord.compareTo(theWords[g]) < 0) {
           theWords[g] = theWords[z];
           theWords[g] = newWord;
        }
            else
            newWord = theWords[z];          
     }
        howMany++;
  }

任何帮助深表感谢!

4

2 回答 2

2

根据数组的容量,语句

theWords[g] = theWords[z]

将在第一次执行循环时失败,因为 z = 数组的长度(数组中的最后一个索引是长度 - 1)。附带说明一下,在循环中,您最初设置

g = howMany - 1

然后递减它,所以 g < howMany 将永远为真......你将有一个无限循环。当 g 或 z 低于 0 时,这个无限循环也可能导致索引越界异常。

于 2012-09-27T23:06:48.297 回答
1

如果howMany == capacity - 1在函数执行之前,你会遇到问题。

会发生什么:您进入 if (//adds in words) 的第一个分支,然后将howMany其递增,howMany = capacity然后分配z = howMany和访问其边界之外的数组:theWords[g] = theWords[z];.

另一件事是你不增加capacity变量,它必须是capacity+=10.

这是我的变体:

import java.util.Arrays;
import java.util.Random;
public class OrderedArray{

  int howMany = 0;
  String[] theWords = new String[10];

  public void addWord(String newWord) {

    //don't accept nulls
    if(newWord == null) {
      return;
    }
    //if length is reached increase the array
    if(howMany >= theWords.length - 1) {
      theWords = Arrays.copyOf(theWords, theWords.length+10);
    }

    //go through existing words and add newWord if it is less then met value
    boolean isAdded = false;
    for(int idx = 0; idx < howMany && theWords[idx] != null; idx++){                                                          
      if(newWord.compareTo(theWords[idx]) < 0){
        isAdded = true;
        String valToShift = theWords[idx];
        theWords[idx] = newWord; 

        //copy all values after the met index
        for(int shIdx = idx+1; shIdx <= howMany && shIdx < theWords.length; shIdx++){
          String tmp = theWords[shIdx];
          theWords[shIdx] = valToShift;
          valToShift = tmp;
        }        
        break;
      }
    }

    //if a value was not added then add it
    if(!isAdded){
      theWords[howMany] = newWord;
    }
   ++howMany;
  }

  public String toString(){
    StringBuffer sb = new StringBuffer("howMany:");
    sb.append(howMany);
    for(int idx = 0; idx < howMany; idx++){
      sb.append(",");
      sb.append(theWords[idx]);
    }
    return sb.toString();
  }


  public static void main(String[] args){
    OrderedArray m = new OrderedArray();
    Random r = new Random(System.currentTimeMillis());
    for(int i = 0; i < 210; i++){
      m.addWord(Math.abs(r.nextInt())+ "");
    }
    System.out.println(m);

    OrderedArray m1 = new OrderedArray();

    m1.addWord("c");
    m1.addWord("d");
    m1.addWord("a");
    m1.addWord("cd");

    System.out.println(m1);

  }

}
于 2012-09-27T23:14:45.263 回答