Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将对象克隆到数组列表中,java_Java_Object_Arraylist_Clone - Fatal编程技术网

将对象克隆到数组列表中,java

将对象克隆到数组列表中,java,java,object,arraylist,clone,Java,Object,Arraylist,Clone,我创建了一个对象,它有许多属性,包括一个锯齿状数组属性,称为矩阵 在我的脚本中,我想克隆对象的一个副本,并将其放入对象的arraylist中。但是,我无法正确设置矩阵属性的克隆副本,因为它一直将最后一个已知的引用传递到我的列表中。代码如下 MatrixObject newMatrixObject = new MatrixObject(); List<MatrixObject> listMatrix = new ArrayList<MatrixObject>(); try

我创建了一个对象,它有许多属性,包括一个锯齿状数组属性,称为矩阵

在我的脚本中,我想克隆对象的一个副本,并将其放入对象的arraylist中。但是,我无法正确设置矩阵属性的克隆副本,因为它一直将最后一个已知的引用传递到我的列表中。代码如下

MatrixObject newMatrixObject = new MatrixObject();
List<MatrixObject> listMatrix = new ArrayList<MatrixObject>();

try {  //some code here looking int text file

    int[][] myArray = some value;
    newMatrixObject.matrix = myArray; //this works but keeps changing to the last value of myArray
    //I tried this as well
    newMatrixObject.matrix = newMatrixObject.SetMatrix(myArray); // didnt work either and I tried setting it without returning an array, same story
    listMatrix.add(new MatrixObject(newMatrixObject));
}
我也在课堂上创建了这个方法,但我认为它不起作用

public  int[][] SetMatrix(int[][] inputMatrix){
    //if (inputMatrix == null){
    //return null;
    //}

    int [][] result = new int [inputMatrix.length][];
    for ( int i= 0; i< inputMatrix.length; i++)
    {
       result[i] = Arrays.copyOf(inputMatrix[i], inputMatrix[i].length);
    }

    System.out.println(Arrays.deepToString(result));
    return result;
}

如果有更好的方法将对象的克隆添加到列表中,也可以。我很容易,只是想弄明白这件事

很难说清楚你在做什么,但我认为问题在于构造器

public MatrixObject (MatrixObject copy) {

    this.startDate = copy.startDate;
    this.matrix = copy.matrix;
}
这将创建一个共享副本内部的MatrixObject,因此对新MatrixObject或旧MatrixObject所做的任何更改都将实际更改另一个MatrixObject。相反,您应该复制所有字段,如下所示:

public MatrixObject (MatrixObject copy) {

    this.startDate = new Date(copy.startDate.getTime());
    this.matrix = new int[copy.matrix.length][];
    for (int i = 0; i < copy.matrix.length; i++)
        this.matrix[i] = Arrays.copyOf(copy.matrix[i], copy.matrix[i].length);
}

很难说清楚你在做什么,但我认为问题在于构造器

public MatrixObject (MatrixObject copy) {

    this.startDate = copy.startDate;
    this.matrix = copy.matrix;
}
这将创建一个共享副本内部的MatrixObject,因此对新MatrixObject或旧MatrixObject所做的任何更改都将实际更改另一个MatrixObject。相反,您应该复制所有字段,如下所示:

public MatrixObject (MatrixObject copy) {

    this.startDate = new Date(copy.startDate.getTime());
    this.matrix = new int[copy.matrix.length][];
    for (int i = 0; i < copy.matrix.length; i++)
        this.matrix[i] = Arrays.copyOf(copy.matrix[i], copy.matrix[i].length);
}
使用new后跟类名来复制对象通常会导致代码不可扩展。使用的应用程序克隆是实现这一点的更好方法。但是,使用Java中提供的克隆也会有问题

最好从克隆方法调用非公共副本构造函数。这使我们能够将创建对象的任务委托给类本身的实例,从而提供可扩展性,并使用非公共复制构造函数安全地创建对象

下面是对MatrixObject类的修改。它展示了如何依靠构建过程安全地实现克隆。这在类包含final字段的情况下特别有用。

