Java中的IntegerList帮助

时间:2015-06-02 22:19:28

标签: java arrays

所以在课堂上我们按照实验手册说明进行操作。我能够做第一步和第二步,我只需要第三步的帮助。

Lab Manuel说明:

  1. 将一个方法void removeFirst(int newVal)添加到IntegerList类,该类从列表中删除第一次出现的值。如果值没有出现在列表中,它应该什么也不做(但这不是错误)。删除项目不应该更改数组的大小,但请注意,数组值确实需要保持连续,因此当您删除值时,您必须将其后的所有内容移动以填充其空间。还记得减少跟踪元素数量的变量。
  2. 在IntegerListTest的菜单中添加一个选项以测试新方法。

    IntegerList

    public class IntegerList
    {
        private int count;
        private double totalInt;
        int[] list; //values in the list
        //-------------------------------------------------------
        //create a list of the given size
        //-------------------------------------------------------
    
        void addElement(int newVal)
        {
            if (count == list.length)
                increaseSize();
    
            list[count] = newVal;
            count++;
        }
    
        void removeFirst(int newVal2)
        {
            for (int i = 0; i < 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()
        {
            for (int i=0; i<list.length; i++)
                {
                list[i] = (int)(Math.random() * 100) + 1;
                count++;
                }
    
        }
                public void print()
        {
            for (int i=0; i<count; i++)
                System.out.println(i + ":\t" + list[i]);
        }
    
    
    
    
    private void increaseSize()
    {
        int[] temp = new int[list.length * 2];
    
        for (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 the 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 would you like to add?");
        int newVal = scan.nextInt();
        list.addElement(newVal);
        break;
    case 4:
        System.out.println("What number do you want to 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 a new list (** do this first!! **)");
        System.out.println("2: Print the list");
        System.out.println("3: Add to the list");
        System.out.println("4: Remove Integer");
        System.out.print("\nEnter your choice: ");
            }
        }
    

    非常感谢任何帮助。如果你也可以解释为什么这样我会从中吸取教训。谢谢! :D

3 个答案:

答案 0 :(得分:0)

public void removeFirst(int val){
    //the position at which the algorithm currently is
    int i = 0;

    boolean found = false;

    //search for the first occurence of val in the array
    for(; i < count && list[i] != val ; i++);
    //i is now the index of the first value equal to val in list

    //if a match was found, the index must be smaller than the array-size
    found = (i < count);

    //shift all elements that are right of the value to the left
    //to leave no empty space. Since the value at i is the searched value  
    //it's left out (i += 1)
    i += 1;
    for(; i < count ; i++)
        list[i - 1] = list[i];//move the current element one to the left

    //check if a match was found and decrement the sizecounter, if one was found
    if(found)
        --count;
}

答案 1 :(得分:0)

我没有时间给你很多细节,我知道这不是一种有效的方法,但你能做的就是始终创造一个新的&#34; size-1&#34阵列;然后记下要删除的数组元素的索引,然后将该数组元素之前和之后的所有内容复制到新数组中,一旦完成所有这些操作,就可以将其复制回原始数组,如oldArray = newArray 。

答案 2 :(得分:0)

因此,您可以循环数组以查找与给定值匹配的第一个元素。 如果找到它,将其位置保存为索引并将其后的所有元素移动一个位置:

void removeFirst(int newVal2) {
  int index = -1;
  for (int i = 0; i < count; i++) {
     if (index == -1 && newVal2 == list[i]) {
       // element found - save index
       index = i;
     }
     if (index != -1) {
        // this 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];
        }
     }
  }
}