Basic array operation using java

Basic array operation using java

In array, we can perform the various operation.

So in this section, we learn about some basic operation that can be performed on the array.

so there is some following operation which we are applied on the array:

  • Insertion in an array
  • Deletion in the array
  • Searching in the array

now we discuss these all above-mentioned operation.

for performing this operation we must have the knowledge to declare, create and initialise the array.

 

array operation

Insertion in the array

To insert any element in an array in java programming, you have to ask the user to enter the array size and array element. Now ask the user to enter the element and position where he/she want to insert that element at the desired position as shown in the following program.

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int size,insert,poss;
//creation of the array
int[] arr = new int[50];
//declare scanner class
Scanner sc= new Scanner(System.in);
System.out.print("enter array size ");
size=sc.nextInt();
System.out.println("enter array elements");
for(int i=0;i<size;i++)
arr[i]=sc.nextInt();
System.out.println("the array before insertion");
for(int i=0;i<size;i++)
System.out.print(arr[i]+" ");
System.out.println();
System.out.print("enter element to be insert ");
insert=sc.nextInt();
System.out.print("enter the position ");
poss=sc.nextInt();
for(int i=size;i>poss-1;i--)
arr[i]=arr[i-1];
arr[poss-1]=insert;
System.out.println("element successfully inserted");
System.out.println("the array after insertion");
for(int i=0;i<=size;i++)
System.out.print(arr[i]+" ");

       }
}

Output

enter array size 4                                                                                                               
enter array elements                                                                                                             
12                                                                                                                               
55                                                                                                                               
3                                                                                                                                
8                                                                                                                                
the array before insertion                                                                                                       
12 55 3 8                                                                                                                        
enter element to be insert 2                                                                                                     
enter the position 4                                                                                                             
element successfully inserted                                                                                                    
the array after insertion                                                                                                        
12 55 3 2 8          

Deletion in the array

To delete an element from an array in Java programming, you have to first ask the user to enter the size and elements of the array, now ask to enter the element/number which is to be deleted. Now to delete that element from the array first you have to search that element to check whether that number is present in the array or not if found then place the next element after the founded element to the back until the last as shown in the following program.

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int size,delete,c=0;
int[] arr = new int[50];
//declare the scanner class
Scanner sc= new Scanner(System.in);
System.out.print("enter array size ");
size=sc.nextInt();
System.out.println("enter array elements");
for(int i=0;i<size;i++)
arr[i]=sc.nextInt();
System.out.println("the array before deletion");
for(int i=0;i<size;i++)
System.out.print(arr[i]+" ");
System.out.println();
System.out.print("enter element to be delete ");
delete=sc.nextInt();
for(int i=0;i<size;i++)
{
if(arr[i]==delete)
{
for(int j=i;j<size;j++)
arr[j]=arr[j+1];
c++;
break;
}
}
if(c==0)
{
System.out.print("Element Not Found");
}
else
{
System.out.println("element successfully deleted");
System.out.println("the array after deletion");
for(int i=0;i<size-1;i++)
System.out.print(arr[i]+" ");
}
}
}

Output

enter array size 4                                                                                                              
enter array elements                                                                                                             
12                                                                                                                               
44                                                                                                                               
2                                                                                                                                
6                                                                                                                                
the array before deletion                                                                                                        
12 44 2 6                                                                                                                        
enter element to be delete 2                                                                                                     
element successfully deleted                                                                                                     
the array after deletion                                                                                                         
12 44 6        

Searching in the array

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int size,search,c=0,pos=0;
int[] arr = new int[50];
Scanner sc= new Scanner(System.in);
System.out.print("enter array size ");
size=sc.nextInt();
System.out.println("enter array elements");
for(int i=0;i<size;i++)
arr[i]=sc.nextInt();
System.out.println("the array is");
for(int i=0;i<size;i++)
System.out.print(arr[i]+" ");
System.out.println();
System.out.print("enter element to be search ");
search=sc.nextInt();
for(int i=0;i<size;i++)
{
if(arr[i]==search)
{
c++;
pos=i+1;
break;
}
}
if(c==1)
System.out.println(search+" is found at the possition "+pos);
else
System.out.println("not found");
}
}

Output

enter array size 4                                                                                                               
enter array elements                                                                                                             
12                                                                                                                               
33                                                                                                                               
2                                                                                                                                
8                                                                                                                                
the array is                                                                                                                     
12 33 2 8                                                                                                                        
enter element to be search 2                                                                                                     
2 is found at the possition 3