• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Iterator

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question on a program that I am writing. I need a method which moves the first value to the last position, and then moves the last value to the first spot (switching the two). What would the code look like for it? Will I need an iterator?
 
author
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In what kind of structure are you values held? An array? A List? A LinkedList? If they're in one of those you shouldn't need to use an iterator because you can access the first and last elements directly, without having to iterate through the rest of them.
Perhaps you can try and come up with a solution, and post the code here if you run into problems.
 
Johnaton Jones
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot to mention that the items are stored in a linked list. My original code was:
public Swap(List list)
{
ListNode head = new ListNode(0, null);
ListNode tail = new ListNode(list.getLast(), null);
ListNode temp = new ListNode();

head == temp;
tail == head;
temp == tail;
}
Im not to comfortable with linked lists yet, so this is as far as I got, but I wasn't sure how to go about doing this.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your code example, the syntax is wrong, but I think you've got a good basic idea of how to do the switch.
If the implementation of this swap operation had access to the necessary parts of the list, I'd likely leave the structure of the list in tack, get the data from the head node, get the data from the tail node, put the old head data in the tail node, put the old tail data in the head node, test it and call it done.
So, can you write the code to do the first part, get the data from the head node of the list?
(Note that if you don't have adequate access to the internal parts of the list, then you may have to completely deconstruct it and build a new one, depending on just what methods are available. Hopefully this can be avoided.)
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the Intermediate forum...
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what you want is:

This method of swapping doesn't ever change the number of nodes in the list, it just moves the content of the extreme nodes around. You could also have done it by using List.remove() and List.add(), which actually removes the nodes and recreates new ones, but that would just be wasting cycles unnecessarily.
sev
 
Oh the stink of it! Smell my tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic