wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Help with adding elements to vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with adding elements to vector" Watch "Help with adding elements to vector" New topic
Author

Help with adding elements to vector

Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Hey Yall, just wondering if you could spare some time with this question. I am trying to insert obj after the final element in this vector. The code is pretty long so I will just insert the relevant stuff. I just don't know how to increase the capacity of the vector so I can add in the obj that is coming in my add(Object obj) method.(it is at the bottom of the ArrayVector class)
The output is supposed to be like this:
v[1] = < 2, 3, 4, 5, 6, 7 > (cap: 8)
v[2] = < 3, 2, 1, 0, 1, 2, 3 > (cap: 10)
I'm not worried about the physical appearance of the output, because that is handled elsewhere and it works fine. But instead of the above output I get:
v[1] = empty vector (cap: 4)
v[2] = empty vector (cap: 5)
Anyway, here is my code for my add method and the main arg.

When I insert the SOP in the add method, myArray[i] and obj both are showing the same values. So at least I know that they are being passed in, but they are not getting added to the empty vector.
Thanks in advance
Steve
[ January 26, 2004: Message edited by: Steve Wysocki ]
[ January 26, 2004: Message edited by: Steve Wysocki ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Why are you both extending Vector and implementing its functionality yourself? I'd think you'd do one or the other, not both. The Vector already has an array of elements inside it, and this is separate from your myArray object. It's going to be extremely confusing if these get mixed up. I don't want to address our other questions without a better understanding of what you're trying to do here, as it's too easy to give bad advice.


"I'm not back." - Bill Harding, Twister
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Hi Jim, sorry to be so confusing, and thank you for your help. Basically the output just needs to look like this:
v[1] = < 2, 3, 4, 5, 6, 7 > (cap: 8)
v[2] = < 3, 2, 1, 0, 1, 2, 3 > (cap: 10)
and notice that my original capacity in my empty vectors is:
v[1] = empty vector (cap: 4)
v[2] = empty vector (cap: 5)
This is the code that I am supposed to use (cannot change this) This code is in the main method. It calls the add method from my ArrayVector class.


I need to come up with an add method that will change my capactiy as shown above and add the new numbers into it.
I can post the entire code, but it is much too long. Therefore I only posted the relevant information regarding this question. I did not post the entire code. Notice that the //3. add only calls the add method in the ArrayVector class. This is all I need to work with.
Right now this is all I have for the add method:

Thanks again for your help Jim.
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
Steve:
Here's a start, w/out giving too much away. You basically need to do a couple of things:
1) Check to see if there is room for the new element (myCnt < myArray.length).
2) If the array is full, then you need to: (a) reallocate the array, making it bigger; (b) copy the elements from the current array into the new array; and (c) reassign "myArray" to the new array. "myCnt" shouldn't change.
3) At this point you know the array is large enough, so put the new element into the array at "myCnt".
4) Increment "myCnt".
If there is room in the array, there is no need to loop through the current array elements.
Since you have "implements Vector" I assume that this is not the "java.util.Vector", since that is a class and not an interface, and can be extended but not implemented. Is it your own class, or one that you have to use for this assignment?
[ January 26, 2004: Message edited by: Wayne L Johnson ]
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Hi Wayne, thanks for the tips. Yes, this is an assignment.
3) At this point you know the array is large enough, so put the new element into the array at "myCnt".

what do you mean by put the new element into the array at myCt? You mean myArray[myCt]=obj; ??? Plus myCt is zero.
And as far as checking the array capacity, how is this?


Thanks again
[ January 26, 2004: Message edited by: Steve Wysocki ]
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
You are correct in your assignment:

Keep in mind that Java array indexes are zero-based, so if you have an array of length 10, you can uses indexes from zero to nine, inclusive. In general: myArray[0] ... to ... myArray[myArray.length-1] are the lower and upper limits.
As for you "ensureCapacity()" method, I didn't compile it, but just looking over it it seems to have all the right stuff. There is an "arrayCopy()" method in the "java.lang.System" class that could be used instead of the "for" loop, but either one would work fine, as long as you understand what's happening.
Good luck!
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Got it Wayne, and Jim. Here is the final code. The key was to add one to myCt because for every object coming into the add method myCt had to goto ensureCapacity and add 1 so that it would be greater than myArray.length at some portion.
Here is the code:
Thanks again for the help.
Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with adding elements to vector
 
Similar Threads
problem with indexing an array
Set Capacity of array problem
Arrays doubt
array copy question