我如何编写函数 returns Java 中索引处的三维数组的深层副本?
How I can write a function returns a deep copy of the three dimensional array at the index in Java?
在这个作业中,你必须实现一个简单的旅游规划系统。可用旅行的数据是静态给出的,每个旅行都有多个路点。单个航路点由 x 值和 y 值组成。
我必须写 2 个函数:
int getCountOfTours- returns 可用游览的数量
int[][] createDeepCopyOfTour - returns 索引 idx
的游览深度副本
第一个功能我做了,但是第二个功能createDeepCopyOfTour我不懂
我想弄清楚第二个函数是如何工作的。请帮我。非常感谢你!
这是我的代码:
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.
}
简单地说,深拷贝就是分配一个新的内存区域来存储您要复制的任何内容的副本。在深度复制数组的情况下,您将创建一个新数组并使用 for 循环将值从原始数组复制到新数组中。
我可以收集到的 createDeepCopyOfTour 函数的目的是创建一个新数组,其中包含静态 TOUR 数组中指定索引的游览 waypoints。
不幸的是,它并不像这样简单:
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];
}
以上将创建一个浅表副本,并且只会 return 对原始数组的引用。要创建深拷贝,您需要使用 new 关键字创建一个新数组,该关键字将为您想要复制的任何内容分配新内存,然后使用 for 循环将值复制到新数组中。幸运的是,这很简单,因为我们知道每个航路点坐标只有两个轴,所以您只需要一个 for 循环来复制值。
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;
}
在这个作业中,你必须实现一个简单的旅游规划系统。可用旅行的数据是静态给出的,每个旅行都有多个路点。单个航路点由 x 值和 y 值组成。 我必须写 2 个函数: int getCountOfTours- returns 可用游览的数量 int[][] createDeepCopyOfTour - returns 索引 idx
的游览深度副本第一个功能我做了,但是第二个功能createDeepCopyOfTour我不懂
我想弄清楚第二个函数是如何工作的。请帮我。非常感谢你! 这是我的代码:
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.
}
简单地说,深拷贝就是分配一个新的内存区域来存储您要复制的任何内容的副本。在深度复制数组的情况下,您将创建一个新数组并使用 for 循环将值从原始数组复制到新数组中。 我可以收集到的 createDeepCopyOfTour 函数的目的是创建一个新数组,其中包含静态 TOUR 数组中指定索引的游览 waypoints。
不幸的是,它并不像这样简单:
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];
}
以上将创建一个浅表副本,并且只会 return 对原始数组的引用。要创建深拷贝,您需要使用 new 关键字创建一个新数组,该关键字将为您想要复制的任何内容分配新内存,然后使用 for 循环将值复制到新数组中。幸运的是,这很简单,因为我们知道每个航路点坐标只有两个轴,所以您只需要一个 for 循环来复制值。
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;
}