pushing value at last in java | how to add element at the end of array in java
Categories: Java 9 Java 8(JDK1.8) Java Java Examples
public class r4r{
public static void main(String[] args) {
int[] a=new int[10];
for(int i=0;i<a.length;i++){
//a[i]=i+1;
System.out.print(a[i]+",");
}
System.out.println("\nAfter Pushing value at Last");
a=pushLast(a,20);
a=pushLast(a,21);
a=pushLast(a,22);
a=pushLast(a,23);
a=pushLast(a,24);
for(int i=0;i<a.length;i++){
//a[i]=i+1;
System.out.print(a[i]+",");
}
}
static int[] pushLast(int[] inArray,int value){
if(inArray.length==10){
inArray=shiftLeft(inArray,value);
}
return inArray;
}
static int[] shiftLeft(int[] shift,int v){
for(int i=0;i<shift.length-1;i++){
shift[i]=shift[i+1];
}
shift[shift.length-1]=v;
return shift;
}
}
Output:-
0,0,0,0,0,0,0,0,0,0,
After Pushing value at Last
0,0,0,0,0,20,21,2
2,23,24,