Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

back and forth in a vector

 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
I have a vector which holds X amount of elements I only am displaying Y amount at a time
with a previous and next button while the next Button works fine i am having problems witht he previous as it returns wierd results
is there a basic algorithim to do this?
Fred
p.s. i cannot use a stack!
 
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What method are you using to get the vector data? Technically, you can obtain any object from the vector using the method Vector.elementAt(int). Implementing an algorithm that displays Y elements starting with element X should not be difficult. If Y=3, the display could conceptually be:

To go forward, increment x by 1. Do go backward, decrement x by one. In each case, you'll have to re-read the objects from the vector. You'll also have to do checks to make sure you don't go off the end of the vector.

Originally posted by Fred Abbot:
hi
I have a vector which holds X amount of elements I only am displaying Y amount at a time
with a previous and next button while the next Button works fine i am having problems witht he previous as it returns wierd results
is there a basic algorithim to do this?
Fred
p.s. i cannot use a stack!


 
Fred Abbot
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have written most of the code just wondering if any one has done anything like this that i can look at
thnak you
 
Fred Abbot
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using the elementat()method
the first time in it starts at 0 and then i do a count of how many are displayed when you hit next it starts at elementAt(counter)
the problem is for the previous i have to do elementAt(counter - amountDisplyed *2 )but that is kind of rediculous
 
Matt Senecal
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't follow your logic here. Why do you have to decrement the counter by twice the number displayed (2Y)? Isn't that going to skip over a set of Y elements when you press the back button?

Originally posted by Fred Abbot:
I am using the elementat()method
the first time in it starts at 0 and then i do a count of how many are displayed when you hit next it starts at elementAt(counter)
the problem is for the previous i have to do elementAt(counter - amountDisplyed *2 )but that is kind of rediculous


 
Fred Abbot
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes but then you will loop through it with ++ to display them
otherWise you have to do -- and then you still went back twice
DUH
 
Matt Senecal
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's with the "DUH" crack? Did it ever occur to you that maybe I'm trying to look at this from your point of view to help you out? You did ask for help in your original post, right?

Originally posted by Fred Abbot:
yes but then you will loop through it with ++ to display them
otherWise you have to do -- and then you still went back twice
DUH


 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please keep it professional, Fred.
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Fred.
I think the reason we're all confused here is because we don't see the need in your design for multiplying by a factor of two, and only for when you reverse through your vector. Perhaps it's an issue with the misuse of pre-fix versus post-fix operators?
For example, Fred, I whipped up this code to demonstrate how you might cycle through objects in a Vector using a "Back" and "Forward" button:

Notice when the "Back" button is clicked, I simply take the greater of 0 or one less than "index". (This prevents a negative index on the Vector "elements".) No doubling necessary. I also wanted to point out that I help prevent the emergence of a negative index by disabling the "Back" button when the index == 0, and similarly disabling "Forward" when index == the maximum index, (MAX - 1).
Maybe it's time we have a look at some of your code to help diagnose your problem?
Art
 
Fred Abbot
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry about that
in any case my code now works and yes i disable the buttons when
there are no more elements in the vector
as far as the *2 issue here goes
if i am displaying 5 elements at a time
the first time i display 1 thru 5 (lets use 1 instaed of 0 so we understand what is happening)
the second time i display 6 - 10
now i hit previous i wan to display 1-5
but i am at element 10 correct?
so i decrement the amout i return that puts me at element 5
now i want to display 1 - 5 so either i can count down(--)until 1
which in effect i decremented the number of elements displayed by 2 as now i am at element 1
so now if i was to hit the next button i would get elements 1-5 which is wrong bec. we would want to see 5 - 10
if you have any questions feel free to ask
as i said i have it working fine now so if you want me to post some code for you to understand better just ask me
once again i am sorry if i offended anyone
thank you
Fred
 
Fred Abbot
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Art Metzer
from looking at your code it looks to me as you are only displaying one element at a time so you will always be at the right element for the next and previous try the same code but display 3 elements at a time see what happens
thank you
Fred
 
Art Metzer
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK.
Now, we're getting somewhere.
So in your original post, that's what you were talking about, "displaying Y amount at a time." I can't speak for everyone else here, but that aspect of your program wasn't clear to me.
I modified my program slightly to accommodate this requirement. New/changed lines are marked with my initials:

I think you were getting confused, Fred, because you may be confusing the usage of your one instance variable to mark your index in the vector. You'll need to decide in your design, will my ONE index represent the first element of the "Y" elements I'm displaying, or the last? Then ripple this decision accordingly through the checks you make at the boundaries (0, MAX) and in your display method(s).
I've done a lot of work for you here, Fred, but you still have some issues to resolve. For example, I assume that "Y" is not a fixed value; if it's not, you'll have to streamline the process that displays "Y" elements at a time. Additionally, in my example, I show twelve elements three at a time; you'll have to determine how you'll handle cases where MAX % ELEMENTS_SHOWN != 0, coming at the boundaries both from the "ground up" and from the "top down". Also, Fred, you may need to add some code to handle the case where MAX < ELEMENTS_SHOWN.
Good luck, Fred. Hang in there.
Art
 
Fred Abbot
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx alot art
 
Because those who mind don't matter and those who matter don't mind - Seuss. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic