2:53 am - Tuesday April 23, 2019

Java Example Program for Delete element from array

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

/**
* @ Auther : Candidjava.com
* @ Description : To Delete Element from array.
*/

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class DeleteElement {
static int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
static int[] b = new int[10];
static int i, k, f, j = 0;
static int l = a.length;

void DeletElnt() {
for (i = 0; i < l; i++) {
if (i != k) {
if (i < k) {
b[i] = a[i];
} else {
b[i - 1] = a[i];
}
}
}
l = l - 1;
System.out.println("New Array ");
for (i = 0; i < l; i++) {
System.out.println(b[i]);
}
}

public static void main(String[] args) {
DeleteElement d = new DeleteElement();
System.out.println("Array Elements are ");
for (i = 0; i < l; i++)
System.out.println(a[i]);
InputStreamReader is = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(is);
System.out.println("Enter the Element for Delete");
try {
String s = br.readLine();
f = Integer.parseInt(s);
for (i = 0; i < l; i++) {
if (f == a[i]) {
System.out.println("DeleteElement found from given array");
k = i;
j++;
d.DeletElnt();
}
}
if (j == 0) {
System.out
.println("Entered Element does not found from given array");
}
} catch (IOException e) {
System.out.println(e);
}
}
}

OUTPUT :

Array Elements are
1
2
3
4
5
6
7
8
9
10
Enter the Element for Delete
5
DeleteElement found from given array
New Array
1
2
3
4
6
7
8
9
10

Filed in: Z-A All Java Codes

Comments are closed.