Single array tutorial inserting a new elements of index position


Within this new blog post, I wanna show you the other forms of implementing a single array in
java, plus you will learn how to insert a new element in the index position, also in this context, I used buffered reader along with my codes, so I hope you take a time to test the codes and see the awesome result, then try to experiments it with your own desire... thanks for taking the time to visit my blog
have a nice coding...
import java.util.Arrays;
import java.io.*; public class Array2{
public static void main (String args[])throws IOException{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the size of index:");
int n = Integer.parseInt(input.readLine());
int[] my_array = new int[n];

for(int i =0; i<n; i++){
System.out.println("Enter the Element of array:");
my_array[i] = Integer.parseInt(input.readLine());
}

System.out.println("Enter the position to be inserted:");
int Index_position = Integer.parseInt(input.readLine());

System.out.println("Original Array:"+Arrays.toString(my_array));
for(int i=my_array.length -1;i>Index_position;i--){
my_array[i]=my_array[i-1];
}

//int Index_position=6;
System.out.println("Enter the element to be inserted:");
int newValue = Integer.parseInt(input.readLine());

my_array[Index_position]= newValue;
System.out.println("new Array:"+Arrays.toString(my_array));
}
}

Post a Comment

0 Comments