Re: array in constructor.
Re: array in constructor.
Quote:
Originally Posted by
scd250
wrong thread. :P
where should i post ?
Re: array in constructor.
Quote:
Originally Posted by
Juukamen
where should i post ?
Here is fine; w.r.t. your problem: you do create a new left array but you're trying to use the 'right' array while you haven't allocated a new array for it. The compiler complains about it.
kind regards,
Jos
Re: array in constructor.
I got 6 variables in the shoes class. only gonna declare 5 of them and the last one will be a copy from the last one. meaning i will declare the left and right will be an copy of the left.
Quite sure that got something with this error todo:
Code:
G:\Coding\Skole\Øving 9\tabeller.java:22: error: variable right might not have been initialized
right[i] = left[i];
and for the client class i got the
Code:
shoes test1 = new shoes("Nike", "Black", 300, 35,30);
whom is arguing with me on this error.
Code:
shoes test1 = new shoes("Nike", "Black", 300, 35,30);
^
[CODE] required: String,String,int,int,[B]int[][/B]
found: String,String,int,int,[B]int[/B]
reason: actual argument int cannot be converted to int[] by method invocation conversion
3 errors
[/CODE]
where i can understand i've done something wrong in my shoes constructor.
Code:
public shoes(String brand, String colour, int price, int minSize, [B]int[] left[/B])
Re: array in constructor.
I commented the code in your constructor:
Code:
public shoes(String brand, String colour, int price, int minSize, int[] left) {
this.brand = brand; // set the simple member variables
this.colour = colour; // ditto
this.price = price; // ditto
this.minSize = minSize; // ditto
this.left = new int[left.length]; // a new member array 'left'
for(int i=0;i<left.length;i++) {
right[i] = left[i]; // there is no new member array 'right' yet ...
}
// too late:
this.right = new int[right.length]; // thisone shall be one deep copy of the left array.
}
kind regards,
Jos
Re: array in constructor.
Quote:
Originally Posted by
JosAH
I commented the code in your constructor:
Code:
public shoes(String brand, String colour, int price, int minSize, int[] left) {
this.brand = brand; // set the simple member variables
this.colour = colour; // ditto
this.price = price; // ditto
this.minSize = minSize; // ditto
this.left = new int[left.length]; // a new member array 'left'
for(int i=0;i<left.length;i++) {
right[i] = left[i]; // there is no new member array 'right' yet ...
}
// too late:
this.right = new int[right.length]; // thisone shall be one deep copy of the left array.
}
kind regards,
Jos
Code:
public shoes(String brand, String colour, int price, int minSize, int[] left) {
this.brand = brand;
this.colour = colour;
this.price = price;
this.minSize = minSize;
this.left = new int[left.length];
this.right = new int[right.length];
for(int i=0;i<left.length;i++){
right[i] = left[i];
}
moved the Code:
this.right = new int[right.length];
just above the copy of the left.
Code:
G:\Coding\Skole\Øving 9\tabeller.java:21: error: variable right might not have been initialized
this.right = new int[right.length];
can that be a reason since it's not in the constructor Code:
public shoes(String brand, String colour, int price, int minSize, int[] left)
Re: array in constructor.
What is right.length supposed to mean if there is no array 'right' yet?
kind regards,
Jos
Re: array in constructor.
Right. In your constructor you're passing in some variables. {String brand, String colour, int price, int minSize, int[] left}. Nowhere in there are you passing in an array called right. And in your fields, the array "right[]" has no length! So you're initialising an array with no length. The code doesn't throw a semantic error because theoretically there's nothing wrong with it, but when you try to run it, you're attempting to use an array with no memory locations:
Code:
public shoes(String brand, String colour, int price, int minSize, int[] left) {
// At this point we have an actual parameter, which is an array of integers called left.
this.brand = brand;
this.colour = colour;
this.price = price;
this.minSize = minSize;
this.left = new int[left.length]; // You won't have a copy of your array. You'll have an empty array, with the same amount of memory as the array you've passed in.
this.right = new int[right.length]; // This array has been initialized with no length.
for(int i=0;i<left.length;i++){
right[i] = left[i]; // This is illegal. right[i] does not exist, and left[i] has no value.
}
What you should try is this:
Code:
this.left = left; // Now your field array, left, has the same value as the array passed in.
then i'm not telling you the rest, because this looks liek a homework assignment.
Then you can soldier on with the for loop and populating the array to make your deep copy.
I strongly, strongly suggest you go and read a few tutorials too avoid something like this happening in the future :)
Re: array in constructor.
indeed christopherx it's schoolwork. that easy to sniff out heh =) and as i've said, i've been trying to search for answers and my book, seems to be piss poor on this case. wanna murder my teacher or the writters of the book for bad task/book :D
Is there any special sites that you want to reccomend ? I do have the new boston tutorials downloaded and will go trough them all from 1 to 84 when i'm done with the exercises since right, got 3 more to come. and then i'm up for the exam in the spring. time to prepare then :D
Back to the task at hand.
Constructor Code:
public shoes(String brand, String colour, int price, int minSize, int[] left, int[] right)
and then the right is declared. but the client will not be giving the right varibale, only the left one. since it should not be needfull to say that you got 30 left shoes and maybe 31 left shoes ? that's just wrong.
So that is the reason for the copy from left to the right.
gunna send this code in for my teacher and get her comments on it by tomorrow. I'm prolly gonna get tossed out of the course ^^
Re: array in constructor.
Eeee no. You've missed what I meant mate! by the way, you use British slang. GO UK. Okay, "back to the task at hand". You're right, having an array of right shoes being passed too you is inefficient, as the shoes come in pairs so it's daft. So all you need to do is, as I said initialize the right array with the same length as the "left" array. That way you've got two lovely arrays.
The deep copying is also very simple, and I've even got an example of it in the slight change to your code that I suggested. I'm really doing my best to not tell you the answer, because i'd feel bad for stealing that sense of acheivement you get when you work out something for yourself, but your original constructor parameters were okay. Just think how you can create right from the data and the length of left.
Re: array in constructor.
sorry for the language slangs. I'm norwegian so that might not help the case.
are you thinking something like Code:
public shoes(String brand, String colour, int price, int minSize, int[] left){
this.brand = brand;
this.colour = colour;
this.price = price;
this.minSize = minSize;
this.left = left;
this.right = new int[left.length];
for(int i=0;i<left.length;i++){
right[i] = left[i];
}