Array in constructor and deep copy.
Hello, this is an school homework task, studying over the net and i've got none to ask in my hometown or friends. only gaming wow and bf3.
This is the start of one class and i'm having troubble with it.
1. the arrays seems to be not right. (seems = is)
2. deep copy, the right array shall be one copy of the left array
Code :
class shoes{
private final String brand;
private final String colour;
private final double price;
private final int minSize;
private final int[] left;
private final int[] right;
public shoes(String brand, String colour, double price, int minSize, int[] left, int[] right) {
this.brand = brand;
this.colour = colour;
this.price = price;
this.minSize = minSize;
this.left = new int[] left;
this.right = new int[] right; // thisone shall be one deep copy of the left array.
}
}
I'm not asking for one complete solution, would be nice but i've been trying to search on google for tips. and my school book are of no use either on their examples.
is there some example code or web site i could look on ?
regards
Re: Array in constructor and deep copy.
think i'm on my move:
Code :
public shoes(String brand, String colour, double price, int minSize, int[] left, int[] right) {
this.brand = brand;
this.colour = colour;
this.price = price;
this.minSize = minSize;
this.left = new int[] left;
for(int i=0;i<left.length;i++){
right[i] = left[i];
}
this.right = new int[] right; // thisone shall be one deep copy of the left array.
}
but still getting error message.
Code :
G:\Coding\Skole\Øving 9\tabeller.java:20: error: array dimension missing
this.left = new int[] left;
^
G:\Coding\Skole\Øving 9\tabeller.java:24: error: array dimension missing
this.right = new int[] right; // thisone shall be one deep copy of the left array.
^
2 errors
Tool completed with exit code 1
Re: Array in constructor and deep copy.
See the following for the correct syntax in declaring arrays: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics) - you must define the size of the array when you create it.
To copy the arrays, you need to loop over one and place its values in the second (as your second post demonstrates).