Sunday 15 January 2012

java - A Sorted Integer List -



java - A Sorted Integer List -

so original code

// (unsorted) integer list class method add together // integer list , tostring method returns contents // of list indices. // // **************************************************************** public class intlist { private int[] list; private int numelements = 0; //------------------------------------------------------------- // constructor -- creates integer list of given size. //------------------------------------------------------------- public intlist(int size) { list = new int[size]; } //------------------------------------------------------------ // adds integer list. if list full, // prints message , nothing. //------------------------------------------------------------ public void add(int value) { if (numelements == list.length) { system.out.println("can't add, list full"); } else { list[numelements] = value; numelements++; } } //------------------------------------------------------------- // returns string containing elements of list // indices. //------------------------------------------------------------- public string tostring() { string returnstring = ""; (int = 0; < numelements; i++) { returnstring += + ": " + list[i] + "\n"; } homecoming returnstring; } }

and

// *************************************************************** // listtest.java // // simple test programme creates intlist, puts // ints in it, , prints list. // // *************************************************************** import java.util.scanner ; public class listtest { public static void main(string[] args) { scanner scan = new scanner(system.in); intlist mylist = new intlist(10); int count = 0; int num; while (count < 10) { system.out.println("please come in number, come in 0 quit:"); num = scan.nextint(); if (num != 0) { mylist.add(num); count++; } else { break; } } system.out.println(mylist); } }

i need alter add together method sort lowest highest. tried doing.

// (unsorted) integer list class method add together // integer list , tostring method returns contents // of list indices. // // **************************************************************** public class intlist { private int[] list; private int numelements = 0; //------------------------------------------------------------- // constructor -- creates integer list of given size. //------------------------------------------------------------- public intlist(int size) { list = new int[size]; } //------------------------------------------------------------ // adds integer list. if list full, // prints message , nothing. //------------------------------------------------------------ public void add(int value) { if (numelements == list.length) { system.out.println("can't add, list full"); } else { list[numelements] = value; numelements++; (int = 0; < list.length; i++) { if (list[i] > value) { (int j = list.length - 1; j > i; j--) { list[j] = list[j - 1]; list[i] = value; break; } } } (in = 0; < list.length; i++) { } } } //------------------------------------------------------------- // returns string containing elements of list // indices. //------------------------------------------------------------- public string tostring() { string returnstring = ""; (int = 0; < numelements; i++) { returnstring += + ": " + list[i] + "\n"; } homecoming returnstring; } }

the outcome wrong. 1 able steer me in right direction? can sort of see why have doesn't work, can't see plenty prepare it.

so realize not descriptive here first time. exception of add together method modifications code not doing. assignment touch add together method sort array print out smallest largest. beginners class , little no practice tools basic understandings of loops , arrays.

i tried redoing 1 time again , came this:

if(list[numelements-1] > value){ for(int i=0; i<numelements; i++){ if(list[i]>value){ for(int j = numelements; j>i; j-- ){ list[j] = list[j-1]; } list[i] = value; break; } } numelements++; } else { list[numelements] = value; numelements++; }

my input was:8,6,5,4,3,7,1,2,9,10 output was: 1,10,1,9,10,1,1,2,9,10

this thing kicking butt. understand want check input number array , move numbers higher 1 space , come in behind sorted on entry, doing proving hard me. apologize if code on here hard follow formatting little odd on here me , time allows me best. think break not breaking loop thought would. maybe problem.

the biggest bug see using list.length in for loop,

for(int = 0; <list.length; i++)

you have numelements. also, think it's i needs stop 1 before like,

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

and

for (int j = numelements; j > i; j--)

java

No comments:

Post a Comment