27

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

 }
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
Andre Liberty
  • 707
  • 1
  • 9
  • 17

4 Answers4

109

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

Arrays.copyOf(original, original.length-1)
Naman
  • 27,789
  • 26
  • 218
  • 353
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
1

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"
pedrofurla
  • 12,763
  • 1
  • 38
  • 49
1

Well, I found a more efficient option:

System.arraycopy(input, 0, new_array, 0, arr.length - 1);

In a nutshell, this method will copy arr's elements to new_array, without the last element. However this method will require you to create and use another array than the original.

-1
    @Test
    public void removeLastElement() {

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

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

    System.out.println(Arrays.toString(removedElement));
    }
Akshay Paliwal
  • 3,718
  • 2
  • 39
  • 43