How to delete the last element from an array?

2020-03-01 07:22发布

问题:

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!

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;
}

 }

回答1:

Since Java arrays are non-resizable, you will have to copy everything into a new, shorter array.

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


回答2:

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"


回答3:

    @Test
    public void removeLastElement() {

    String[] lastElementRemoved = { "one", "two", "three" };

    String[] removedElement = Arrays.copyOf(lastElementRemoved, 2);

    System.out.println(Arrays.toString(removedElement));
    }