java - Sorting a integer list without a sort command -


so have code:

public class sortedintlist extends intlist {        private int[] newlist;      public sortedintlist(int size)      {         super(size);         newlist = new int[size];     }      public void add(int value)     {         for(int = 0; < list.length; i++)         {             int count = 0,                 current = list[i];              if(current < value)             {                 newlist[count] = current;                 count++;             }             else             {                 newlist[count] = value;                 count++;             }         }     } } 

yet, when run test, nothing prints out. have system.out.print in class in same source.

where going wrong?

edit: print code comment:

 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);        }  } 

edit2: superclass comment below

 public class intlist   {        protected int[] list;        protected int numelements = 0;         public intlist(int size)        {             list = new int[size];        }        public void add(int value)        {            if (numelements == list.length)                  system.out.println("can't add, list full");             else {                  list[numelements] = value; numelements++;            }       }        public string tostring()        {             string returnstring = "";            (int i=0; i<numelements; i++)                  returnstring += + ": " + list[i] + "\n";             return returnstring;       }  } 

let's walk through logic of how want work here:

first make new sorted list passing 10 constructor, make integer array of size 10.

now call add method passing 100 it. method sets position 0 100

now add 50, method sets 50 in position 0 , 100 in position 1

now add 200, gets placed @ position 2

and add 25. gets set position 0, , else gets shuffled on down

then method print out in list.

so here problems:

for first add, compare current, initialized @ 0, 50. 0 less 50, 50 never gets set array. true elements.

edit: seeing super class how should fix code:

public class sortedintlist extends intlist {        private int[] newlist;     private int listsize;      public sortedintlist(int size)      {         super(size);         // removed newlist bit becuase superclass has list using         listsize = 0; // keeps track of number of elements in list     }      public void add(int value)     {         int placeholder;         if (listsize == 0)         {             list[0] = value; // sets first element eqal value             listsize++; // incriments size, since added value             return;   // breaks out of method         }         for(int = 0; < listsize; i++)         {             if (list[i] > value) // checks if current place greater value             {                 placeholder = list[i]; // these 3 lines swap value value in array, , sets comparison continue                 list[i] = value;                 value = placeholder;             }         }     list[i] = value; // done checking existing array, remaining value gets added end     listsize++;  // added element needs increase;     }      public string tostring()      {          string returnstring = "";         (int i=0; i<listsize; i++)              returnstring += + ": " + list[i] + "\n";          return returnstring;     } } 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -