【问题标题】:Throwing custom Exceptions, if for loop抛出自定义异常,如果 for 循环
【发布时间】:2014-01-01 08:32:35
【问题描述】:
public class StringArray {
    private String strArr[];

    public StringArray(int capacity) {
       strArr = new String [capacity];
    }

    public int indexOf(String s) throws StringNotFoundException {
        for(int i=0;i<strArr.length ;++i) {
            if (strArr[i].equals(s)) {
                return i;
            } else {
                throw new StringNotFoundException();
            }
        }
    }
}

我想要做的是返回我正在寻找的字符串的索引,如果它在数组中,否则抛出异常。

但是 Eclipse 说我必须返回一个 int。

那么我应该将返回类型更改为 void 还是有其他选项?

StringNotFoundException 是我自己编造的自定义异常。

【问题讨论】:

  • 如果索引对程序的功能至关重要,则返回 -1 或让程序退出
  • 问问自己:如果strArr.length 为0会怎样?
  • 另请注意,如果您搜索的字符串不在数组的第一个槽中,您将始终抛出异常。
  • 还要处理代码格式...您还缺少 for 循环的右括号。
  • 我的问题是:你为什么要实现StringArray

标签: java arrays exception for-loop custom-exceptions


【解决方案1】:

这样做

public int indexOf(String s) throws StringNotFoundException {
     for(int i=0;i<strArr.length ;++i) {
         if (strArr[i].equals(s)){
             return i;
         }
     }
     throw new StringNotFoundException();
}

【讨论】:

    【解决方案2】:

    为什么在这里返回 -1?这是代码:

    public int indexOf(String s) throws StringNotFoundException {
        for(int i=0; i<strArr.length ;++i) {
            if (strArr[i].equals(s)) {
                return i;
            }
        }
        throw new StringNotFoundException();
    }
    

    【讨论】:

      【解决方案3】:

      您需要遍历数组中的每个字符串,并且只有在没有匹配项时才抛出异常。

      我想这就是你想要的:

      public int indexOf(String s) throws StringNotFoundException {
              for (int i = 0; i < strArr.length; ++i) {
                  if (strArr[i].equals(s)) {
                      return i;
                  } 
      
              }
              throw new StringNotFoundException();
      
          }
      

      【讨论】:

        【解决方案4】:

        您没有找到您正在寻找的String 这一事实不足以证明使用Exception 是合理的。这不是一个例外情况,你知道它会发生,你在你的代码中这么说。

        您的代码应该反映这一点。您不应该返回自定义值,即为 -1 之类的东西添加含义,这是不正确的。

        关于这个主题的更多信息:Should a retrieval method return 'null' or throw an exception when it can't produce the return value?

        【讨论】:

          【解决方案5】:

          怎么样:

          /** Returns index of String s in array strArr.  Returns -1 if String s is not found. */
          public int indexOf(String s) {
                  for(int i=0;i<strArr.length ;++i) {
                      if (strArr[i].equals(s)){
                       return i;
                      }
                  return -1;
          }
          

          完全避免使用异常。

          【讨论】:

            【解决方案6】:

            试试这个..

            public int indexOf(String s) throws StringNotFoundException {
            
                   int index = Arrays.binarySearch(strArr ,s);
            
                    if( index > 0)
                         return index;
                    else
                        throw new StringNotFoundException();
                }
            

            你为什么要这样做? .

            【讨论】:

            • 这需要一个排序数组。
            猜你喜欢
            • 2013-09-28
            • 2018-01-27
            • 2011-10-06
            • 2011-05-30
            • 2014-01-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多