如何在Java中编写类似splice的方法

时间:2017-02-27 21:52:05

标签: java arrays methods splice

我的任务是创建一个与splice具有相同功能的方法,但是我无法在适当的索引中获得适当的值。我的代码如下,

public void PlaceElementAt(int newValue, int index) throws ArrayIndexOutOfBoundsException {

    //check that index is valid
    if (index >= 0 && index <= data.length) {  //Checks that the index is within position 0 and 7 by default.
        System.out.println ("Index is valid"); //returns index is valid if so
    }
    //increase size if necessary
    if (data.length == numElements) {  //checking if the number of elements is filling the spaces
      doubleCapacity();               // calls upon the double capacity method if it is
    }
    if (numElements==0) {
        data[numElements] = newValue;
        System.out.println ("Element: " + data[numElements] + " at index: " + index);
        numElements++;
    }
    //shuffle values down from index
    else {
     int bottompos = numElements-1;
     int loopcount = numElements-index;
     int NewBottom = numElements+1;
     for (int i=0; i<loopcount; i++){
         data[bottompos]=data[NewBottom];
         bottompos--;
         NewBottom--;
     }
      //insert newValue at index
      data[numElements] = newValue;
        System.out.println ("Element: " + data[numElements] +" at index: " + index);      
        numElements++;

    }
}

当我稍后在我的main方法中给出命令时,我的问题很明显。

myData.PlaceElementAt(3,0)

myData.PlaceElementAt(2,5)

myData.PlaceElementAt(7,3)

一旦我检查了断点,我就会看到值被添加到数组中,但是从索引0开始逐个添加它们。任何建议都会有所帮助。

2 个答案:

答案 0 :(得分:1)

我正在对你的班级结构进行一些假设(根据需要进行调整),但一般情况下我会建议向右移动,因为它会简化整个过程。基本上我们有一个数组,其最大允许大小由MAX给出,当前大小由size给出。每当我们添加到数组时,我们都会将size的值增加1(例如插入或添加到列表的后面)。现在,假设我们要在value处插入index。这将需要将此索引的所有元素向右移动1,然后将value插入到我们所创建的空间中。在进行任何插入之前,我们首先需要检查是否有足够的空间来插入项目。如果没有足够的空间,我们将需要分配额外的空间,禁止插入或其他替代方案。在您的情况下,您似乎想要分配更多空间。

class MyList
{
  private int MAX = 6;
  private int size = 0;
  private int[] array;

  public MyList()
  {
    array = new int[MAX];
  }

  public void placeElementAt(int value, int index)
  {
    if (size == 0)
    {
      // If size is 0, just insert the value at index 0.
      array[size++] = value;
      return;
    }

    if (index < 0 || index >= size)
    {
      // Index is out of bounds.
      System.out.println("Invalid index.");
      return;
    }

    if (size >= MAX)
    {
      // Max capacity reached -> allocate more space.
      doubleCapacity();
    }

    // Shift all elements at and above index right by 1.
    for (int i = size - 1; i >= index; i--)
    {
      array[i + 1] = array[i];
    }

    // Insert element.
    array[index] = value;
    size++;
  }

  public void doubleCapacity()
  {
    int[] newArray = new int[MAX * 2];

    // Copy old elements to new array.
    for (int i = 0; i < size; i++)
    {
      newArray[i] = array[i];
    }

    // Double MAX to reflect new array.
    MAX *= 2;
    array = newArray;

    System.out.println("Doubled");
  }

  public void add(int value)
  {
    if (size >= MAX)
    {
      // Max capacity reached -> allocate more space.
      doubleCapacity();
    }

    // Add the element to the back of the list.
    array[size++] = value;
  }

  public void print()
  {
    for (int i = 0; i < size; i++)
    {
      System.out.print(array[i] + " ");
    }
    System.out.println();
  }

  public static void main(String[] args)
  {
    MyList data = new MyList();
    data.placeElementAt(1, 0);
    data.print();
    data.placeElementAt(2, 0);
    data.print();
    data.placeElementAt(3, 0);
    data.print();
    data.placeElementAt(5, 0);
    data.print();
    data.placeElementAt(3, 0);
    data.print();
    data.placeElementAt(9, 0);
    data.print();
    data.placeElementAt(4, 0);
    data.print();
    data.placeElementAt(6, 0);
    data.print();
  }
}

此程序的输出(初始MAX = 6)将为......

1
2 1
3 2 1
5 3 2 1
3 5 3 2 1
9 3 5 3 2 1
Doubled
4 9 3 5 3 2 1
6 4 9 3 5 3 2 1

答案 1 :(得分:0)

嗯,看到你的代码,你就会收到一个&#34;索引&#34;参数。但是您没有使用索引插入阵列。

您的代码:

data[numElements] = newValue;
numElements++;

试试这个:

data[index] = newValue;
numElements++;

我认为这会解决它,但是稍后你必须处理指定索引已经有元素的情况。