About Array Deletion issues

Source: Internet
Author: User

Convert the array to list, call the Remove method to delete

There is an ordered array of integers that requires a number to be entered in the array to find out if there is this number,

If so, the number is removed from the array, and the deleted array is still ordered;

If not, the display "array does not have this number!"

This code also includes the bubbling Sort method.

List<integer> list =new arraylist<integer> (); Use this statement to declare a list; you need to declare an array of a to be an Integer before you can assign a value

for (int i=0;i<a.length;i++) {
List.add (A[i]);
}
Use this code to add an array of a into the list

if (D==list.get (i)) {
List.remove (i);
Flag=true;}

In the judgment, must use List.get (i), cannot use List.indexof (); IndexOf is used to intercept the list array; This method can only delete one number if the array to be deleted is 0, 0,1,2,3. If you want to delete 0, you can delete only one 0. This is because when the Remove method deletes a number, it automatically puts the back number up, that is, when the first 0 is deleted, the second 0 acts as the position of the first element in the array, and I in the For loop becomes 1, so the deletion does not drop two 0. The solution to this problem is to declare an empty array to store. The code is as follows:

int b[]=new int[5];

int count=0;
int j=0;
for (int i=0;i<=a.length-1;i++)
{
if (D!=a[i]) {
B[j]=a[i];
j + +;
count++;
}
}
if (count!=0) {
System.out.print ("The deleted array is:");
for (int i=0;i<=b.length-count;i++)
{
System.out.print (B[i]);
}
}else{
System.out.print ("This number was not found");
}

You can also use this method to traverse the entire array for output.

for (int i:list)
{
System.out.print (i);
}

Import java.util.ArrayList;

Import java.util.List;

Import Java.util.Scanner;

public class Sz_test8 {

public static void Main (string[] args) {

Integer A[]=new integer[5];

Boolean flag=true;

Scanner scan=new Scanner (system.in);

int c=0;

System.out.println ("Please enter an array:");

for (int i=0;i<5;i++)

{A[i]=scan.nextint (); }

for (int i=0;i<a.length;i++)

{for (int j=0;j<a.length-i-1;j++)

{if (a[j]>a[j+1])

{C=A[J];

A[J]=A[J+1];

A[j+1]=c;

}

}

}

System.out.println ("Sorted array is:");

for (int i=0;i<a.length;i++)

{

System.out.print (a[i]+ "\ t");

}

Integer D;

System.out.println ("Please enter the number to be deleted");

D=scan.nextint ();

List<integer> list =new arraylist<integer> ();

for (int i=0;i<a.length;i++)

{

List.add (A[i]);

}

for (int i=0;i<=list.size () -1;i++)

{

if (D==list.get (i))

{

List.remove (i);

Flag=true;

Break

}else{

Flag=false;

}

}

if (flag==true) {

System.out.print ("The deleted array is:");

for (int i=0;i<=list.size () -1;i++)

{System.out.print (List.get (i));

}

}else{

System.out.print ("This number was not found");

}

} }

About Array Deletion issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.