【问题标题】:IntegerList help in JavaJava 中的 IntegerList 帮助
【发布时间】:2015-06-02 22:19:27
【问题描述】:

因此,我们在课堂上遵循实验室手册的说明。我能够完成第一步和第二步,我只需要第三步的帮助。


实验室手册说明:

  1. 将方法 void removeFirst(int newVal) 添加到 IntegerList 类,从列表中删除第一次出现的值。如果该值没有出现在列表中,它应该什么都不做(但这不是错误)。删除一个项目不应该改变数组的大小,但请注意,数组值确实需要保持连续,所以当你删除一个值时,你必须将它之后的所有内容向下移动以填满它的空间。还要记住减少跟踪元素数量的变量。

在 IntegerListTest 的菜单中添加一个选项来测试您的新方法。


整数列表

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

整数列表测试

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

公共静态无效调度(int选择) {

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

【问题讨论】:

    标签: java arrays


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:
      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;
      }
      

      【讨论】:

      • 非常感谢。我越来越紧张,因为我想不通,这是为了我的计算机科学课。 :)
      【解决方案3】:

      我真的没有时间给你很多细节,我知道这不是一个真正有效的方法,但你可以做的是总是创建一个“size-1”的新数组,然后记下您要删除的数组元素的索引,然后将该数组元素之前和之后的所有内容复制到您的新数组中,一旦完成所有这些,您就可以将其复制回原始数组,就像这样 oldArray = newArray.

      【讨论】:

        猜你喜欢
        • 2011-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-20
        • 2011-05-08
        • 1970-01-01
        相关资源
        最近更新 更多