Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从数组中删除最后一个元素?_Java_Arrays_Recursion_Backtracking - Fatal编程技术网

Java 如何从数组中删除最后一个元素?

Java 如何从数组中删除最后一个元素?,java,arrays,recursion,backtracking,Java,Arrays,Recursion,Backtracking,现在我正在使用递归回溯,我的任务是在迷宫中找到最长的路径,质量表示为坐标覆盖的区域,并且墙的坐标在文件中。 我制作了一个解析器来解析输入文件并构建墙,但我也将这个坐标存储在对象类型坐标的数组中,以检查是否可以在下一个字段上移动下一条“蛇”,然后我创建了这个方法,现在我明白了,当我使用回溯时,我需要一种方法从数组中删除最后一个坐标,我该怎么做?目标不是只使用数组列表或链接列表数组! 谢谢大家! public class Coordinate { int xCoord; int yCoord;

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

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;

 }

 }

公共类行{
静态最终整数最大坐标数=1000;
坐标[]坐标数组;
整数元;
第()行{
坐标数组=新坐标[坐标的最大坐标数];
numberOfElements=0;
}
无效添加(坐标添加){
coordArray[numberOfElements]=toAdd;
元素数+=1;
}
布尔型(如果可能)(坐标c1){

对于(inti=0;i,因为在Java中,数组是不可调整大小的,所以必须将所有内容复制到一个新的、较短的数组中

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

我知道这是一条非常古老的线索。但批准的答案本身对我来说并不适用。我就是这样解决的

创建如下方法:

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"
"Three", "Four"
现在我可以用我想要的任何方式切片数组了

lines = sliceArray(lines, 2, 3);
这将导致:

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

arrays
是否必须使用?您可以使用对象变量,如
ArrayList
或其他提供帮助的方法。是的,这是必须的,我知道使用它们很愚蠢,但这是必须的!使用数组足够长的时间,并保留一个int变量size,其中包含数组的有效元素数。如果需要指定起始位置和结束位置您应该使用
数组。copyOfRange