-3

When I print this code what I get on the terminal is {3,4,4,20}. I want to insert an index at a position and shift the array to the right.When I was shifting the elements in the array it dropped 10. How do I fix this so what prints on the terminal is {3,4,10,20}. Thanks!

    public static void main(String[] args) {

    practice obj = new practice(5);
    obj.insert(3);
    obj.insert(10);
    obj.insert(20);
    obj.insertShift(4,1);
    obj.print();
  
public void insertShift(int index, int pos){
sum[counter++] = index;  
for(int i=counter-1; i> pos; i--){   
    sum[i]= sum[i-1];    
    sum[pos]= index;
}
}

   public class practice {
   private int[] sum;
  private int counter;

 public practice(int counter) {
sum = new int[counter];
}


 public void insert(int index) {
sum[counter] = index;
counter++;
if (counter == 0) {
    int[] newArray = new int[counter * 2];
    for (int i = 0; i < counter; i++) {
        sum[i] = newArray[i];
    }
   // sum = newArray;
   // System.out.println(counter);
}
}
public void insertAt(int index, int pos){
    sum[counter] = index;
    for(int i=pos; i<counter; i++){
       // sum[i] = sum[i+1];
        sum[pos]= index;
       // newArray[pos] = index;
    }

}

public void insertShift(int index, int pos){
sum[counter] = index;
for(int i=counter; i> pos; i--){
    sum[i]= sum[i-1];
    sum[pos]= index;
}


}
   public void print () {
    for (int i = 0; i < counter; i++) {
        System.out.println(sum[i]);
    }
 }
 }
  • In the `insert` method, since `counter` can never be negative, it means that right after `counter++` it will always be 1 or higher, never 0, so `if (counter == 0)` will *never* be true. – Andreas Nov 26 '20 at 22:06
  • 6
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas Nov 26 '20 at 22:07
  • @Andreas the problem is in the insertShift method. I want to add the number 4 at the position 1. so instead of 3,10,20. I want to print 3,4,10,20. when I run the code what get is 3,4,4,20. – Packman19567 Nov 26 '20 at 22:47
  • 1
    Then draw on a piece of paper, follow the logic of what your code is doing, so you'll learn where it is going wrong. – Andreas Nov 26 '20 at 22:52
  • Are you trying to insert an element or to replace an existing element? – NomadMaker Nov 26 '20 at 23:39

1 Answers1

3

To find the problem, draw on a piece of paper, and follow the logic of the code.

When insertShift(4,1) is called, the data is as follows:

sum = { 3, 10, 20, 0, 0 }
counter = 3
index = 4
pos = 1

which means:

┌────┬────┬────┬────┬────┐
│  3 │ 10 │ 20 │  0 │  0 │
└────┴────┴────┴────┴────┘
                  ↑
                  counter
sum[counter++] = index;              // sum[3] = 4
                                     // counter = 4
┌────┬────┬────┬────┬────┐
│  3 │ 10 │ 20 │  4 │  0 │           Why???
└────┴────┴────┴────┴────┘
                       ↑
                       counter
for(int i=counter-1; i> pos; i--){   // i = 3
    sum[i]= sum[i-1];                // sum[3] = sum[2]
┌────┬────┬────┬────┬────┐
│  3 │ 10 │ 20 │ 20 │  0 │
└────┴────┴────┴────┴────┘
    sum[pos]= index;                 // sum[1] = 4
┌────┬────┬────┬────┬────┐
│  3 │  4 │ 20 │ 20 │  0 │           Oops! We just lost the value 10
└────┴────┴────┴────┴────┘
for(int i=counter-1; i> pos; i--){   // i = 2
    sum[i]= sum[i-1];                // sum[2] = sum[1]
┌────┬────┬────┬────┬────┐
│  3 │  4 │  4 │ 20 │  0 │
└────┴────┴────┴────┴────┘
    sum[pos]= index;                 // sum[1] = 4
┌────┬────┬────┬────┬────┐
│  3 │  4 │  4 │ 20 │  0 │           Nothing changed, value was already 4
└────┴────┴────┴────┴────┘
for(int i=counter-1; i> pos; i--){   // i = 1, loop ends

Now ask yourself this: Why is value of parameter index being assigned more than once? When method ends, the value 4 should only be in one place in the array, so assigning the value multiple times seems ... excessive.

Andreas
  • 154,647
  • 11
  • 152
  • 247