Insert a element in array
Insert a element in array
this code is performe to Insert a element in Array using Java programming Language.
Array is a collection of same type data which is store multiple element in a single variable.
structure :
data type variable name [] = {elements with saprate incommas};
Example : int array[] = {2,3,4,5};
size = array.length;
here code to Insert a element in given data:
import java.util.Scanner;class HelloWorld { public static void main(String[] args) { int arr[] = {2,3,4,5}; int n = arr.length; int insarr[] = new int[n+1]; int add = 20;
for(int i = 0 ;i< arr.length; i++){ insarr[i] = arr[i]; insarr[n] = add; } for(int i = 0 ; i<insarr.length; i++){ System.out.println(insarr[i]); } }}
Output:
234520
thanks.
import java.util.Scanner;
class HelloWorld {
public static void main(String[] args) {
int arr[] = {2,3,4,5};
int n = arr.length;
int insarr[] = new int[n+1];
int add = 20;
for(int i = 0 ;i< arr.length; i++){
insarr[i] = arr[i];
insarr[n] = add;
}
for(int i = 0 ; i<insarr.length; i++){
System.out.println(insarr[i]);
}
}
}
Output:
2
3
4
5
20
thanks.
No comments: