//Array Is a Basic Data Structure in Computer Programming. Array Have Continues Memory Allocation
in Memory Space. Data Are Stored In Continues memory space . We Access Using the index Of The
Element. Index Start From 0 to n-1
//Basic Operations in Array
/*
- traverse
- search
- find
- insert
- Remove
*/
class array{
//Traverse A Array
public static void traverse(int[] arr){
int n=arr.length;
for(int i=0;i<n;i++){
System.out.println(arr[i]);
}
}
//Find Value By Index
public static void findByIndex(int[] arr,int index){
System.out.println(arr[index]);
}
//Find Index By Value
public static void findIndex(int[] arr,int value){
int n=arr.length;
for(int i=0;i<n;i++){
if(arr[i]==value){
System.out.println(i);
}
}
}
//Insert Function (Array Copy Method)
public static void insert(int[] arr,int index,int value){
int n=arr.length;
int[] brr=new int[n+1];
for(int i=0;i<index;i++){
brr[i]=arr[i];
}
int j=index;
while(j<n){
brr[j+1]=arr[j];
j++;
}
brr[index]=value;
traverse(brr);
}
//Remove Element By Index (Array Copy Method)
public static void removeByIndex(int[] arr,int index){
int n=arr.length;
int[] brr=new int[n-1];
for(int i=0;i<index;i++){
brr[i]=arr[i];
}
for(int i=index;i<n-1;i++){
brr[i]=arr[i+1];
}
traverse(brr);
}
public static void main(String[] args){
int[] arr={2,3,4,5,1};
//treaverse array
//traverse(arr);
//findByIndex(arr,3);
//insert(arr,2,5);
removeByIndex(arr,2);
}
}
Comments
Post a Comment