0

I want to produce the code which will do the following for me:

  • insert an element into array
  • find the position of the element in the array

For some reason (I think this might be because of the insertionSort method), I am not getting the desired results. If I, for example, insert 4 elements to the array with a length 5 the last element inserted should be at index 3. However, at the moment the position that I got is 4, which is wrong.

My question is what I have to use to make this code works as it should, when I am trying to use Integer instead of int (to check if there is null at next position to stop the insertionSort from executing) I am getting a NullPointerException (I replace all int[] to Integer[] without changing anything else).

public class arrayImplementation implements programInterface
{
    int pointer = 0;
    int number = -1;
    static int[] theArray;

    public arrayImplementation(int size) {
        theArray = new int[size];
    }

    @Override
    public void insert(int key) {
        theArray[pointer] = key;
        if(pointer != 0){
        insertionSort(theArray);
        }
        pointer++;
    }

    @Override
    public int find(int key) {

        int low = 0;
        int high = theArray.length - 1;

        while (high >= low) {
            int mid = (low + high) / 2;
            if (theArray[mid] == key) {
                number = mid;
                return number;
            }
            if (theArray[mid] < key) {
                low = mid + 1;
            }
            if (theArray[mid] > key) {
                high = mid - 1;
            }
        }
        return number;
    }

    @Override
    public void delete(int key) {}

    public void insertionSort(int[] theArray) {
        for (int i = 1; i < theArray.length; i++) {
             int temp = theArray[i];
             int j = i;
            while (j > 0 && temp < theArray[j - 1]) {
                theArray[j] = theArray[j - 1];
                j = j - 1;
            }
            theArray[j] = temp;
        }
    }

    public static void main(String arg[]) {
        arrayImplementation arrImp = new arrayImplementation(5);
        arrImp.insert(1);
        arrImp.insert(2);
        arrImp.insert(3);
        arrImp.insert(7);
        System.out.println(arrImp.find(7));
        for(int i = 0;i<theArray.length;i++){
        System.out.println(theArray[i]+", ");
        }
    }
}
7
  • Why is theArray static? That's going to cause you some problems as soon as you have more than one instance of your class. Commented Nov 11, 2015 at 17:40
  • it is used only to print the content of an array in main method (just for testing) Commented Nov 11, 2015 at 17:42
  • You should replace that with arrImp.theArray so that it doesn't need to be static. Commented Nov 11, 2015 at 17:43
  • Ok thanks for advice Commented Nov 11, 2015 at 17:44
  • Some minor things classes and interfaces in Java are written in UpperCamelCase. I also suggest changing the name of the interface to "Programable", instead of having the "-Interface" part there. Commented Nov 11, 2015 at 17:47

1 Answer 1

0

"Inserting int into your array with sorting in the same time" causes losing some value. int arr[] = new int [5] means your array has "5" numbers which are all "0"({0,0,0,0,0}). If you sort them while adding number you will lose your some int value. You should sort your array after adding them all. When you run your own code, you will see that there is not "2" in your array. If you use Integer object instead of 'int', there will be null instead of '0'. I write my own sorting method which send null values at the end. Try code below.

import java.util.Arrays;

public class ArrayImplementation {
    
    int pointer = 0;
    static Integer[] theArray;

    public ArrayImplementation(int size) {
        theArray = new Integer[size];
    }
    
    public void insert(int i) {
        theArray[pointer++] = i;
    }
    
    public int find(int i) {
        int c = 0;
        for (int n : theArray) {
            if (n == i)
                return c;
            c++;
        } 
        return -1;
    }
    public void sort () {
    
        Arrays.sort(theArray, new Comparator<Integer>() {
            @Override
            public int compare(Integer x, Integer y)
            {
                if (x == null || y == null)
                    return 1; // sends null value at the end of the array.
                return x - y;
            }
        });
    }
    public static void main(String arg[]) {
        ArrayImplementation arrImp = new ArrayImplementation(5);
        arrImp.insert(1);
        arrImp.insert(2);
        arrImp.insert(3);
        arrImp.insert(7);
        arrImp.sort(); // you should sort at the end of insertion. 
        for (int i = 0; i < theArray.length; i++) {
            System.out.println(theArray[i] + ", ");
        }

        System.out.println("");
        System.out.println(arrImp.find(7));
    }
}

"7" stays at 3 in the array.

prints:

1, 
2, 
3, 
7,
null

3
Sign up to request clarification or add additional context in comments.

4 Comments

but there is the same issue there is 0 inside which wasn't inserted to the array so I should get 1,2,3,7 : 3 so I think I have to use Integer instead of int
ArrayImplementation arrImp = new ArrayImplementation(4); you should set size 4 instead of make it Integer. "Arrays.sort(theArray);" will throw nullpointer exception if there are empty(null) values.
the thing is that the code have to do it with array that is full as well as partially filled
@user3396201 I updated answer. Own sorting method will send "null" value at the end of the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.