Method One:
Package Delete;
Import Java.util.Scanner;
public class Delete {
public static void Main (string[] args) {
Scanner input = new Scanner (system.in);
int[ ] num = new int[]{3,6,9,13};
System.out.print ("Please enter the number to be deleted:");
int number = Input.nextint ();
Boolean isfind =false;
int i;
for (i = 0; i < num.length; i++) {
if (Number==num[i]) {
isfind=true;
break;//found
}
}
If (Isfind) {
System.out.println ("There are numbers in the array to delete" +number+ "," + (i+1) + "in the array num, subscript" +i ");
Int[] Nums=new int[num.length-1];//Subsequent actions
if (i==num.length-1) {
for (int j = 0; J < Nums.length; J + +) {
Nu MS[J]=NUM[J];
}
}
else{
for (int j2 = i; J2 < num.length-1; j2++) {
num[j2]=num[j2+1];
}
for (int k = 0; k < nums.length; k++) {
Nums[k]=num[k];
}
}
for (int j = 0; J < Nums.length; J + +) {
System.out.print (nums[j]+ "\ t");
}
}
else{
System.out.print ("No number in array num to delete" +number);//Not Found
}
}
}
Method Two:
Package Delete;
Import Java.util.Scanner;
public class Anothermethod {
public static void Main (string[] args) {
Scanner input=new Scanner (system.in);
Int[] num={3,6,9,12,15};
System.out.println ("Please enter the number you want to delete:");
int number =input.nextint ();
Boolean Isfind =false;
int i;
for (i = 0; i < num.length; i++) {
if (Number==num[i]) {
Isfind=true;
Break
The number that was found to be deleted
}
}
if (isfind) {
System.out.println ("You enter the number to delete" +number+ ", the" + (i+1) + "item in the array num, subscript" +i ");
Int[] Nums=new int[num.length-1];
for (int j = 0; J < Nums.length; J + +) {
NUMS[J]=NUM[J];
if (j==i) {
NUMS[J]=NUM[J+1];
i++;
}
}
for (int k = 0; k < nums.length; k++) {
System.out.print (nums[k]+ "\ t");
}
}
else{
System.out.println ("The number you entered does not exist in the array num, cannot be deleted");//The number to delete is not found
}
Input.close ();
}
}
Summary: Delete an item in an array and output the deleted array the problem needs to take into account the array's staying power, and cannot use the collection to delete an item's method. Delete an item in an array, divided into several steps:
1. Enter the item to be deleted;
2. Iterate through the array to find out if there are any items to delete;
3. The method is ① in two cases: the item to be deleted is the last item, each item in the original array is assigned to a new array and then output; ②: The item to be deleted is not the last item, and the item to be deleted begins by assigning the following item to the previous item of the array, then assigning each item of the assigned value to the new array and outputting it.
Method Two assigns the deleted array directly to each item in the new array and outputs it, eliminating the ② steps in the case of method one. The idea of method two is to assign a value to the new array first, and wait until the item to be deleted is assigned to the previous item of the new array by assigning the latter item to the next one that was found, eliminating the number of the code.
The first approach is straightforward, and the second approach is a little bit around but can reduce the number of code and improve execution efficiency.
Delete an item in an array and output the deleted array