Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!

# Set Capacity of array problem

Gabriel White
Ranch Hand
Posts: 233
Hey Yall, I'm back again. I just can't seem to get this method to work. I really can't figure out a way to decrease the capacity of myArray. I am supposed to change the capacity of this vector to capacity. If the new capacity is smaller than the current size, remove items from the end of this
vector until the size and new capacity are equal.
Here is my main arg calling this method:

And here is my setCapacity method that I have come up with so far:

As you can see I have remarked out a few of my test SOP statements. The loop works and temp stops at capacity 4 and 9 for both int capacity cast in. I am certain that there is something stupid that I am missing or there may be an even better way to accomplish this. I'm open for suggestions.
The output is supposed to look like this:
v[1] = < 2, 3, 4, 5 > (cap: 4)
v[2] = < 3, 2, 1, 0, 1, 2, 3 > (cap: 9)
but it still looks like this:
v[1] = < 2, 3, 4, 5, 6, 7 > (cap: 16)
v[2] = < null, 2, 1, 0, 1, 2, 3 > (cap: 10)

Thanks in advance, and thanks for ALL of your help with everything.
Steve

Gabriel White
Ranch Hand
Posts: 233
Well, I have changed my code for this quite a bit. I now have a different output. But now my conditions have switched. My capacity lists correctly but my array is full of null's and 1 number.
My output should look like this:
v[1] = < 2, 3, 4, 5 > (cap: 4)
v[2] = < 3, 2, 1, 0, 1, 2, 3 > (cap: 9)
now it looks like this:
v[1] = < 2, null, null > (cap: 4)
v[2] = < 3, null, null > (cap: 9)
Here is my code:

I could really use the help on this one.

Gabriel White
Ranch Hand
Posts: 233
any help would be great.
Thanks again.

Loren Rosen
Ranch Hand
Posts: 156
Have you considered using a for loop that counts downwards, e.g.
for (int i=n-1; i>=0; i--)

Gabriel White
Ranch Hand
Posts: 233
Yes, that doesn't work. I think I found the problem.
Thanks anyway
Steve

Gabriel White
Ranch Hand
Posts: 233
nope, didn't find it. Still need some assistance.
Thanks

Gabriel White
Ranch Hand
Posts: 233
Ok, I got it to work....But
I have null's in my output
Here is what my output looks like now:
v[1] = < 2, 3, 4, 5 > (cap: 4)
v[2] = < 3, 2, 1, 0, 1, 2, 3, null, null > (cap: 9)
I would like to get thoes null's out but I have no clue, I'm stretched on this one.

[ January 30, 2004: Message edited by: Steve Wysocki ]

Loren Rosen
Ranch Hand
Posts: 156
What does v[2] look like before you started?