Results 1 to 3 of 3
  1. #1
    Jrr
    Jrr is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

    Default adding a variable in order to a list

    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:

    Java Code:
    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
    Java Code:
    // ****************************************************************
    // 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 ++;	
    }
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    The Random instance works better if instantiated without the seed argument.
    Java Code:
    import java.util.Random;
    
    public class IntegerListRx
    {
        int[] list; //values in the list
        int arraySize = 0;
        Random rg = new Random();
    
        //-------------------------------------------------------
        //create a list of the given size
        //-------------------------------------------------------
        public IntegerListRx(int size)
        {
            list = new int[size];
            arraySize = size;
        }
    
        //-------------------------------------------------------
        //fill array with integers between 1 and 100, inclusive
        //-------------------------------------------------------
        public void randomize()
        {
            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)
        {
            if (arraySize == list.length)
            {
                increaseSize();
            }
            // Assume the list is sorted from low to high.
            // Find the index of the last element that has
            // a lower value than newVal.
            int insertionIndex = arraySize;
            for(int j = 0; j < arraySize; j++)
            {
                if(list[j] > newVal)
                {
                    insertionIndex = j;
                    break;
                }
            }
            // Slide everything back after our insertionIndex.
            for(int j = arraySize; j > insertionIndex; j--)
            {
                list[j] = list[j-1];
            }
            list[insertionIndex] = newVal;
    
            arraySize ++;	
        }
    
        public static void main(String[] args)
        {
            // Exercise time.
            IntegerListRx test = new IntegerListRx(10);
            System.out.println("------randomize------");
            test.randomize();
            test.print();
            System.out.println("------sort------");
            test.selectionSort();
            test.print();
            System.out.println("------addInOrder------");
            // Try early, middle and late insertions.
            test.addInOrder(5);
            test.addInOrder(65);
            test.addInOrder(97);
            test.print();
        }
    }

  3. #3
    Jrr
    Jrr is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 03-12-2008, 03:23 PM
  2. Problem with applying Sql order by to html table
    By sireesha264 in forum Advanced Java
    Replies: 2
    Last Post: 02-04-2008, 11:20 AM
  3. Adding taglibs in JSP
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-14-2008, 12:43 AM
  4. Adding List to MIDlet Display
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-23-2007, 03:07 PM
  5. Adding a Restock Fee
    By Nexcompac in forum New To Java
    Replies: 2
    Last Post: 07-31-2007, 03:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •