arrays - IntegerList help in Java -


so in class follow lab manual instructions. able step 1 , two, need step three.


lab manuel instructions:

  1. add method void removefirst(int newval) integerlist class removes first occurrence of value list. if value not appear in list, should nothing (but it’s not error). removing item should not change size of array, note array values need remain contiguous, when remove value have shift after down fill space. remember decrement variable keeps track of number of elements.

add option menu in integerlisttest test new method.


integerlist

public class integerlist {     private int count;     private double totalint;     int[] list; //values in list     //-------------------------------------------------------     //create list of given size     //-------------------------------------------------------      void addelement(int newval)     {         if (count == list.length)             increasesize();          list[count] = newval;         count++;     }      void removefirst(int newval2)     {         (int = 0; < list.length-1; i++)         {             if (newval2 == list[i])             {                 list[list.length] =  (integer) null;                 list[i] = list [i-1];             }         }      }      public integerlist(int size)        {         list = new int[size];         count = 0;      }      public void randomize()     {         (int i=0; i<list.length; i++)             {             list[i] = (int)(math.random() * 100) + 1;             count++;             }      }             public void print()     {         (int i=0; i<count; i++)             system.out.println(i + ":\t" + list[i]);     }     private void increasesize() {     int[] temp = new int[list.length * 2];      (int lst = 0; lst < list.length; lst++)         temp[lst] = list[lst];      list = temp; } } 

integerlisttest

import java.util.scanner; public class integerlisttest {     static integerlist list = new integerlist(10);     static scanner scan = new scanner(system.in);      public static void main(string[] args)     {         printmenu();         int choice = scan.nextint();         while (choice != 0)         {             dispatch(choice);             printmenu();             choice = scan.nextint();         }     } 

public static void dispatch(int choice) {

int loc; switch(choice) { case 0:     system.out.println("bye! ") ;     break; case 1:     system.out.println("how big should list be?");     int size = scan.nextint();     list = new integerlist(size);     list.randomize();     break; case 2:     list.print();     break; case 3:     system.out.println("what number add?");     int newval = scan.nextint();     list.addelement(newval);     break; case 4:     system.out.println("what number want remove? (removes first occurance.)");     int newval2 = scan.nextint();     list.removefirst(newval2); default:     system.out.println("sorry, invalid choice"); } }   public static void printmenu() {     system.out.println("\n menu ");     system.out.println(" ====");     system.out.println("0: quit");     system.out.println("1: create new list (** first!! **)");     system.out.println("2: print list");     system.out.println("3: add list");     system.out.println("4: remove integer");     system.out.print("\nenter choice: ");         }     } 

any appreciated. cool if explain why can learn this. thanks! :d

so can loop array find first element matches given value. if found, save position index , shift elements after 1 position:

void removefirst(int newval2) {   int index = -1;   (int = 0; < count; i++) {      if (index == -1 && newval2 == list[i]) {        // element found - save index        index = i;      }      if (index != -1) {         // code handles after found case         if (i == count-1) {           // zero-out last element           list[i] = 0;           count--;         }         else {           // shift value           list[i] = list[i+1];         }      }   } }    

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -