【问题标题】:How to delete the last element from an array?如何从数组中删除最后一个元素?
【发布时间】:2014-12-09 01:40:13
【问题描述】:

现在我正在使用递归回溯,我的任务是在迷宫中找到最长的路径,质量表示为被坐标覆盖的场,并且墙壁的坐标在文件中很痛。 我制作了一个解析器来解析输入文件并构建墙壁,但我也将此坐标存储在对象类型 Coordinate 的数组中,以检查是否可以将下一条“蛇”移动到下一个字段,然后我创建了这个方法,现在我明白了,当我使用回溯时,我需要一个方法来从数组中删除最后一个坐标,我该怎么做?目标是不使用数组列表或链表只有数组! 谢谢!

public class Coordinate {
int xCoord;
int yCoord;

 Coordinate(int x,int y) {
     this.xCoord=x;
     this.yCoord=y;
 }

 public int getX() {
     return this.xCoord;
 }

 public int getY() {
     return this.yCoord;
 }
 public String toString() {
     return this.xCoord + "," + this.yCoord;

 }

 }

还有

public class Row {
static final int MAX_NUMBER_OF_COORD=1000;

Coordinate[] coordArray;
int numberOfElements;


Row(){
    coordArray = new Coordinate[MAX_NUMBER_OF_COORD];
    numberOfElements=0;

   }


void add(Coordinate toAdd) {
    coordArray[numberOfElements]=toAdd;
    numberOfElements +=1;
}
boolean ifPossible(Coordinate c1){
    for(int i=0;i<numberOfElements;i++){

        if(coordArray[i].xCoord==c1.xCoord && coordArray[i].yCoord==c1.yCoord){
                return false;
            }
        }


    return true;
}

 }

【问题讨论】:

  • arrays 必须使用吗?您可以使用像 ArrayList 这样的对象变体或其他有用的方法。
  • 是的,这是强制性的,我知道使用它们很愚蠢,但这是强制性的!
  • 使用一个足够长的数组,同时保留一个int变量,size,它包含了数组中有效元素的数量。

标签: java arrays recursion backtracking


【解决方案1】:

由于在 Java 中,数组是不可调整大小的,因此您必须将所有内容复制到一个新的、更短的数组中。

Arrays.copyOf(original, original.length-1)

【讨论】:

    【解决方案2】:
        @Test
        public void removeLastElement() {
    
        String[] lastElementRemoved = { "one", "two", "three" };
    
        String[] removedElement = Arrays.copyOf(lastElementRemoved, 2);
    
        System.out.println(Arrays.toString(removedElement));
        }
    

    【讨论】:

      【解决方案3】:

      我知道这是一个非常古老的线程。仍然批准的答案本身对我不起作用。我就是这样解决的。

      创建一个这样的方法:

      String[] sliceArray(String[] arrayToSlice, int startIndex, int endIndex) throws ArrayIndexOutOfBoundsException {
          if (startIndex < 0)
              throw new ArrayIndexOutOfBoundsException("Wrong startIndex = " + startIndex);
          if (endIndex >= arrayToSlice.length)
              throw new ArrayIndexOutOfBoundsException("Wrong endIndex = " + endIndex);
      
          if (startIndex > endIndex) { // Then swap them!
              int x = startIndex;
              startIndex = endIndex;
              endIndex = x;
          }
      
          ArrayList<String> newArr = new ArrayList<>();
          Collections.addAll(newArr, arrayToSlice);
          for (int i = 0; i < arrayToSlice.length; i++) {
              if (!(i >= startIndex && i <= endIndex)) // If not with in the start & end indices, remove the index
                  newArr.remove(i);
          }
          return newArr.toArray(new String[newArr.size()]);
      }
      

      然后这样称呼它:

      String lines[] = {"One", "Two", "Three", "Four", "Five"};
      lines = sliceArray(lines, 0, 3);
      

      这将导致:

      "One", "Two", "Three", "Four"
      

      现在我可以以任何我想要的方式对数组进行切片!

      lines = sliceArray(lines, 2, 3);
      

      这将导致:

      "Three", "Four"
      

      【讨论】:

      • 如果你需要指定一个开始和结束位置你应该使用Arrays.copyOfRange
      猜你喜欢
      • 2012-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 2020-07-04
      • 1970-01-01
      相关资源
      最近更新 更多