Results 1 to 3 of 3
  1. #1
    DerekRaimann is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default Iterators - can you assign new values to them?

    I know an Iterator object has the hasNext(), next(), and remove() methods, but is there a way to assign a different value to an Iterator object as you iterate through? For instance:

    Java Code:
    Vector v = new Vector();
    v.add(1);
    v.add(2);
    v.add(3);
    
    for (Iterator i = v.iterator; i.hasNext(); ) {
         // set current i value to its value times 10
    }
    What would be the code to replace the comment line in the preceding code without having to use the set() method of the Vector object itself on the v vector? Thanks for your help!

    -Derek Raimann

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    you could use the ListIterator!
    Java Code:
    		for (ListIterator<Integer> i = v.listIterator(); i.hasNext(); ) {
    		    i.set(i.next()*10);
    		}

  3. #3
    DerekRaimann is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    Excellent! Thanks for your help, got it working just as you showed me! :)

    -Derek Raimann

Similar Threads

  1. Assign Different Objects to Array
    By Briksins in forum New To Java
    Replies: 17
    Last Post: 12-08-2010, 10:44 AM
  2. Iterator over Iterators
    By chawlakunal in forum New To Java
    Replies: 2
    Last Post: 05-22-2010, 10:16 PM
  3. Assign it, or call it again,, (again!)
    By sonny in forum New To Java
    Replies: 4
    Last Post: 03-17-2010, 03:47 AM
  4. Array Assign Values from a Textfile
    By fawadafr in forum Java Applets
    Replies: 6
    Last Post: 11-30-2008, 01:10 AM
  5. Assign a keyboard key to a JButton.
    By gszauer in forum AWT / Swing
    Replies: 1
    Last Post: 12-15-2007, 11:42 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •