• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with adding elements to vector

 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic