user3425699
user3425699

Reputation:

how to insert an integer element into sorted array

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

Upvotes: 0

Views: 956

Answers (3)

DirkyJerky
DirkyJerky

Reputation: 1168

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.

Upvotes: 0

Braj
Braj

Reputation: 46861

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

Upvotes: 0

  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)

Upvotes: 1

Related Questions