Mon Jul 23, 2012 4:53 pm
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
public static void main(String[] args) {
ArrayList firstArray = new ArrayList();
int size = 10;
for (int i = 0; i < size; i++) {
firstArray.add(new SumPool(i));
}
ArrayList secondArray = (ArrayList) firstArray.clone();
for (int i = 0; i < size; i++) {
((SumPool) secondArray.get(i)).add(4);
}
for (int i = 0; i < size; i++) {
// Notice that we printing the first array.
System.out.println(((SumPool) firstArray.get(i)));
}
}
}
class SumPool {
private float sum;
public SumPool(float x) {
sum = x;
}
public void add(float x) {
sum += x;
}
public String toString() {
return "current sum=" + sum;
}
}
current sum=4.0
current sum=5.0
current sum=6.0
current sum=7.0
current sum=8.0
current sum=9.0
current sum=10.0
current sum=11.0
current sum=12.0
current sum=13.0
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Test {
public static void main(String[] args) {
ArrayList firstArray = new ArrayList();
int size = 10;
for (int i = 0; i < size; i++) {
firstArray.add(new SumPool(i));
}
ArrayList secondArray = (ArrayList) firstArray.clone();
for (int i = 0; i < size; i++) {
try {
secondArray.set(i, ((SumPool) firstArray.get(i)).clone());
} catch (CloneNotSupportedException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
for (int i = 0; i < size; i++) {
((SumPool) secondArray.get(i)).add(4);
}
for (int i = 0; i < size; i++) {
// Notice that we printing the first array.
System.out.println(((SumPool) firstArray.get(i)));
}
}
}
class SumPool implements Cloneable {
private float sum;
public SumPool(float x) {
sum = x;
}
public void add(float x) {
sum += x;
}
public String toString() {
return "current sum=" + sum;
}
@Override
public Object clone() throws CloneNotSupportedException {
return new SumPool(sum);
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.