+ 1

Why my last element is not coming i.e. what will i do so my last element came??

As In my program i want to insert element in specific position https://code.sololearn.com/cg8J0FbtLj53/?ref=app [A^dr3w] Gaurav Agrawal ‎ ‏‏‎Sreejith 

12th Nov 2018, 2:02 PM
Harsh Agrawal
Harsh Agrawal - avatar
7 Antworten
+ 3
ok it won't work, i was working on c++ so got confused. for this purpose you have to do this or just create a loop for taking input. int a[] = new int[30]; a[0] = 7; a[1] = 8; a[2] = 6; a[3] = 9; or you can create a separate array for result
13th Nov 2018, 5:14 PM
‎ ‏‏‎Anonymous Guy
+ 19
//here is full surgery of your code , have a look & identify the mistakes public class Program{ public static void main(String[] args) { int a[]= {7,8,6,9},p= 2,num= 10,prev,i,x= a.length; //4 if(p<=x){ //execute once ... prev= x-1; //3 while(prev>=p) { //3>=2 , 2>=2 only work twice a[prev]= a[prev-1]; //shifting at arr[2] & arr[3] only prev= prev-1; } //prev = 1 after all this a[prev]= num; //prev = 1 , a[1]=10 now for(i=0;i<x;i++) System.out.print(a[i]+" "); //printing elements } } } //btw I am getting that U just want to rotate numbers around that array , btw take a pen-paper & make your strategy , will not take more than some seconds or minutes ... just try carefully ☺👍
12th Nov 2018, 2:15 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 11
HINT : try thinking like shifting from arr[0] to arr[1] , 1 to 2 , 2 to 3 ... but when comed to arr[3] do shifting arr[0] ... [ie make some if statement seeing that 3+1 = size of array(that is 4 here) ] //make use of some extra int variable to do this //OR entire new int array[that will be more easy & direct] ● take your time & give a try
12th Nov 2018, 2:28 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 3
‎ ‏‏‎Sreejith  Thnxzzz i got my mistake
14th Nov 2018, 9:12 AM
Harsh Agrawal
Harsh Agrawal - avatar
+ 2
Harsh Agrawal Array index out of bound, which means you are trying to access element out of the declared range of array. so, Increase the range of array int a[] = new int [30]; a= {7,8,6,9};
13th Nov 2018, 1:46 AM
‎ ‏‏‎Anonymous Guy
+ 2
‎ ‏‏‎Sreejith  but it show error can u give ur code so i cn understand properly?
13th Nov 2018, 9:02 AM
Harsh Agrawal
Harsh Agrawal - avatar
+ 1
Gaurav Agrawal ya dat i know whats my code u just give the dry run of my code that i dont want....I want to display my last element dat is not coming and if i want to increase the size of array i.e. x+= 1; then it show array index out of bounds so plzz give some suggestion about this
12th Nov 2018, 2:21 PM
Harsh Agrawal
Harsh Agrawal - avatar