-2

In this assigment, you have to implement a simple tour planing system. The data for the available tours, each with multiple way points, is statically given. A single waypoint consists of a x-value and a y-value. I have to write 2 function: int getCountOfTours- returns the number of available tours int[][] createDeepCopyOfTour - returns a deep copy of the tour at the index idx

I have done the first function, but I don't understand the second function createDeepCopyOfTour

I want to figure it out how the second function works. Please help me. thank you so much in advance!! Here is my code:

private static final int[][][] TOUR = new int[][][]{
        {{0, 0}, {4, 0}, {4, 3}, {0, 3}}, 
        {{0, 0}, {3, 0}, {3, 4}, {0, 0}}, 
        {{1, 3}, {3, 2}, {0, 4}, {2, 2}, {3, 1}, {1, 4}, {2, 3}}, 
        {{-2, -1}, {-2, +3}, {4, 3}, {0, 0}} 
    };


public static int[][] createDeepCopyOfTour(int idx) {
        throw new UnsupportedOperationException("Not supported yet.");
//I dont understand about this function.
    }   

1 Answers1

0

To simply put, a deep copy is allocating a new memory region to store a duplicate of whatever you are trying to copy. In the case of deep copying an array, you would create a new array and use a for loop to copy the values from the original into the new array. The purpose of your createDeepCopyOfTour function from what I can gather is to create a new array containing the waypoints of a tour of the specified index in the static TOUR array.

Unfortunately it is not as simple as:

private static final int[][][] TOUR = new int[][][]{
    {{0, 0}, {4, 0}, {4, 3}, {0, 3}}, 
    {{0, 0}, {3, 0}, {3, 4}, {0, 0}}, 
    {{1, 3}, {3, 2}, {0, 4}, {2, 2}, {3, 1}, {1, 4}, {2, 3}}, 
    {{-2, -1}, {-2, +3}, {4, 3}, {0, 0}} 
};


public static int[][] createDeepCopyOfTour(int idx) {
    return TOUR[idx];
}

The above will create a shallow copy and will merely return the reference to the original array. To create a deep copy you will need to create a new array using the new keyword which will allocate new memory for whatever you want to duplicate and then use a for loop to copy the values into the new array. Fortunately for you this is simple since we know there are only two axis for each waypoint coordinate so you only need one for loop to copy the values over.

private static final int[][][] TOUR = new int[][][]{
    {{0, 0}, {4, 0}, {4, 3}, {0, 3}}, 
    {{0, 0}, {3, 0}, {3, 4}, {0, 0}}, 
    {{1, 3}, {3, 2}, {0, 4}, {2, 2}, {3, 1}, {1, 4}, {2, 3}}, 
    {{-2, -1}, {-2, +3}, {4, 3}, {0, 0}} 
};


public static int[][] createDeepCopyOfTour(int idx) {
    int tour[][] = new int[TOUR[idx].length][2];
    for (int i = 0; i < TOUR[idx].length; i++)
    {
        tour[i][0] = TOUR[idx][i][0];
        tour[i][1] = TOUR[idx][i][1];
    }

    return tour;
}
Red
  • 16
  • 3