MatrixObject类 主要方法 输出 使用new后跟类名来复制对象通常会导致代码不可扩展。使用的应用程序克隆是实现这一点的更好方法。但是,使用Java中提供的克隆也会有问题

最好从克隆方法调用非公共副本构造函数。这使我们能够将创建对象的任务委托给类本身的实例,从而提供可扩展性,并使用非公共复制构造函数安全地创建对象

下面是对MatrixObject类的修改。它展示了如何依靠构建过程安全地实现克隆。这在类包含final字段的情况下特别有用。

MatrixObject类 主要方法 输出
你有其他语言的背景吗,可能是JavaScript?这整件事看起来好像你在尝试使用Java关键字编程,但用其他语言思考。复制和克隆某些东西通常是一个坏主意,你应该尽量保持一切不变。在一个地方看到文本文件、矩阵和日期是很奇怪的。你到底想做什么?问题的一部分似乎是你正在努力使一个拷贝构造函数“适合”你想做的,但你并不需要它。您可能需要使用公共MatrixObject[][]inputMatrix,它以与SetMatrix相同的方式复制inputMatrix。SetMatrix不设置任何内容,也不与实例变量交互:它应该被称为copyMatrix并被设置为静态。您是否有其他语言的背景,可能是JavaScript?这整件事看起来好像你在尝试使用Java关键字编程,但用其他语言思考。复制和克隆某些东西通常是一个坏主意,你应该尽量保持一切不变。在一个地方看到文本文件、矩阵和日期是很奇怪的。你到底想做什么?问题的一部分似乎是你正在努力使一个拷贝构造函数“适合”你想做的,但你并不需要它。您可能需要使用公共MatrixObject[][]inputMatrix,它以与SetMatrix相同的方式复制inputMatrix。SetMatrix不设置任何内容,也不与实例变量交互:它应该被称为copyMatrix并成为静态的。谢谢!这很有魅力。感谢所有评论。我是从C背景过来的,很高兴能得到一些帮助谢谢!这很有魅力。感谢所有评论。我来自C语言背景,很高兴能得到一些帮助
public static void main(String[] args) {
    String output = "";
    Calendar dates = Calendar.getInstance();
    Date dateOne = dates.getTime();
    // offset day of the month by one day
    dates.set(Calendar.DAY_OF_MONTH, dates.get(Calendar.DAY_OF_MONTH) + 1);
    Date dateTwo = dates.getTime();

    int[][] myArrayOne = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }, };
    int[][] myArrayTwo = { { 0, 0, 1 }, { 0, 1, 0 }, { 1, 0, 0 }, };

    // create two MatrixObjects, an original and a copy
    MatrixObject oneMO = new MatrixObject(dateOne, myArrayOne);
    MatrixObject copyMO = (MatrixObject) oneMO.clone();

    // show the contents of the original MatrixObject and its copy
    output += String.format("First MatrixObject:%n%s%n", oneMO.toString());
    output += String
    .format("Copied MatrixObject:%n%s%n", copyMO.toString());

    // alter the original MatrixObject
    oneMO.setMatrix(myArrayTwo);
    oneMO.setDate(dateTwo);

    // show that alterations to the original MatrixObject did not
    // effect the copy
    output += String.format("Changed First MatrixObject:%n%s%n",
    oneMO.toString());
    output += String.format("Unchanged Copied MatrixObject:%n%s%n",
    copyMO.toString());

    System.out.println(output);
}
First MatrixObject: Mon Apr 20 21:29:14 EDT 2015 [1, 0, 0] [0, 1, 0] [0, 0, 1] Copied MatrixObject: Mon Apr 20 21:29:14 EDT 2015 [1, 0, 0] [0, 1, 0] [0, 0, 1] Changed First MatrixObject: Tue Apr 21 21:29:14 EDT 2015 [0, 0, 1] [0, 1, 0] [1, 0, 0] Unchanged Copied MatrixObject: Mon Apr 20 21:29:14 EDT 2015 [1, 0, 0] [0, 1, 0] [0, 0, 1]