Example code for array insertion and deletion of specified elements in JAVA

  • 2021-01-22 05:10:20
  • OfStack

Today learned the Java array, write the array insert and delete, I small white, write not small white look, god please ignore, there is a mistake, please point out;


/**
 Assign the position of the array to the array insert 
*/
import java.util.*;
public class ArrayInsert{
  public static void main(String []args){
    System.out.println(" Please type on the keyboard 5 The number: ");
    int [] array =new int[10];
    Scanner sc=new Scanner(System.in);
    // Enter a number into the array via the keyboard 
    for(int i=0;i<array.length-5;i++){
      array[i]=sc.nextInt();
    }
    // Through the array 
    System.out.print(" The original array for :");
    for(int a:array){
      System.out.print(" "+a);
    }
    // Inserts a number into the specified location 
    System.out.println("\n Please enter the insert location: the valid location is 0-----"+(array.length-1));
    int index=sc.nextInt();
    System.out.println("\n Please enter the inserted value -----");
    int num=sc.nextInt();

    // Calling Static Functions index
    // Iterate through the inserted array 
      System.out.println(" Array traversal after inserting elements: ");
      Insert(index,num,array);
       for(int i=0;i<array.length;i++){
      System.out.print(" "+array[i]);
    }
  }
  // Inserts a data method into an array at the specified location 
  public static int[] Insert(int index,int num,int a[]){
      // If there are elements, the elements after the index are moved backward 1 position ,
      for(int  a[i]=a[i-1];
      }
      a[index]=num;
 return a;   
  }
}

// Deletes the number at the specified position in the array. 
import java.util.*;
public class ArrayDelete{
  public static void main(String args[]){
    System.out.println(" Please type on the keyboard 5 The number: ");
    int [] array =new int[10];
    Scanner sc=new Scanner(System.in);
    // Enter a number into the array via the keyboard 
    for(int i=0;i<array.length-5;i++){
      array[i]=sc.nextInt();
    }
    // Through the array 
    System.out.print(" The original array for :");
    for(int a:array){
      System.out.print(" "+a);
    }
    // Deletes the number at the specified location 
    System.out.println("\n Enter the location you want to delete:   In the range 0---"+(array.length-1));
    int index=sc.nextInt();
    delete(index,array);// call delete methods 
    // Traversal after deletion 
    System.out.println(" Traversal after deletion: ");
    for(int i=0;i<array.length;i++){
      System.out.print(" "+array[i]);
    }
  }
  public static int[] delete(int index,int array[]){
    // Move the end of the array forward according to drop index 1 position 
    for(int i=index;i<array.length;i++){
      if(i!=array.length-1){
        array[i]=array[i+1];
      }else{// Deal with the last 1 Bit out condition 
      array[i]=array[i];
      }
    }
    return array;
  }
}

Related articles: