0

I have an array of objects and i want to add elements in this array and simultaneously sort them in ascending order.Although I tried many compinations , I always take a java.lang.ArrayIndexOutOfBoundsException. Here is a part of my code :

public boolean insert(Person p)
{
    for(int i=0;i<=size();i++)
    {   
        if (c==0)
        {   
            array[0] = p;
            c++;
            return true;
        }
        else
        {   
            if (p.compareTo(array[i])==-1)
            {
                array[i]=p;
                c++;
                for(int j = size(); j>i; j--)
                {
                    array[j]=array[j-1];
                }
            }
            else if((p.compareTo(array[i])==1))
            {
                array[i+1]=p;
                c++;
                for (int j=(size()-1);j>= i+1; j--)
                {
                    array[j+1]=array[j];
                }
            }
            else
            {
                return false;
            }
            return true;
        }
    }
    return false;
}

private int c;
private Person array[];
public SortedPersonList()
{   
    this.array = new Person[c];
}
public int size()
    {
        return c;
    }
10
  • 2
    Use a TreeSet<Person>. It will do that for you, and do it much faster. Commented Apr 13, 2014 at 13:05
  • if size() == array.length, than you are accessing array[array.length], which is illegal. Commented Apr 13, 2014 at 13:08
  • 1
    Library solutions - best solutions! Specially for more or less common things. Though using a TreeSet is slightly misguided, since the data structure at hand is an array and allows multiple equal objects Commented Apr 13, 2014 at 13:09
  • size() actually is c. Commented Apr 13, 2014 at 13:10
  • @DiVeRsi0n: And how do you initialize the array? It probably needs to be resized, otherwise the capacity is bounded. Commented Apr 13, 2014 at 13:11

3 Answers 3

1

Remove the equal sign in for(int i=0;i<=size();i++). That is, change to

for(int i=0;i<size();i++)

Array indices go from 0 to size-1. So array[size()] is outside the array. Hence the error.

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

Comments

0

1) You are initializing your array to a size 0, and you never resize it. An array has a fixed size, so in order to populate it with items beyond its range, you should first create a larger array and copy the contents to it (This is how ArrayList works).

2) When you find the insertion point, you shouldn't override this position before keeping its value in a temp variable. (Also, I don't understand the third case in your code. Why do you insert if p is greater than array[i]? you should rethink your logic.)

3) you may want to use binary search to find the insertion point. It has a better performance than linear search.

1 Comment

I must insert every time the main method calls the insert(), apart from the case that already a same object already exists in the array.
0

Your code :

    for(int i=0;i<=size();i++)

Write like

    for(int i=0;i<size();i++)  or  for(int i=0;i<=size()-1;i++)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.