Skip to content

Commit

Permalink
solved issue QMiNt#2 CURD operations on Array JAVA
Browse files Browse the repository at this point in the history
  • Loading branch information
Pracs2023 committed Oct 23, 2022
1 parent c3b5c9c commit dbab33a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
Binary file added CURD.class
Binary file not shown.
62 changes: 62 additions & 0 deletions CURD.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import java.util.*;
public class CURD
{
int ar[],m;
CURD(int a)
{
m=a;
ar=new int[m];
}
void CreateArray()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter elements of array");
for (int i=0;i<m;i++)
ar[i]=sc.nextInt();
}
void PrintArray()
{
for (int i=0;i<m;i++)
System.out.print(ar[i]+" ");
}
void UpdateElement(int newElement,int pos)
{
if(ar==null || pos>=m || pos<0){
System.out.println("Out of bound index");
return;
}
for (int i=0;i<m;i++)
{
if(pos==i+1)
ar[i]=newElement;
}
}
void DeleteElement(int pos)
{
if(ar==null || pos>=m || pos<0){
System.out.println("Out of bound index");
return;
}
for(int i=pos;i<m-1;i++)
ar[i]=ar[i+1];
m--;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter length of array");
int m=sc.nextInt();
CURD obj=new CURD(m);
obj.CreateArray();
obj.PrintArray();
System.out.println("Enter element to be updated also the position in which it is to be updated");
int Element=sc.nextInt();
int pos=sc.nextInt();
obj.UpdateElement(Element,pos);
obj.PrintArray();
System.out.println("Enter index to be deleted");
int index=sc.nextInt();
obj.DeleteElement(index);
obj.PrintArray();
}
}

0 comments on commit dbab33a

Please sign in to comment.