100% found this document useful (1 vote)
457 views5 pages

Java Array Deletion Techniques

This document contains Java code examples for deleting elements from arrays at different positions. It includes code to delete an element from a specific position in the array, delete an element from the front of the array, and delete an element from the end of the array. The code takes user input for the array size and elements, displays the original array, performs the deletion, and displays the modified array. It was prepared by three teaching assistants for a data structures and algorithms lab session.

Uploaded by

Rohini Aravindan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
457 views5 pages

Java Array Deletion Techniques

This document contains Java code examples for deleting elements from arrays at different positions. It includes code to delete an element from a specific position in the array, delete an element from the front of the array, and delete an element from the end of the array. The code takes user input for the array size and elements, displays the original array, performs the deletion, and displays the modified array. It was prepared by three teaching assistants for a data structures and algorithms lab session.

Uploaded by

Rohini Aravindan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PRESIDENCY UNIVERSITY

Bengaluru, Karnataka
Computer Science & Engineering
School of Computer Science & Engineering

Subject: CSE2001 - Data Structures & Algorithms Semester: III


Lab Session 2: Date: 11/09/2023
1. Java Code to delete element from a specific position (Array Operations in Data Structure)
import java.util.*;
public class delete_array
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n,i,pos,value;
int a[]=new int[10];
System.out.println("Enter the size of array");
n=sc.nextInt();
System.out.println("Enter the array elements");
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println("The array elements are");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
System.out.println("Enter the position to be deleted");
pos=sc.nextInt();
for(i=pos-1;i<=n-1;i++)
{
a[i]=a[i+1];
}
Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 1
n--;
System.out.println("The array elements after deletion");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}

}
}
Output

Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 2
2. Java Code to delete element at front (Array Operations in Data Structure)
import java.util.*;
public class delete_front
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n,i;
int a[]=new int[10];
System.out.println("Enter the size of array");
n=sc.nextInt();
System.out.println("Enter the array elements");
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println("The array elements are");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}

for(i=0;i<=n-1;i++)
{
a[i]=a[i+1];
}

n--;
System.out.println("The array elements after deletion");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 3
}
}
Output:

3. Java Code to delete element at front (Array Operations in Data Structure)


import java.util.*;
public class delete_end
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n,i;
int a[]=new int[10];
System.out.println("Enter the size of array");
n=sc.nextInt();
System.out.println("Enter the array elements");
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println("The array elements are");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}

n--;
System.out.println("The array elements after deletion");
for(i=0;i<n;i++)
{
Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 4
System.out.println(a[i]);
}

}
}

Output:

Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 5

You might also like