Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-19-2007, 01:20 AM
Jrr Jrr is offline
Member
 
Join Date: Nov 2007
Posts: 2
Jrr is on a distinguished road
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:

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
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 ++; } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-19-2007, 02:38 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
The Random instance works better if instantiated without the seed argument.
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(); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-19-2007, 03:10 AM
Jrr Jrr is offline
Member
 
Join Date: Nov 2007
Posts: 2
Jrr is on a distinguished road
ahh, I get it.

Thanks for the help
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hibernate order by query thru java solution urgently reqd altaf Database 0 03-12-2008 04:23 PM
Problem with applying Sql order by to html table sireesha264 Advanced Java 2 02-04-2008 12:20 PM
Adding taglibs in JSP Java Tip Java Tips 0 01-14-2008 01:43 AM
Adding List to MIDlet Display Java Tip Java Tips 0 11-23-2007 04:07 PM
Adding a Restock Fee Nexcompac New To Java 2 07-31-2007 04:46 PM


All times are GMT +3. The time now is 03:49 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org