• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble with a grow method  RSS feed

 
courtney young
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have to implement a grow and shrink method in java using loops but I'm lost! We aren't allowed to use things like .copy or .copyof or anything of that nature. Please help! Any advice on where to head would be great.

 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

The method grow() is supposed to increase the size of the internal array.
As in Java arrays have fixed size, you need to create a new bigger one and copy values from the old one to the new one.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
courtney young wrote:Hi, I have to implement a grow and shrink method in java using loops but I'm lost! We aren't allowed to use things like .copy or .copyof or anything of that nature. Please help! Any advice on where to head would be great.

OK, try this.

You know that what you need to do is take an "old array" of size x and "grow" it to size y; and, as Pawel pointed out, to do that you need to:
1. Create a "new array" of size y.
2. Copy all the elements from "old array".
3. Replace "old array" with "new array".

My advice: Create a new GrowArray class that ONLY contains the things you need: an Array, a "current size", and a grow() method (you may not even need the second of those).

Right now (I suspect) you're distracted by all the "clutter" in your ArrayList class, and you need somewhere 'quiet' to work on this problem. Once you have it written and tested in GrowArray, and you KNOW it works, transfer it to your ArrayList class.

HIH

Winston
 
courtney young
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! I'll try it!
 
courtney young
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For some reason when I try to create a new array I get the error: "Cannot create a generic array of
AnyType"



I'm not sure what I'm doing wrong.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
courtney young wrote:I'm not sure what I'm doing wrong.

You are creating a generic array. You can't do that.
This is a way to create a generic array:By the way, this won't work anyway. Your array field is declared as Object[].
 
courtney young
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I might have got it:

 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is correct.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is an AnyType? There is one class in Java® which might be any type.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see you have sorted out anytype already. Well done

And welcome to the Ranch again
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a name of a generic parameter of OP's ArrayList class.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
courtney young wrote:I think I might have got it:

Looks like it, but it's difficult to tell without seeing the whole class.

And BTW, the convention for generic types is to use a single letter, usually 'T' (for 'type') - but in your case, I might make it 'C', just to remind myself that it's a Comparable type.

Java's ArrayList class actually uses 'E' - for 'entry' (or possibly 'element').

HIH

Winston
 
courtney young
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! I was seriously stressing! (I'm still a beginner) I'm working on the shrink method now which is half the capacity instead of double.

Would it make sense to do:



so it will cut the array in half and put it in a new array or am I just hoping it would have been this simple to create a shrink method after my grow method?
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose capacity is 10 and size is 7.
What will happen?

By the way, I don't see you updating capacity variable.
By the way, is this variable needed at all? Isn't capacity the length of the array? (array.length)
 
courtney young
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Suppose capacity is 10 and size is 7.
What will happen?


Ohhh, I see the problem now.
 
courtney young
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since it's a homework problem my teacher has told us not to modify his code. We are only allowed to add our code to the "to do" portion and main(to test it).

I think I've figure out the first part of shrink:



This is as far as I've got. I'm confused. I know I need another loop here. I'm just not too sure about what needs to happen. Do I need to copy contents again in a new array of the new size?
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can't remove capacity variable, that's fine. Just remember to update it every time you replace the internal array.
You need to do:
1. Check if you need to shrink the array.
2. Calculate new size.
3. Create a new array with that size (update capacity!)
4. Replace the array with the new one.

courtney young wrote:I'm confused. I know I need another loop here.
My advice is: StopCoding (this is a link). Stop guessing. Turn off your computer. Think about the solution (a description, not a code!) and write it down on paper.
 
courtney young
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I've got it.

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carefully consider what you're doing on lines 3, 5, and 7. There's a logic error there.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tested it? I think you have not.
You are guessing again...
On line 3 you are calculating new capacity.
On line 5 you are throwing that calculation away.
In the result the size of the array won't change.
 
courtney young
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right. I just need to breathe and step back and just think. Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!