Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

. Java Program to Delete the Specified Integer from an Array

import java.util.Scanner;
public class Delete
{
    public static void main(String[] args) 
    {
        int n, x, flag = 1, loc = 0;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter no. of elements you want in array:");
        n = s.nextInt();
        int a[] = new int[n];
        System.out.println("Enter all the elements:");
        for (int i = 0; i < n; i++) 
        {
            a[i] = s.nextInt();
        }
        System.out.print("Enter the element you want to delete:");
        x = s.nextInt();
        for (int i = 0; i < n; i++) 
        {
            if(a[i] == x)
            {
                flag =1;
                loc = i;
                break;
            }
            else
            {
                flag = 0;
            }
        }
        if(flag == 1)
        {
            for(int i = loc+1; i < n; i++)
            {
                a[i-1] = a[i];
            }
            System.out.print("After Deleting:");
            for (int i = 0; i < n-2; i++) 
            {
                System.out.print(a[i]+",");
            }
            System.out.print(a[n-2]);
        }
        else
        {
            System.out.println("Element not found");
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: Explain try & catch finally block in Java 
Java :: arraylist in java 
Java :: java add element to map 
Java :: java run jnlp 
Java :: math.sin in java 
Java :: 9999999999999 
Java :: java csv compare 
Java :: update in spring boot 
Java :: jpql spring boot 
Java :: array copy java 
Java :: creating jdbc connection in java using service name oracle 
Java :: Java if...else 
Java :: UPLOAD TEXTFILE USING CYPERTEXT USING JAVA 
Java :: java get number at the end of string 
Java :: android studio space 
Java :: firebase realtime database get key 
Java :: how to find mongo java driver version 
Java :: qr code spring boot 
Java :: how to get ascii value of string letter in java 
Java :: Java Remove ArrayList Elements 
Java :: java list change element position 
Java :: string into ascii in java 
Java :: java for loop syntax 
Java :: comments java 
Java :: what is method in java 
Java :: class property java 
Java :: private method in interface java 
Java :: java array 
Java :: calling a void method java 
Java :: fragment to activity typecasting 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =