How to insert element (Specific position ) in Array : by Java2021 - Java2021 : Online Tutorials Hub

Java2021 : Online Tutorials Hub

JAVA | Advance Java | MySQL | Data Structure | Git | HTML | CSS | JS

Latest

Post Top Ad

Saturday, July 11, 2020

How to insert element (Specific position ) in Array : by Java2021

Simple way to learn | How to insert element (Specific position ) in Array.
  • Here is the program for you how to insert element in specific position in array only integer value
  • So we insert element in 10 in array and all the right side of array is shifted to +1 position and so on.(below image)
  • Array means similar data types.
Program 

public class InsertElement
{
    public static void main(String[] args) {
        int array[]={20,30,40,50,60};  //this element put in array
        int position=4;        //at position 4              
        int element=10;     //10 put in 4 position
        for (int i=array.length-1;i<position-1;i--)
        {
            array[i]=array[i-1];
        }
        array[position-1]=element;
        for (int i=0;i<array.length;i++)
        {
            System.out.print(array[i]+" ");
        }
    }
}
Output :  



Click to next or Start with your choice
Sorting and Searching Programs
Start with your Choice
-Important Programs for Freshers
Learn Pattern Programs 
Click to Learn MySQL 
for any complaint regarding my Blog please visit contact us page and write what problem you have!
Learn with us | HTML | CSS | Java Script | Bootstrap | JAVA | ADV JAVA | MySQL | GIT | Data Structure

No comments:

Pages