Java 如何从数组中删除最后一个元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26357805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete the last element from an array?
提问by Andre Liberty
Now I'm working with the recursive backtracking,my assignment is to find the longest path in the maze,the mass is presented as the field covered with the coordinates,and the coordinates of the walls are sore in the file. I have made a parser to parse the input file and build the walls,but I have also stored this coordinates in the array of an object type Coordinate,to check whether it is possible to move the next piece of the "snake" on the next field,then I have created this method,now I have understood that I will need a method to remove the last coordinate from the array when I will use backtracking,how can I do it?the goal is not to use array lists or linked lists only arrays! Thank you!
现在我正在使用递归回溯,我的任务是找到迷宫中最长的路径,质量显示为覆盖有坐标的场,并且墙壁的坐标在文件中很痛。我已经制作了一个解析器来解析输入文件并构建墙壁,但我也将此坐标存储在对象类型 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;
}
}
And
和
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;
}
}
回答by Marko Topolnik
Since Java arrays are non-resizable, you will have to copy everything into a new, shorter array.
由于 Java 数组是不可调整大小的,因此您必须将所有内容复制到一个新的更短的数组中。
Arrays.copyOf(original, original.length-1)
回答by Dilip Muthukurussimana
I know its a very old thread. Still the approved answer itself didn't work for me. And this is how I resolved it.
我知道它是一个非常古老的线程。批准的答案本身仍然对我不起作用。这就是我解决它的方法。
Create a method like this:
创建一个这样的方法:
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()]);
}
Then called it like this:
然后这样称呼它:
String lines[] = {"One", "Two", "Three", "Four", "Five"};
lines = sliceArray(lines, 0, 3);
This will result in:
这将导致:
"One", "Two", "Three", "Four"
Now I can slice the array in whichever way I want!
现在我可以按照我想要的任何方式对数组进行切片!
lines = sliceArray(lines, 2, 3);
This will result in:
这将导致:
"Three", "Four"
回答by Mahendra Sri
@Test
public void removeLastElement() {
String[] lastElementRemoved = { "one", "two", "three" };
String[] removedElement = Arrays.copyOf(lastElementRemoved, 2);
System.out.println(Arrays.toString(removedElement));
}