Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何删除数组中的最后一个输入元素?_Java_Arrays_Input_Indexoutofboundsexception_Array Algorithms - Fatal编程技术网

Java 如何删除数组中的最后一个输入元素?

Java 如何删除数组中的最后一个输入元素?,java,arrays,input,indexoutofboundsexception,array-algorithms,Java,Arrays,Input,Indexoutofboundsexception,Array Algorithms,对于这个程序,我让用户使用private static void add()方法将元素输入到一个5空间数组中。添加这些元素后,用户就可以使用private static void delete()方法,该方法允许用户输入他们希望删除的数组中的数字。当输入要删除的数字时,除非我尝试删除数组的currentSize的最后一个数字,否则程序工作得非常好。例如,如果我有一个具有以下索引和值的数组: 0. = 1 1. = 2 2. = 3 3. = 4 4. = <empty> /**

对于这个程序,我让用户使用
private static void add()
方法将元素输入到一个5空间数组中。添加这些元素后,用户就可以使用
private static void delete()
方法,该方法允许用户输入他们希望删除的数组中的数字。当输入要删除的数字时,除非我尝试删除数组的
currentSize
的最后一个数字,否则程序工作得非常好。例如,如果我有一个具有以下索引和值的数组:

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

/**
*一个类,包含执行添加、更新、删除、删除、调整大小和结束命令的方法,以使用5个整数更改数组的状态。
*/
公共类数组管理器
{
//要修改的5个整数的数组
私有静态int[]值=新int[5];
私有静态int currentSize=0;
私有静态int位置=0;
//私有静态int索引=0;
静态扫描仪输入=新扫描仪(系统输入);
/**
*一种方法,只要输入的值在1到99之间,就将其插入数组。如果数组已满,将打印一条错误消息,说明数组已满。
*/
私有静态void add()
{
System.out.println(“输入介于1和99之间的值,包括要添加到数组中的值”);
if(在.hasNextInt()中)
{
int n=in.nextInt();

如果(n>=1&&n位置是值数组最后一个元素的索引的角情况处理不当。在这种情况下,代码开始从下一个索引迭代元素,以便将所有元素移动1个位置,而for循环中的条件不满足该条件

for(int i=position+1;i

for(int i=position+1;i
解决方案是检查该条件并显式处理它

   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;
   } 
if(值[位置]==n){
如果(位置!=values.length-1){
对于(int i=位置+1;i
delete
函数的内部
循环中,您正在改变
i
直到
position+1
。因此
position
的最大值为
values.length
。但是,在接下来的行中,您正在调用,
值[i-1]=values[i];
。有
值[values.length]
不是有效的索引,因为max index是
values.length-1
,因此会给您一个
ArrayOutOfBoundException
这是一种赋值吗?我在问,因为有些更改,比如(一些)成员是非静态的,将读取要添加/删除的数字的代码放在相应方法之外,并将其作为参数传递给这些方法,可能会改进程序(与错误无关)@fabian是的,这是一项任务。讲师建议我们使用这种风格。
/**
 * 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>");
            }
        }
    }
    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;
   }