I have a strange problem that confused me completely.
I am using WEKA for my project, and I want to change class values of my dataset to test somethings; so I preserve my dataset into another value whose name is d, and using setClassValue method for changing class values. Finally, I print classValues for each instance in d and dataset variables.
Instances d=dataset;
for (int i = 0; i <dataset.size(); i++) {
dataset.get(i).setClassValue(10);
System.out.println(dataset.get(i).classValue()+ "\t" +d.get(i).classValue() );
}
Result is not believable, both d and dataset class values were changed. Why did this happen? How can I change class values of a dataset with considering that I should preserve my original dataset?
d=dataset
You now have two variables that both refer to the same object.
Unlike C++, Java never implicitly copies anything.
If you want a separate copy of an object, you need to make one yourself.
If the object implements Cloneable
, you can do that using clone()
.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다