• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

arrayList iterator problems  RSS feed

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

With a little help I've finally managed to add an arrayList to my swing program. The problem is now with iteration through this list. When I try to go through the list, it simply jumps straight to the beginning or end of the list, depending on whether I click the backwards or forwards button.

The arrayList is printing out correctly, so I'm guessing the problem must be with the code for the iteration methods.

My code so far is as follows,


I can't seem to see the problem. Any advice or help appreciated.

Many thanks.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this part of your code:

The while loop will continue until you get the beginning of the list. Instead use an if in place of the while statement:

and it will only execute once if the condition is true.
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the reply. That works correctly for one click of the backwards button, but it will not go back beyond one position.

Any ideas what the problem might be?

Many thanks for the help.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thinking of this problem a little more, I'm not sure if an Iterator is the best way to go. Maybe if you defined a class level (or less if appropriate) int variable


Then if you want to go forward you could put this int your method

[ January 28, 2006: Message edited by: Garrett Rowe ]
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've tried that solution. The problem is that by setting currentPosition to 0, for some reason it is ignoring the arrayList, and starting at 0. It then increments the value from 1 upwards and does not go through the arrayList.

Maybe there's a way to link this to the arrayList, but I'm getting really confused at the moment.

Anybody got any ideas?

Thanks.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post some code to show us what you tried. Garrett's suggestion should work, if you implement it properly. We need to see your code in order to help you figure out what you did differently.

Layne
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you don't need an iterater.

just keep a 'positionCounter', it's initial value will be 0;

in the constructor, disable the back button

the 'back' actionPerformed
positionCounter--
textField.setText(list.get(positionCounter)+"")
forwardButton enable
if(positionCounter == 0) backButton disable

the 'forward' actionPerformed
positionCounter++
textField.setText(list.get(positionCounter)+"")
backButton enable
if(positionCounter == list.size()-1) forwardButton disable
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the reply. I have tried implementing the actionPerformed methods as suggested above, and I get a really weird result. It outputs an error saying 'ArrayIndexOutofBoundsException: -1'. Any ideas why this is happening?

My current is as follows,


I really can't see where the problem is with these methods. It's all very strange.

Any help or advice really appreciated at this stage.

[ January 29, 2006: Message edited by: celine scarlett ]
[ January 29, 2006: Message edited by: celine scarlett ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont see the error myself. One thought would be to make sure that when the current index is 0, the back button is always disabled. And anytime you add a new value to the ArrayList, the current index should be set to (theList.size - 1).
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've managed to get the buttons to alternate between decrementing and incrementing the index values. However, they only jump backwards and forwards to the first and last values in the list.

eg: a list contains 22,32,42,52 and a user presses the backwards button, it will go back to 22 and not 42 from 52 and vice versa

Current code for the two actionPerformed methods is as follows,

I've checked the actionPerformed method for the convert button, but I don't see how that would affect the other buttons. Anyway, the code for that method is as follows,


Any ideas what is causing this problem?

Thanks for the help!
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

With alot of help, I've managed to get the actionPerformed methods of the backward and forward buttons going through the arrayList correctly, with the exception of one small problem.

When a user enters a sequence of numbers eg: 22,32,42,52 and then presses the back button, they are taken back to 22, instead of 52. Then the user has to press the forward button. From this point on, the buttons work perfectly, and go backwards and forwards through the list.

Does anybody know why this error might be occurring?

My current code for the actionPerformed methods is as follows,


Any help or advice appreciated!
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java collections are poorly implemented as you are observing.
I sympathise with your frustrations and assure you that you are not entirely at fault for being confused.

Use a Sequence and a BidirectionalIterator. Still broken (ala Java), but simpler from a client perspective. ContractualJ
[ January 29, 2006: Message edited by: Tony Morris ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this section of code.

When you add a new value to the list, it is added to the end of the list (which means you are now at the end of the list). The currentPosition variable should be updated to reflect this fact.


[ January 29, 2006: Message edited by: Garrett Rowe ]
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

That's brilliant. Thank you so much for your help. I now have working backwards and forwards buttons.

It's been a long weekend, but I've finally got there.

Have a great week!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!