Answers for "java program on remove element from string array"

1

Java program to delete specified integer from an array

// Java program to delete specified integer from an array
import java.util.Scanner;
public class DeleteSpecifiedInteger
{
   public static void main(String[] args)
   {
      int num, n, temp = 1, place = 0;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter number of elements: ");
      num = sc.nextInt();
      int[] arrNum = new int[num];
      System.out.println("Please enter all the elements: ");
      for(int a = 0; a < num; a++)
      {
         arrNum[a] = sc.nextInt();
      }
      System.out.println("Enter the element you want to delete: ");
      n = sc.nextInt();
      for(int a = 0; a < num; a++)
      {
         if(arrNum[a] == n)
         {
            temp = 1;
            place = a;
            break;
         }
         else
         {
            temp = 0;
         }
      }
      if(temp == 1)
      {
         for(int a = place + 1; a < num; a++)
         {
            arrNum[a - 1] = arrNum[a];
         }
         System.out.println("After deleting element: ");
         for(int a = 0; a < num - 2; a++)
         {
            System.out.print(arrNum[a] + ",");
         }
         System.out.print(arrNum[num - 2]);
      }
      else
      {
         System.out.println("Element not found!!");
      }
      sc.close();
   }
}
Posted by: Guest on December-23-2020
5

how to delete an element from an array in java

import java.util.Scanner;

public class ElemRemoval {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[] intArr = {1, 2, 5, 12, 7, 3, 8};
        System.out.print("Enter Element to be deleted : ");
        int elem = in.nextInt();
        
        for(int i = 0; i < intArr.length; i++){
            if(intArr[i] == elem){
                // shifting elements
                for(int j = i; j < intArr.length - 1; j++){
                    intArr[j] = intArr[j+1];
                }
                break;
            }
        }
      
        System.out.println("Elements -- " );
        for(int i = 0; i < intArr.length - 1; i++){
            System.out.print(" " + intArr[i]);
        }                
    }
}
Posted by: Guest on April-29-2020

Code answers related to "java program on remove element from string array"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language