Java 如何将一个对象数组复制到另一个数组?
我正在用Java制作一个简单的对象数组,代码的一部分如下Java 如何将一个对象数组复制到另一个数组?,java,oop,Java,Oop,我正在用Java制作一个简单的对象数组,代码的一部分如下 public class Student{ private String name; private double score; public Student(String name){ this.name=name; this.score=score } public class Classroom{ private Student[] listStu; p
public class Student{
private String name;
private double score;
public Student(String name){
this.name=name;
this.score=score
}
public class Classroom{
private Student[] listStu;
private int sc;
public addStu(Student stu){
listStu[sc]=stu;
sc++;
}
public Student[] getClassroom(){
return listStu;
}
public int getNum(){
return sc;
}
public printList(){
for (int i=0;i<sc;i++){
System.out.pritnln(listStu[i].getName());
}
}
问题是当我在主类中修改类stuList的对象时,类似于:
stuList[0].setName("peter");
当我调用printList方法时,我看到数组中的数据也被修改了(在我的例子中,“michael”的名称被“peter”更改)。我的问题是如何将我的对象数组复制到主类的stuList中,这样这个数组就不会干扰我的原始数据了?这个程序真的有必要吗?或者这种情况不太可能发生
谢谢通常您会使用
列表
,而不是编写自己的收藏,但是问题是相同的。你有一个数组的浅拷贝,看起来你想要一个深拷贝。您需要使用包含相同信息的新Student对象创建一个新数组,而不是引用数组
显然,这样做既繁琐又缓慢,一般建议避免更改从集合中获得的任何集合(如数组)。i、 e.除非“Michael”将他的名字改为“Peter”,否则使用setName是个坏主意。如果你想用Peter替换michael,你有一个新的学生,这应该替换旧的学生参考资料。通常你会使用
列表,而不是编写自己的收藏,但是问题是相同的。你有一个数组的浅拷贝,看起来你想要一个深拷贝。您需要使用包含相同信息的新Student对象创建一个新数组,而不是引用数组
显然,这样做既繁琐又缓慢,一般建议避免更改从集合中获得的任何集合(如数组)。i、 e.除非“Michael”将他的名字改为“Peter”,否则使用setName是个坏主意。如果你想用Peter替换michael,你有一个enw学生,这应该替换旧的学生引用。在Java中,你使用指针,这就是为什么当你在第二个数组中修改时,它也会在第一个数组中修改。尝试更改此选项:
public addStu(Student stu){
listStu[sc]=stu;
sc++;
}
为此:
public addStu(Student stu){
listStu[sc]=(Student) stu.clone();
sc++;
}
看看它是否有效。在Java中,您使用指针,这就是为什么当您在第二个数组中进行修改时,它也在第一个数组中进行修改。尝试更改此选项:
public addStu(Student stu){
listStu[sc]=stu;
sc++;
}
为此:
public addStu(Student stu){
listStu[sc]=(Student) stu.clone();
sc++;
}
并查看它是否工作。要复制阵列,请使用System.arraycopy(请参阅)
但是请记住:Java中的对象数组是对数组元素的引用数组。与克隆集合类似,副本将包含相同的元素。对于您的用例,您必须复制元素本身。要复制数组,请使用System.arraycopy(请参阅)
但是请记住:Java中的对象数组是对数组元素的引用数组。与克隆集合类似,副本将包含相同的元素。对于您的用例,您必须复制元素本身。对Student
类进行以下更改:
class Student implements Cloneable {
protected Student clone() throws CloneNotSupportedException {
return (Student) super.clone();
}
public Student[] deepCopy() throws CloneNotSupportedException {
Student[] deepCopy = new Student[this.listStu.length];
for (Student student : this.listStu) {
deepCopy[0] = student.clone();
}
return deepCopy;
}
在课堂
课堂中添加此方法:
class Student implements Cloneable {
protected Student clone() throws CloneNotSupportedException {
return (Student) super.clone();
}
public Student[] deepCopy() throws CloneNotSupportedException {
Student[] deepCopy = new Student[this.listStu.length];
for (Student student : this.listStu) {
deepCopy[0] = student.clone();
}
return deepCopy;
}
然后在main
方法中:
Student[] backupList = c.deepCopy();
backupList[0].setName("test");
System.out.println(backupList[0].getName());
System.out.println(stuList[0].getName());
将输出:
test
michael
此外,代码中似乎存在一些语法错误:
public Student(String name){
this.name=name;
this.score=score
}
缺少右括号}
(来自方法的括号),分数不是参数,缺少代码>
但是,如果您使用此解决方案,请确保您记录了更多关于Java中的克隆的信息
对学生
类进行以下更改:
class Student implements Cloneable {
protected Student clone() throws CloneNotSupportedException {
return (Student) super.clone();
}
public Student[] deepCopy() throws CloneNotSupportedException {
Student[] deepCopy = new Student[this.listStu.length];
for (Student student : this.listStu) {
deepCopy[0] = student.clone();
}
return deepCopy;
}
在课堂
课堂中添加此方法:
class Student implements Cloneable {
protected Student clone() throws CloneNotSupportedException {
return (Student) super.clone();
}
public Student[] deepCopy() throws CloneNotSupportedException {
Student[] deepCopy = new Student[this.listStu.length];
for (Student student : this.listStu) {
deepCopy[0] = student.clone();
}
return deepCopy;
}
然后在main
方法中:
Student[] backupList = c.deepCopy();
backupList[0].setName("test");
System.out.println(backupList[0].getName());
System.out.println(stuList[0].getName());
将输出:
test
michael
此外,代码中似乎存在一些语法错误:
public Student(String name){
this.name=name;
this.score=score
}
缺少右括号}
(来自方法的括号),分数不是参数,缺少代码>
但是,如果您使用此解决方案,请确保您记录了有关Java中克隆的更多信息。
您可以简单地执行以下操作:
Student newStudent = stuList[0];
newStudent.setName("peter");
现在您有了一个新对象,如果更改它,数组值将不会受到影响。您可以简单地执行以下操作:
Student newStudent = stuList[0];
newStudent.setName("peter");
现在您有了一个新对象,如果更改它,数组值将不会受到影响。传递参数是java的关键。调用c.addStu()只传递对student对象的引用。因此,您的对象仍然是相同的,它绝对不依赖于使用哪个特定引用来执行实际的对象操作,结果将永远是相同的,因为只有一个对象。创建对象的副本是解决问题的好方法。看看这篇文章:传递参数是java的关键。调用c.addStu()只传递对student对象的引用。因此,您的对象仍然是相同的,它绝对不依赖于使用哪个特定引用来执行实际的对象操作,结果将永远是相同的,因为只有一个对象。创建对象的副本是解决问题的好方法。看看这篇文章: