Java 如何从原始数组中删除元素

Java 如何从原始数组中删除元素,java,arrays,Java,Arrays,我有一个程序可以弹出并显示,但是我不能在我的程序中弹出弹出的数字,可能我不能删除该数组的元素。这是我目前的代码: import java.io.*; import java.util.*; import java.awt.*; import javax.swing.*; public class Class2 { public static void main(String args[]) throws IOException { BufferedReader in =

我有一个程序可以弹出并显示,但是我不能在我的程序中弹出弹出的数字,可能我不能删除该数组的元素。这是我目前的代码:

import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;

public class Class2 {
    public static void main(String args[]) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out
                .print("1.)Push\n2.)Pop\n3.)Display\n4.)Exit\n\nChoose Item :");
        int x = Integer.parseInt(in.readLine());
        int[] a = new int[10];
        int b = 0;

        int i = 0;

        do
        {
            switch (x) {
            case 1:
                System.out.print("Enter Push number :");
                int v = Integer.parseInt(in.readLine());
                a[b] = v;
                b += 1;

                System.out.print("Item push :" + v + "\n");

                System.out
                        .print("1.)Push\n2.)Pop\n3.)Display\n4.)Exit\n\nChoose Item :");
                x = Integer.parseInt(in.readLine());

                if (x == 3) {
                    for (i = 0; i < b; i++) {
                        System.out.print(a[i] + " ");
                    }
                }
                break;

            case 2:

                System.out.print("Enter Pop Number :");
                int c = Integer.parseInt(in.readLine());

                if (c == a[i]) {

                }

                System.out.print("Item Pop :" + c + "\n");

                System.out
                        .print("1.)Push\n2.)Pop\n3.)Display\n4.)Exit\n\nChoose Item :");
                x = Integer.parseInt(in.readLine());

                if (x == 3) {
                    for (i = 0; i < b; i++) {
                        System.out.print(a[i] + " ");
                    }
                }

                break;
            case 4:
                break;
            }

        } while (x == 1 || x == 2);
    }
}
import java.io.*;
导入java.util.*;
导入java.awt.*;
导入javax.swing.*;
公共课2{
公共静态void main(字符串args[])引发IOException{
BufferedReader in=新的BufferedReader(新的InputStreamReader(System.in));
系统输出
.print(“1.)推送\n2.)弹出\n3.)显示\n4.)退出\n\n选择项目:”;
intx=Integer.parseInt(in.readLine());
int[]a=新的int[10];
int b=0;
int i=0;
做
{
开关(x){
案例1:
系统输出打印(“输入推送编号:”);
intv=Integer.parseInt(in.readLine());
a[b]=v;
b+=1;
系统输出打印(“项目推送:+v+”\n);
系统输出
.print(“1.)推送\n2.)弹出\n3.)显示\n4.)退出\n\n选择项目:”;
x=Integer.parseInt(in.readLine());
如果(x==3){
对于(i=0;i
当您按下
2
5
6
时,您的队列中有3个项目:

a[0] == 2
a[1] == 5
a[2] == 6
当您弹出
2
时,您希望将其从阵列中删除。为此,您需要将弹出
2
的位置后的所有内容复制到上面的位置,以便最终得到:

a[0] == 5
a[1] == 6
这方面的代码如下所示:

int popPosition = findPopPosition();
for(int i = popPosition + 1; i < MAX; i++)
    a[i - 1] = a[i];
b--; // Decrease effective queue size
int-popPosition=findPopPosition();
对于(int i=popPosition+1;i
其中
findPopPosition()
需要找到
2
所在的索引,因此在本例中它将返回
0


或者,用户可以直接输入pop位置(因此pop元素
0
),而不是指定要弹出的值。这可能更有用。

您可以使用下面的代码,它可能会解决您的问题

            int brr[]= new int [a.length-1];
               int j=0;
               for(;j<a.length;j++)
               {
                   if(c== a[j])
                   {
                       break;
                   }
                   else{
                       brr[j]=a[j];
                   }
               }
               for(j=j+1;j<a.length;j++)
               {
                 brr[j-1] =a[j];
               }
int brr[]=新的int[a.length-1];
int j=0;

对于(;j首先,如果要更改数组的元素,则不应使用数组

如果您确实不想使用容器,那么可以动态分配一个新数组,而不需要删除元素(这可能不是一个好主意,因为性能不好,内存浪费)

这可能是返回新数组的方法:

public static int[] removeElementAt(int [] array, int index){
    if(index > 0 && index < array.length){
        int [] newArray = new int[array.length - 1];
        for(int i=0; i<index; i++)
            newArray[i] = array[i]; // copying elements
        for(int i=index+1; i<array.length; i++)
            newArray[i-1] = array[i];
        array = newArray;
    }
    return array;
}
您还可以使用API中的函数
removeElement
,类似于:

array = ArrayUtils.removeElement(array, element)
另一种方法是将数组转换为列表,删除元素,然后将其转换回数组:这不是一个好主意,因为您可以直接使用
ArrayList


因此,当您想要修改内容时,可以使用动态容器,否则可以使用普通的原始数组。

并不是所有的测试用例都已处理,而是根据您的要求使用一个示例工作程序

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ArrayDemo {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the Array Max Capacity");
        int maxSize = Integer.parseInt(br.readLine().trim());
        int[] array = new int[maxSize];
        int arrayCount = 0;
        int choice = 0;
        do {
            System.out.println("\n1.Insert\n2.Delete\n3.Display\n4.Exit");
            choice = Integer.parseInt(br.readLine().trim());
            switch (choice) {
            case 1:
                boolean insertTrue = insertElement(br, maxSize, array,
                        arrayCount);
                if (insertTrue) {
                    arrayCount++;
                }
                break;
            case 2:
                System.out.println("Enter Element to delete");
                int delElement = Integer.parseInt(br.readLine().trim());
                int numOccurrence = getOccurrenceCount(array, arrayCount,
                        delElement);

                int newArrayCount = arrayCount - numOccurrence;
                if (numOccurrence == 0) {
                    System.out.println("No Such Element to delete");
                } else {
                    array = deleteElement(array, arrayCount, delElement,
                            newArrayCount,maxSize);

                }
                arrayCount =newArrayCount;
                break;
            case 3:
                displayArray(array, arrayCount);
                break;
            case 4:
                System.out.println("Exiting");
                break;
            default:
                System.out.println("Invalid Choice, Exiting");
                choice = 4;
            }
        } while (choice >= 1 && choice <= 3);
    }

    private static int[] deleteElement(int[] array, int arrayCount,
            int delElement, int newArrayCount,int maxSize) {
        int newArray[] = new int[newArrayCount];
        int index=0;
        for (int i = 0; i < arrayCount; i++) {
            if (array[i] != delElement) {
                newArray[index] = array[i];
                index++;
            }
        }
        resetArray(array, maxSize);
        for(int i=0;i<newArrayCount;i++){
            array[i]=newArray[i];
        }
        return array;
    }

    private static int[] resetArray(int[] array,int maxSize){
        for(int i=0;i<maxSize;i++){
            array[i]=0;
        }
        return array;
    }

    private static int getOccurrenceCount(int[] array, int arrayCount,
            int delElement) {
        int numOccurrence = 0;
        for (int i = 0; i < arrayCount; i++) {
            if (array[i] == delElement) {
                numOccurrence++;
            }
        }
        return numOccurrence;
    }

    private static void displayArray(int[] array, int arrayCount) {
        if (arrayCount == 0) {
            System.out.println("No Elements to Display");
        } else {
            System.out.println("Array is");
            for (int i = 0; i < arrayCount; i++) {
                System.out.print(array[i] + " ");
            }
            System.out.println("\n");
        }
    }

    private static boolean insertElement(BufferedReader br, int maxSize,
            int[] array, int arrayCount) throws IOException {
        if (arrayCount < maxSize) {
            System.out.println("Enter Element for Insertion");
            array[arrayCount] = Integer.parseInt(br.readLine().trim());
            return true;
        } else {
            System.out.println("Max Limit Reached, Insertion not possible");
            return false;
        }
    }
}
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
公共类ArrayDemo{
公共静态void main(字符串[]args)引发异常{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
System.out.println(“输入阵列最大容量”);
int maxSize=Integer.parseInt(br.readLine().trim());
int[]数组=新的int[maxSize];
int arrayCount=0;
int-choice=0;
做{
System.out.println(“\n1.Insert\n2.Delete\n3.Display\n4.Exit”);
choice=Integer.parseInt(br.readLine().trim());
开关(选择){
案例1:
布尔insertTrue=insertElement(br,maxSize,数组,
阵列计数);
if(insertTrue){
arrayCount++;
}
打破
案例2:
System.out.println(“输入要删除的元素”);
int-delElement=Integer.parseInt(br.readLine().trim());
int numOccurrence=getOccurrenceCount(数组、数组计数、,
删除);
int newArrayCount=arrayCount-numOccurrence;
如果(numocurrence==0){
System.out.println(“无需删除的元素”);
}否则{
数组=删除元素(数组、数组计数、删除元素、,
newArrayCount,maxSize);
}
arrayCount=新arrayCount;
打破
案例3:
显示阵列(阵列、阵列计数);
打破
案例4:
系统输出打印项次(“退出”);
打破
违约:
System.out.println(“无效选择,退出”);
选择=4;
}

}while(choice>=1&&choice您想从最后一个数字或用户想要的任何数字中弹出吗?是,例如,我按了2、5和6
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ArrayDemo {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the Array Max Capacity");
        int maxSize = Integer.parseInt(br.readLine().trim());
        int[] array = new int[maxSize];
        int arrayCount = 0;
        int choice = 0;
        do {
            System.out.println("\n1.Insert\n2.Delete\n3.Display\n4.Exit");
            choice = Integer.parseInt(br.readLine().trim());
            switch (choice) {
            case 1:
                boolean insertTrue = insertElement(br, maxSize, array,
                        arrayCount);
                if (insertTrue) {
                    arrayCount++;
                }
                break;
            case 2:
                System.out.println("Enter Element to delete");
                int delElement = Integer.parseInt(br.readLine().trim());
                int numOccurrence = getOccurrenceCount(array, arrayCount,
                        delElement);

                int newArrayCount = arrayCount - numOccurrence;
                if (numOccurrence == 0) {
                    System.out.println("No Such Element to delete");
                } else {
                    array = deleteElement(array, arrayCount, delElement,
                            newArrayCount,maxSize);

                }
                arrayCount =newArrayCount;
                break;
            case 3:
                displayArray(array, arrayCount);
                break;
            case 4:
                System.out.println("Exiting");
                break;
            default:
                System.out.println("Invalid Choice, Exiting");
                choice = 4;
            }
        } while (choice >= 1 && choice <= 3);
    }

    private static int[] deleteElement(int[] array, int arrayCount,
            int delElement, int newArrayCount,int maxSize) {
        int newArray[] = new int[newArrayCount];
        int index=0;
        for (int i = 0; i < arrayCount; i++) {
            if (array[i] != delElement) {
                newArray[index] = array[i];
                index++;
            }
        }
        resetArray(array, maxSize);
        for(int i=0;i<newArrayCount;i++){
            array[i]=newArray[i];
        }
        return array;
    }

    private static int[] resetArray(int[] array,int maxSize){
        for(int i=0;i<maxSize;i++){
            array[i]=0;
        }
        return array;
    }

    private static int getOccurrenceCount(int[] array, int arrayCount,
            int delElement) {
        int numOccurrence = 0;
        for (int i = 0; i < arrayCount; i++) {
            if (array[i] == delElement) {
                numOccurrence++;
            }
        }
        return numOccurrence;
    }

    private static void displayArray(int[] array, int arrayCount) {
        if (arrayCount == 0) {
            System.out.println("No Elements to Display");
        } else {
            System.out.println("Array is");
            for (int i = 0; i < arrayCount; i++) {
                System.out.print(array[i] + " ");
            }
            System.out.println("\n");
        }
    }

    private static boolean insertElement(BufferedReader br, int maxSize,
            int[] array, int arrayCount) throws IOException {
        if (arrayCount < maxSize) {
            System.out.println("Enter Element for Insertion");
            array[arrayCount] = Integer.parseInt(br.readLine().trim());
            return true;
        } else {
            System.out.println("Max Limit Reached, Insertion not possible");
            return false;
        }
    }
}