class, toCopy
in
Programming Questions
•
1 year ago
About the toCopy method for this class.
I wrote 2 versions of it.
Is the shorter 2nd version correct? Or do I need instead the 1st longer version?
- //====================
class ArrayInt2 {
//assuming same length arrays
int[] a;
int[] b;
int len; - ArrayInt2(int n) {
a=new int[n];
b=new int[n];
len=n;
}
ArrayInt2(int[] a0, int[] b0) {
//assuming same length (a0, b0)
len=a0.length;
a=new int[len];
b=new int[len];
for (int i=0; i<len; i++) {
a[i]=a0[i];
b[i]=b0[i];
}
}
ArrayInt2(ArrayInt2 ai2) {
int[] a0=ai2.a;
int[] b0=ai2.b;
len=a0.length;
a=new int[len];
b=new int[len];
for (int i=0; i<len; i++) {
a[i]=a0[i];
b[i]=b0[i];
}
}
ArrayInt2 toCopy() {
int[] a0=new int[len];
int[] b0=new int[len];
for (int i=0; i<len; i++) {
a0[i]=a[i];
b0[i]=b[i];
}
ArrayInt2 k=new ArrayInt2(a0,b0);
return k;
}
ArrayInt2 toCopy() {
ArrayInt2 k=new ArrayInt2(a,b);
return k;
}
}//class ArrayInt2
1