我有一個程序,將推動彈出和顯示,但我不能彈出我的程序中推入的數字可能我不能刪除該數組的元素。這是到目前爲止我的代碼:如何從原始數組中刪除元素
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);
}
}
你想從最後或任何用戶想要的數字? – Prashant
是的,例如我推了2,5和6。然後當我彈出2和6,然後當我選擇顯示屏幕將只有5。 – shampoo
Pop僅適用於最後一個項目,因此您將永遠無法彈出除最後一次推送以外的任何項目 –