I have to write a method for a program that will add a variable to a list, in order. Here's a description:
Add a method void addInOrder(int newVal) to the IntegerList class that assumes that the list is sorted in increasing order and adds the given element in its correct (sorted) position. So if the list contained the values 10 20 30 40 50 and you added 25, the new list would be 10 20 25 30 40 50. Don't just stick the value on the end and then sort -- sorting is expensive! Instead, look through the list, figure out where the new value should go and put it there, moving everything after it down to make room for it. As for addElement, you may need to increase the size of the array (determine this and do it first, before inserting the new element, if necessary).
The main program consists of a menu, and the user inputs a number to choose what they want the program to do. This particular method is seen in the main program here:
case 7:
System.out.print ("Enter the value to add: ");
int value3 = scan.nextInt();
list.addInOrder(value3);
break;
The method is what I've been having trouble with, I've been trying to make a temporary list for all the numbers above the inputed variable, and then inserting the variable, and putting the numbers save in the temporary array back onto the end of the list.
Heres my code for the entire Integer List class, its the last method that I can get to work.
Does anyone see something wrong with it? I can not get it to work
// ****************************************************************
// IntegerList.java
//
// Define an IntegerList class with methods to create, fill,
// sort, and search in a list of integers.
//
// ****************************************************************
import java.util.Random;
public class IntegerList
{
int[] list; //values in the list
int arraySize = 0;
//-------------------------------------------------------
//create a list of the given size
//-------------------------------------------------------
public IntegerList(int size)
{
list = new int[size];
arraySize = size;
}
//-------------------------------------------------------
//fill array with integers between 1 and 100, inclusive
//-------------------------------------------------------
public void randomize()
{
Random rg = new Random (100);
for (int i=0; i<list.length; i++)
list[i] = rg.nextInt(100) + 1;
}
//-------------------------------------------------------
//print array elements with indices
//-------------------------------------------------------
public void print()
{
for (int i=0; i<arraySize; i++)
System.out.println(i + ":\t" + list[i]);
}
//-------------------------------------------------------
//return the index of the first occurrence of target in the list.
//return -1 if target does not appear in the list
//-------------------------------------------------------
public int search(int target)
{
int location = -1;
for (int i=0; i<list.length && location == -1; i++)
if (list[i] == target)
location = i;
return location;
}
//-------------------------------------------------------
//sort the list into ascending order using the selection sort
//algorithm
//-------------------------------------------------------
public void selectionSort()
{
int minIndex;
for (int i=0; i < list.length-1; i++)
{
//find smallest element in list starting at location i
minIndex = i;
for (int j = i+1; j < list.length; j++)
if (list[j] < list[minIndex])
minIndex = j;
//swap list[i] with smallest element
int temp = list[i];
list[i] = list[minIndex];
list[minIndex] = temp;
}
}
public void increaseSize()
{
int[] temp = new int[list.length *2];
for (int i =0; i< list.length; i++)
{
temp [i] = list[i];
}
list = temp;
}
public void addElement (int newVal)
{
if (arraySize == list.length)
{
increaseSize();
}
list[arraySize] = newVal;
arraySize++;
}
public void removeAll (int newVal)
{
for (int i=0; i<list.length; i++)
{
if (list[i] == newVal)
{
for (int z=i; z<list.length-1; z++)
list[z] = list[ z + 1 ];
arraySize--;
}
}
}
public void addInOrder(int newVal)
{
int[] temp = new int [5];
if (arraySize == list.length)
{
increaseSize();
}
boolean go = false;
for (int i=0; i < arraySize; i++)
{
if (list[i] > newVal)
{
while (go = false)
{
int x=0;
for (int z=i; z<arraySize; z++)
{
temp[x] = list[z];
x++;
}
list [i] = newVal;
System.out.println ("Fuck");
int y=0;
for (int z=i+1; z<arraySize; z++)
{
list[z] = temp[y];
y++;
}
go = true;
}
}
}
arraySize ++;
}
}