0

i am trying to get an executable piece of code from this piece of pseudo code my teacher had provided in class, so that i can see step by step how it works, but due to my limited knowledge in java (less than a week), i am not able to convert this to executable codes from the given structure, please help.

below is the pseudo code: my goal is to add 6 to the 4th position in this array: arr=[1,3,5,7,8]

public class Class3 {
    public static int[] insert(int[]x,int item,int position){
        int[] newX= new int[x.length+1];
        position =Math.min(x.length, position);
        for (int i=0;i<position;i++){
            newX[i]=x[i];
        }
        newX[position]=item;
        for (int indexinOld)=position.indexinOld<x.length;indexinOld++){
            newX[indexinOld+1]=x[indexinOld]
        }
  • 1
    I suggest to google it first. You will find many examples. – xingbin May 06 '18 at 10:09
  • @TimBiegeleisen That's wrong... The 4th position in this array is at `arr[3]`... – 0x1C1B May 06 '18 at 10:10
  • When the task is to insert a new element to an array? Sure. BTW, the pseudo code is nearly working, return the new array and fix your second for-loop (just syntax errors). – sn42 May 06 '18 at 10:11
  • This might help. https://stackoverflow.com/questions/20341825/inserting-into-array-in-order – xingbin May 06 '18 at 10:11
  • Possible duplicate of [How to add an element to Array and shift indexes?](https://stackoverflow.com/questions/11638123/how-to-add-an-element-to-array-and-shift-indexes) – sɐunıɔןɐqɐp May 06 '18 at 10:37

2 Answers2

0

The easiest way would be to use an ArrayList like this...

public static int [] insert(int[]x,int item,int position)
{
    ArrayList <Integer> list = new ArrayList <> ( Arrays.asList(x) );

    list.add(position, item);

    return ArrayUtils.toPrimitive(list.toArray(new Integer [0]));
}

However, because you are new in java I don't suggest this solution. Alternate here is a solution without ArrayList...

public static int[] insert(int [] oldArray, int value, int insertIndex)
{
    int [] newArray = new int[oldArray.length + 1];

    for(int index = 0; index < newArray.length - 1; ++index)
    {
        newArray[index < insertIndex ? index : index + 1] = oldArray[index];
    }

    newArray[insertIndex] = value;

    return newArray;
}

Some explanations: First you create a second array with one more element than the original element. Second, you initialize all slots with the values from the original array. If the counter is equal to the position where the value should be inserted, this algorithm skip this slot. After finishing the loop you initialize the slot at your insert position with the given value.

0x1C1B
  • 1,204
  • 11
  • 40
0

Hi as suggested in the comments you just need to return the newX as your new array. The problem from your teacher needs three steps to solve it:

  1. Create a new Array with a length that can hold the added elements
  2. add all old elements until the position in which you want to add the new element from the old array into the new array
  3. Add your element to it and fill up the new array with the remaining elements from the old array.

    public static int[] insert(int[]x,int item,int position){
        int[] newX= new int[x.length+1];
        position =Math.min(x.length, position);
        for (int i=0;i<position;i++){
            newX[i]=x[i];
        }
        newX[position]=item;
        for (int indexinOld=position+1;indexinOld<x.length+1;indexinOld++){
            newX[indexinOld]=x[indexinOld-1];
        }
        return newX;
    }
    

This is the working function you wanted to have. I just edited your second for loop and returned the newX-Array as the array.

For the other readers, there is a much simpler way without using for-loops for this (and not using ArrayLists)

I used this:

public static int[] insert(int[]x,int item,int position){
    int[] newX= new int[x.length+1];
    position =Math.min(x.length, position);
    System.arraycopy(x, 0, newX, 0, position);
    newX[position]=item;
    System.arraycopy(x, position, newX, position+1, x.length-position);
    return newX;
}
Scorix
  • 487
  • 6
  • 20