I have an array where of integers that has to be sorted and inserted into the correct place in the array for each number, not just at the end. All of this is done in the add method. This is what i have so far.

package sortedintlist;

public class SortedIntList extends IntList{
	protected int num = 0;
	protected int place = 0;
	public SortedIntList(int size)
	{
		super(size);
	}

	
	public void add(int value)
	{
		if (numElements == list.length)
		    System.out.println("Can't add, list is full");
		else
		{
			
			int i = 1;
			
			while (i >= 0 && num < value)
			{
				
				i++;
			}
			
			while (numElements > i)
			{
				
			
			list[i] = list[i-1];
			i--;
			}
			
			
			
			
	    }
		
    	
    
    

	}
	
	
}

This is the list of numbers:

public class ListTest
{
 public static void main(String[] args)
 {
	SortedIntList myList = new SortedIntList(10);
	myList.add(100);
	myList.add(50);
	myList.add(200);
	myList.add(25);
	System.out.println(myList);
	
	
	
 }
}

This was the original IntList that contains the original Add method:

public class IntList
{

 protected int[] list;
 protected int numElements = 0;

 //------------------------------------------------------------- 
 // Constructor -- creates an integer list of a given size.
 //------------------------------------------------------------- 
 public IntList(int size)
 {
	list = new int[size];
 }

 //------------------------------------------------------------- 
 // Adds an integer to the list.  If the list is full,
 // prints a message and does nothing.
 //------------------------------------------------------------- 
 public void add(int value)
 {
	if (numElements == list.length)
	    System.out.println("Can't add, list is full");
	else
	    {
		list[numElements] = value;
		numElements++;
	    }
 }

 //------------------------------------------------------------- 
 // Returns a string containing the elements of the list with their
 // indices.
 //------------------------------------------------------------- 
 public String toString()
 {
	String returnString = "";
	for (int i=0; i<numElements; i++)
	    returnString += i + ": " + list[i] + "\n";
	return returnString;
 }
}

Edited 4 Years Ago by steven8579: n/a

Do you have any questions or problems? Please ask.

Post the full text of the error messages you get.
Post the output from the program showing what it outputs.

In SortedIntList I have to override the Add method in IntList. SortedIntList should sort the numbers as they are added to the array. not at the end. My problem is that I can't get it to sort. This code gives me an array out of bounds exception. Here is the error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2147483647
at sortedintlist.SortedIntList.add(SortedIntList.java:31)
at sortedintlist.ListTest.main(ListTest.java:16)

any help would be appreciated

Thank You
Steven

Edited 4 Years Ago by steven8579: n/a

That index is a long way past the end of the array.
How did you get such a large index?It looks like the max possible value that an int can hold.

Look at the code on line 31 in SortedIntList.java and see what index is used and how it can get such a large value.

Add some printlns to show the value of the index as it is used to see how its value changes.

Edited 4 Years Ago by NormR1: n/a

In SortedIntList I have to override the Add method in IntList. SortedIntList should sort the numbers as they are added to the array. not at the end. My problem is that I can't get it to sort. This code gives me an array out of bounds exception. Here is the error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2147483647
at sortedintlist.SortedIntList.add(SortedIntList.java:31)
at sortedintlist.ListTest.main(ListTest.java:16)

any help would be appreciated

Thank You
Steven

see this:http://stackoverflow.com/questions/5554734/java-lang-arrayindexoutofboundsexception

ok i tried some debugging techniques but now i run into an infinite loop. i feel like im getting closer but it still doesn't sort. does anyone have any ideas?

package sortedintlist;

public class SortedIntList extends IntList{
	protected int num = 0;
	protected int place = 0;
	public SortedIntList(int size)
	{
		super(size);
	}

	
	public void add(int value)
	{
		if (numElements == list.length)
		    System.out.println("Can't add, list is full");
		else
		{
			
			int i = 0;
			
			
			while (i >= 0 && i < numElements)
			{
				
				i++;
			}
			
			while (i >= 0)
			{
				if(i == 0)
				{
					list[i] = value;
				}
				else
				{
					list[i] = list[i-1];
					list[i] = value;
					
					i--;
				}
				
				
				
				
			
			
			}
			
			
			
			
	    }
		
    	
    
    

	}
	
	
}

Edited 4 Years Ago by steven8579: n/a

while (i >= 0)
			{
				if(i == 0)
				{
					list[i] = value;
				}
				else
				{
					list[i] = list[i-1];
					list[i] = value;
 
					i--;
				}
                         }

once i becomes 0 at this loop its value will remain 0 because it will always pass the if statement which doesn't change the value of i

Edited 4 Years Ago by zeroliken: n/a

i get what you're saying. This is what i'm trying to do. The first while loop should read all nums in the array from the left to the right and stop where the element is supposed to be inserted. The condition should be that index < numElements and number is smaller than values. The second loop should start at num elements. and do list = list until the place where it has to insert the value. Then the new number has to be put in. I cant seem to get my loops to do that. The loops aren't nested. I cant seen to figure out how to do this. Any help would be greatly appreciated.

Have you coded what you just described as what you want the code to do?

do list = list until the place where it has to insert the value

Can you describe in English what you want that code to do?
How does the value of i change?

i get what you're saying. This is what i'm trying to do. The first while loop should read all nums in the array from the left to the right and stop where the element is supposed to be inserted. The condition should be that index < numElements and number is smaller than values. The second loop should start at num elements. and do list = list until the place where it has to insert the value. Then the new number has to be put in. I cant seem to get my loops to do that. The loops aren't nested. I cant seen to figure out how to do this. Any help would be greatly appreciated.

Just try to do this one step at at a time - so first just write the code to "read all nums in the array from the left to the right and stop where the element is supposed to be inserted". In your code print the value to insert, the values in the array and the value of the index variable. Do this both inside the loop and after the loop so you can see exactly what is happening. Maybe it won't work first time, but with those printouts you should be able to see what's going wrong and fix it fairly quickly.

When, and only when that is working, use exactly the same technique to add the second loop and debug it.

Edited 4 Years Ago by JamesCherrill: n/a

This article has been dead for over six months. Start a new discussion instead.