0

I need to create a method that inserts an integer into a sorted array of integers. I had already done this method but not with integers but with strings. Now I have a problem with integers.

My method with array of strings is:

    public int inserisci(String s) throws IllegalStateException {
    if(numElementi == elementi.length)
        throw new IllegalStateException("Full array.");     
    int pos = ricercaBinaria(s);
    if(pos > -1) 
        return -1;
    if(elementi[-(pos + 1)] != null) {
           for(int i = numElementi; i >= -(pos + 1); i--)
            elementi[i + 1] = elementi[i];
    }
    elementi[-(pos + 1)] = s;
    numElementi++;
    return -(pos + 1);
}

For the array of strings I wrote this one:

    public int insert(int x) throws IllegalStateException {
    if(numElements == elements.length)
        throw new IllegalStateException("Full array.");     
    int pos = binarySearch(x);
    if(pos > -1) 
        return -1;
    if(**elements[-(pos + 1)] != null**) {
           for(int i = numElements; i >= -(pos + 1); i--)
              elements[i + 1] = elements[i];
    }
    elements[-(pos + 1)] = x;
    numElements++;
    return -(pos + 1);
}

But the bold part (elements[-(pos + 1)] != null) is not correct. How can I replace it? Thanks

1
  • Either switch to Integer or initialize the array with a value you know is outside the range you're going to use. Commented Apr 12, 2014 at 23:23

3 Answers 3

1
  1. if you use java's build in List and collections sorting it will be must faster than your current method
  2. the syntax like -(pos + 1) it very bad for readability and sort of backwards way of re-arranging your arrays, if you are going to use this method then find the location to insert, say index 'i' which is currently in use - them move all the other elements after and equal to i up one before insert i. (I assume this must be personal development or school assignment since you wouldn't want to do this logic on company java code)
Sign up to request clarification or add additional context in comments.

1 Comment

try to keep your loops here going from smaller to larger numbers and add debug statements to help you trace what is happening to the variables during the lines of code, even simple System.out.println's will work for understanding the flow - most programming issues become much easier once you actually see what is happening during debug statements
0

Try this one. a new value one is inserted at index two using ArrayUtils#add(array,index,value) method.

Note: If you are in learning stage then I suggest you, don't use it at this time.

int[] arr = { 0, 2, 3, 5, 6, 7 };

System.out.println("Previous size:" + arr.length);
arr = org.apache.commons.lang.ArrayUtils.add(arr, 2, 1);
System.out.println("New Size:" + arr.length);
System.out.println("inserted value:" + arr[2]);

output:

Previous size:6
New Size:7
inserted value:1

Comments

0

Just use Java's List class as part of the Collections framework. Its much more efficient, reliable, and usable than making your own sort of implementation of a sorted list like you have here.

To add all your data into a list if it was part of an array: java.util.Array#AsList(T[] t) (returns a List if type T is int) To sort this array, because int is a primitive type its sorted using < and > java.util.Collections#sort(yourList)

To learn about all the methods that List has or can be used on a List, just read those 2 links.

Comments

Your Answer

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