如何在Java中深度复制不规则形状的2D数组?
IE浏览器.
int[][] nums = {{5},
{9,4},
{1,7,8},
{8,3,2,10}}
Run Code Online (Sandbox Code Playgroud)
我Arrays.arrayCopy()出于某种原因无法使用(版本控制?)
我在我的 main 中制作了一个对象Recipe recipeOne = new Recipe("Pepperoni Pizza");
该对象是此处定义和构造的对象数组的实例!
public class Recipe implements Cloneable{
String Name;
final int INGREDIENT_ARRAY_MAX = 10;
Ingredient Recipe[] = new Ingredient[INGREDIENT_ARRAY_MAX];
public Recipe(String name){
Name = name;
}
Run Code Online (Sandbox Code Playgroud)
所以我想用这条线制作这个对象的深层副本Recipe ressippi = (Recipe) recipe.clone();,它把我发送到这里!
public Object clone(){
Recipe cloneRec = new Recipe(Name);
return cloneRec;
}
Run Code Online (Sandbox Code Playgroud)
我知道这目前是一个浅拷贝,因为该方法只传递引用,所以如果我尝试对我的新对象(它是recipeOne 的克隆)进行名称更改...它将更改它们的两个名称。显然我不希望这样,我对此很迷茫,有人可以帮忙吗?
编辑:@Rohit Jain
我的 Recipe 类和 Ingredient 类(菜谱数组保存的对象)都有 toString 方法,并且 Recipe 调用配料,以便以漂亮的小格式将其全部打印出来。当我在我的“recipeOne”对象(称为意大利辣香肠披萨的那个)上调用它时,我得到“意大利辣香肠披萨:1.0 磅面团,8.0 盎司酱汁,10.0 盎司奶酪”
然后我继续创建 ressippi 对象并将其设置为recipeOne 的克隆,所以从这里开始一切都很好...然后我将 ressippi 的名称更改为“Pineapple Pizza”,打印效果很好,但它不打印recipeOne 的 3 个成分对象存储,这是它应该做的!
下面是我的函数,它给出了给定数组中的元素总和达到特定目标的所有可能性。我可以打印列表,但是结果列表没有更新。
public List<List<Integer>> helper(List<List<Integer>> res, int[] c, int l, int h, int target, List<Integer> temp){
if(target == 0){
res.add(temp);
System.out.println(temp);
return res;
}
if(target < c[l]){
return res;
}
for(int i = l; i <=h; i++){
temp.add(c[i]);
res = helper(res, c,i,h,target-c[i], temp);
temp.remove(temp.size()-1);
}
return res;
}
Run Code Online (Sandbox Code Playgroud)
res 最后是空数组列表的数组列表,但第 5 行正确打印临时数组列表。
该函数的调用如下。
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> temp = new ArrayList<Integer>();
res = helper(res,candidates, 0, candidates.length-1, target, temp);
Run Code Online (Sandbox Code Playgroud)
示例:给定数组 = [1,2,3],目标 = 6
标准输出:
[1, 1, 1, 1, 1, …Run Code Online (Sandbox Code Playgroud) 对于任何Java开发人员来说,这个问题应该很容易.我发誓我在花了大约2个小时后查了一下,但我真的不明白这段代码有什么问题.
基本上,我正在实施Karger的最小割算法.它需要我在图中合并节点,然后计算结尾的交叉边数(一个int值).该算法必须重复n次,始终从起始图开始.我的问题是我无法创建我的Graph对象的深层副本,我找不到错误.
我已经裁剪了代码只是为了显示问题,而不是更多,但我仍然无法弄清楚出了什么问题.这里的代码是.
类节点:
public class Node {
public Integer Data;
public Node() {
Data = 0;
}
public Node(Node rhs) {
Data = rhs.Data.intValue();
}
public Node(Integer rhs) {
Data = rhs.intValue();
}
public void setNode(Integer rhs) {
Data = rhs;
}
Run Code Online (Sandbox Code Playgroud)
类图:
public class Graph {
public ArrayList<ArrayList<Node>> AdjList;
public ArrayList<Node> NodeSet; // This contains all the nodes
public Graph() {
AdjList = new ArrayList<ArrayList<Node>>();
NodeSet = new ArrayList<Node>();
}
public Graph(Graph G) {
AdjList = new ArrayList<ArrayList<Node>>(); …Run Code Online (Sandbox Code Playgroud) 在编写测试时,我遇到了克隆对象的需求。通过 apache-commons 找到了 2 个 Utill 类,然后我试图找到我应该使用哪一个,我试图通过读出两个 API 文档来找到差异,但没有找到我应该使用哪一个
根据文档: 根据可用的属性 getter 和 setter 克隆 bean,即使 bean 类本身没有实现 Cloneable。
疑问:我应该在 DTO 克隆上使用它吗?
SerializationUtils clone() API 文档
根据文档:
使用序列化深度克隆对象。
这比在对象图中的所有对象上手动编写克隆方法要慢很多倍。然而,对于复杂的对象图,或者对于那些不支持深度克隆的对象图,这可能是一个简单的替代实现。当然,所有对象都必须是可序列化的。
疑问:我应该将它用于 DTO 和实体对象吗?或仅适用于实体
我有一堂课(文学).我需要能够在整个应用程序中将完整的Literal实例保存在内存中,并且有一份我可以更改的副本.我用两种方法来做到这一点:
Literalimplements Cloneable和override Object.clone()方法.工厂构造:
public Literal(Literal lit){
this = lit;
}
Run Code Online (Sandbox Code Playgroud)在这两种情况下,复制都不起作用.我对副本所做的每一项更改都会更改原件.有谁知道我做错了什么?
当我只将对象放在listOfRates中然后通过复制它创建inverseListOfRates时,我首先注意到了这个问题.但即使使用这种方法,我也不能改变一个列表而不改变另一个列表.
我该如何解决这个问题?
List<HistoricRate> listOfRates = new ArrayList<HistoricRate>();
List<HistoricRate> inverseListOfRates = new ArrayList<HistoricRate>();
for (HistoricRate rate : rates){
listOfRates.add(rate);
inverseListOfRates.add(rate);
}
inverseListOfRates.forEach(r -> r.setMid(1 / r.getMid()));
Run Code Online (Sandbox Code Playgroud) 我正在尝试将每个元素从一个ArrayList(av)复制到另一个(copia).问题在于它们是通过引用复制的,因此每当我对原始版本进行任何更改时,副本也会被修改.当然,这种行为不是必需的.我应该怎么写这个方法?
public void copiarArrayList(ArrayList<Articulo_Venta> copia, ArrayList<Articulo_Venta> av){
copia.clear();
for (int i = 0; i < av.size(); i++) {
copia.add(av.get(i));
}
}
Run Code Online (Sandbox Code Playgroud)
Articulo_Venta有以下几个字段:
int codigo;
String nombre;
float cantidad;
Run Code Online (Sandbox Code Playgroud)
PS:我也试过了下一个:
copia = new ArrayList<Articulo_Venta>(av);
Run Code Online (Sandbox Code Playgroud)
但它的元素仍然指向原始的ArrayList.
解释:
public class SomeClass {
int aNumber = 0;
public void changeNumber(int number){
aNumber = number;
}
public int getNumber(){
return aNumber;
}
}
public class Testapp {
public static void main(String[] args) {
NewClass object1 = new NewClass();
NewClass object2 = object1;
object1.changeNumber(5);
object2.changeNumber(2);
System.out.println("object1: "+object1.getNumber());
System.out.println("object2: "+object2.getNumber());
}
}
Run Code Online (Sandbox Code Playgroud)
这将输出:object1:2 object2:2
我如何输出:object1:5 object2:2