【问题标题】:At a total loss with java list processing code完全失去了java列表处理代码
【发布时间】:2015-10-26 08:00:41
【问题描述】:

好的,这可能会是一个非常简单的问题,但老实说,我是编码新手,我在这里碰到了一堵砖墙。我需要在数组中插入一个值,将数据向右移动,并更新数组的大小。教授为我们提供了 cmets 来构建我们的代码,我已经掌握了大部分,但最后一部分让我很生气。任何人都可以帮忙吗?这是代码(相关部分在 //insert value and shift data...等下):

public class List {

    // Declare variables
    private int size = 0;
    private int maxSize = 100;
    private int[] data;
    Scanner keyboard = new Scanner(System.in);

    // constructors
    public List() {
        data = new int[maxSize];
    }

    public List(int maxSize) {
        this.maxSize = maxSize;
        data = new int[maxSize];
    }

    // methods
    // Adds a value into the array and updates the size
    public boolean add(int value) {
        if (size == maxSize) {
            System.out.println("Cannot add value since the list is full");
            return false;
        }
        data[size] = value;
        size++;
        return true;
    }

    // add multiple values to the list obtained from the keyboard
    public void addValues() {
        // declare local variables
        int count = 0;

        System.out.println("Enter multiple integers separated by spaces");
        String line = keyboard.nextLine();
        Scanner scanLine = new Scanner(line);
        try {
            while (scanLine.hasNext()) {
                data[size] = scanLine.nextInt();
                count++;
                size++;
            }
        } catch (ArrayIndexOutOfBoundsException aiobe) {
            System.out.println("Only " + count + " values could be added before the list is full");
            return;
        } catch (InputMismatchException ime) {
            System.out.println("Only " + count + " values could be added due to invalid input");
            return;
        }
    }

    // This will print all the elements in the list
    public void print() {
        System.out.println();
        for (int i = 0; i < size; i++) {
            System.out.print(data[i] + " ");
        }
        System.out.println();
    }

    // This methods returns the index of the key value if found in the list
    // and returns -1 if the key value is not in the list
    public int find(int key) {
        for (int i = 0; i < size; i++) {
            if (data[i] == key) {
                return i;
            }
        }
        return -1;
    }

    // This methods deletes the given value if exists and updates the size.
    public boolean delete(int value) {
        int index = find(value);
        if (index == -1) {
            System.out.println("The specified value is not in the list");
            return false;
        }
        for (int i = index; i < size - 1; i++) {
            data[i] = data[i + 1];
        }
        size--;
        return true;
    }

    // This methods inserts the value at the given index in the list
    public boolean insertAt(int index, int value) {
        // validate index value and insertability
        if (index < 0 || index > size || size == maxSize) {
            System.out.println("Invalid index or list is already full");
            return false;
        }
        // insert value and shift data to the right and update the size

        return true;
    }

    // This method removes the value at given index and shifts the data as needed
    public boolean removeAt(int index) {
        if (index >= 0 && index < size) {
            for (int i=index+1; i<size; i++)
                data[i-1] = data[i];
            size--;
            return true;
        }
        return false;
    }

    // This method sorts the values in the list using selection sort
    public void sort() {
        int temp;
        for (int j=size; j>1; j--) {
            int maxIndex = 0;
            for (int i=1; i<j; i++)
                if (data[maxIndex] < data[i])
                    maxIndex = i;
            temp = data[j-1];
            data[j-1] = data[maxIndex];
            data[maxIndex] = temp;
        }
    }
}

如果代码的结构也非常糟糕,我深表歉意,顺便说一下,我不确定如何在这个网站上格式化它,所以它看起来是正确的。

【问题讨论】:

  • 没有人会为你做这件事,请让我们知道你尝试了什么,什么没有奏效。
  • 类有点不一致,列表满时add不会添加新值。 addValues 不关心这一点。当调用 insertAt 并且列表已满时溢出会发生什么?最后一个元素被踢出?一般来说,如果你写下解决方案是什么(用文字、伪代码或其他什么方式),它会很有帮助——不要试图从你将编写的代码开始解决问题

标签: java arrays list shift


【解决方案1】:

//插入值并向右移动数据并更新大小

// 我认为大小是全局声明的对吗?

尺寸++;

for(int i=size - 1; i < 0; i--) {
    data[i] = data[i - 1];
}
data[index] = value;

如果您有最大尺寸,还会检查尺寸

【讨论】:

    【解决方案2】:

    认为下面的答案应该会有所帮助。由于已经有尺码检查,无需再次检查

    public boolean insertAt(int index, int value) {
        // validate index value and insertability
      if (index < 0 || index > 5) {
        System.out.println("Invalid index or list is already full");
        return false;
      }
      // insert value and shift data to the right and update the size
    
      for(int i=index;i<size;i++) {
          data[++i] = data[i];
      }
      data[index] = value;
    
      return true;
    }
    

    【讨论】:

      【解决方案3】:
      public boolean insertAt(int index, int value) {
              // validate index value and insertability
              if (index < 0 || index > size || size == maxSize) {
                  System.out.println("Invalid index or list is already full");
                  return false;
              }
              // insert value and shift data to the right and update the size
                 for (int i=size - 1; i > index; i--)
                 data[i+1] = data[i];
                 data[index] = value
                 size++;
              return true;
          }
      

      【讨论】:

      • 你能添加一些 cmets 来解决这个问题吗?
      • 我认为您的教授希望您完成“insertAt”功能。这是我的代码
      猜你喜欢
      • 2019-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-07
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      相关资源
      最近更新 更多