1

对于此程序,我使用private static void add()方法将用户输入元素转换为5空间阵列。添加这些元素后,用户就可以使用方法,允许他们输入他们希望删除的数组中存在的数字。当输入要删除的数字时,程序完全正常工作,除非我尝试删除阵列的currentSize的最后一个数字。例如,如果我有以下索引和值的数组:如何删除数组中的最后一个输入元素?

0. = 1 
1. = 2 
2. = 3 
3. = 4 
4. = <empty> 

阵列的currentSize目前4.如果我尝试在索引3除去值4,该程序将不删除值4。如果尝试删除值4后尝试删除值3,2或1,则这些值也不会删除。另一方面,如果我想先删除低于值4的任何值,即值1,2和3,程序将正常工作,直到我尝试删除索引0中的值4.如果我试图删除值4在这一点上,没有任何东西被删除。如果尝试删除值4后尝试添加值(例如1),则值4将替换为值1.如果我尝试在索引0处删除值4两次,然后尝试添加新值,则会得到一个IndexOutOfBoundsException: -1 。我相信这与currentSize--减少,当它不是在private static void delete()方法中存在的移除元素算法中。如果有人有这个解决方案,将不胜感激,谢谢。该计划在下面发布。我评论了private static void delete()方法中给我提出问题的部分。这是我得到的堆栈跟踪:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 
    at ArrayManager.add(ArrayManager.java:35) 
    at ArrayManager.main(ArrayManager.java:216) 

/** 
* A class that contains methods to carry out the Add, Update, Delete, Drop, Resize, and End commands to alter the state of an array with 5 integers. 
*/ 
public class ArrayManager 
{ 
    // Array of 5 integers to be modified 
    private static int [] values = new int [5]; 

    private static int currentSize = 0; 
    private static int position = 0; 
    //private static int index = 0; 

    static Scanner in = new Scanner(System.in); 

    /** 
    * A method that inserts an entered value into the array as long as it is between 1 and 99. If the array is full, an error message will be printed explaining that the array is full. 
    */ 
    private static void add() 
    { 
     System.out.println("Enter values between 1 and 99, inclusive that you would like to add to the array."); 
     if(in.hasNextInt()) 
     { 
      int n = in.nextInt(); 
      if(n >= 1 && n <= 99) 
      { 
       if(currentSize < values.length) 
       { 
        values[currentSize] = n; 
        currentSize++; 
       } 
       else 
       { 
        System.out.println("ERROR: The array is currently full."); 
       } 
      } 
      else 
      { 
       System.out.println("ERROR: The number entered must be between 1 and 99, inclusive."); 
      } 
     } 
     else 
     { 
      System.out.println("ERROR: String has been entered. Enter an Integer."); 
     } 
    } 

/** 
    * A method that asks the user to enter a value they wish to delete in the array. The following values are then shifted down in index in the array. If the value chosen does not exist in the array, an error message is displayed explaining that the value entered does not exist in the array. 
    */ 
    private static void delete() 
    { 
     int n = 0; 
     System.out.println("Please enter the value in the array that you wish to remove."); 
     if(in.hasNextInt()) 
     { 
      n = in.nextInt(); 
      for(position = 0; position < values.length; position++) 
      { 
       if(values[position] == n) 
       { 
       // The stack trace points me back to this section of code which removes the specified value in the values array. 
        for(int i = position + 1; i < currentSize; i++) 
        { 
          values[i - 1] = values[i]; 
          values[i] = 0; 
        } 
        currentSize--; 
        break; 
       } 
       else if(position == values.length - 1) 
       { 
        System.out.println("ERROR: The value entered does not exist in the array."); 
       } 
      } 
     } 
     else 
     { 
      System.out.println("ERROR: String has been entered. Enter an Integer."); 
     } 
    } 

/** 
    * A method that prints out the modified array. 
    */ 
    public static void printArray() 
    { 
     System.out.println("* Current Array Contents *"); 
     for(int i = 0; i < values.length; i++) 
     { 
      if(values[i] != 0) 
      { 
       System.out.println(i + ". = " + values[i]); 
      } 
      else if(values[i] == 0) 
      { 
       System.out.println(i + ". = <empty>"); 
      } 
     } 
    } 
+0

在'delete'函数的内部'for'循环中,您正在改变'i'直到'位置+1'。因此“位置”将具有“values.length”的最大值。然而,在后面的一行中,'values [i - 1] = values [i];'。有'values [values.length]'不是一个有效的索引,因为最大索引是'values.length -1',因此给你一个'ArrayOutOfBoundException' –

+0

这是一种分配?我在问,因为一些变化,比如让一些成员变成非静态变量,并将读取数字的代码添加到/删除到相应方法之外,并将它们作为参数传递给方法,这可能会改进程序独立于bug) – fabian

+0

@fabian是的,这是一个任务。教练建议我们使用这种风格。 – RunnerDude3000

回答

1

角落情况下位置值数组的最后一个元素的索引,处理不当。在这种情况下,代码开始迭代来自下一个索引的元素,以便将所有元素移位1个位置,并且条件不满足for循环中的条件。

for(int i = position + 1; i < currentSize; i++)

for(int i = position + 1; i < currentSize; i++) 
    { 
     values[i - 1] = values[i]; 
     values[i] = 0; 
    } 

的解决办法是检查该条件,并明确处理。

if(values[position] == n) { 
     if(position != values.length - 1) { 
      for(int i = position + 1; i < currentSize; i++) 
      { 
       values[i - 1] = values[i]; 
       values[i] = 0; 
      } 
     } else { 
      values[i] = 0; 
     } 
     currentSize--; 
     break; 
    } 
相关问题