Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. iterator problem

iterator problem

Scheduled Pinned Locked Moved Java
databasehelpquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    prithaa
    wrote on last edited by
    #1

    List<OrderedPair> list1 = new ArrayList<OrderedPair>() ;

    	OrderedPair e0  = new OrderedPair(); e0.index=0; e0.value=1;
    	OrderedPair e1  = new OrderedPair(); e1.index=2; e1.value=2;
    	OrderedPair e2  = new OrderedPair(); e2.index=3; e2.value=3;
    	
    	ListIterator<OrderedPair> iterator = list1.listIterator();
    	iterator.add(e0);
    	iterator.add(e1);
    	iterator.add(e2);
    	
    	OrderedPair op1 = (OrderedPair) iterator.previous();
    	System.out.print("index = "); System.out.print(op1.index);
    	System.out.print(", value = "); System.out.print(op1.value);
    	System.out.println();		
    	
    	OrderedPair op2 = (OrderedPair) iterator.previous();
    	System.out.print("index = "); System.out.print(op2.index);
    	System.out.print(", value = "); System.out.print(op2.value);
    	System.out.println();	
    	
    	OrderedPair op3 = (OrderedPair) iterator.previous();
    	System.out.print("index = "); System.out.print(op3.index);
    	System.out.print(", value = "); System.out.print(op3.value);
    	System.out.println();		
    

    Above is the code block which works fine on one system but the previous() method on some other system doesnt move the iterator backwards and results in the same object . Any idea why such a behaviour? Pritha

    T 1 Reply Last reply
    0
    • P prithaa

      List<OrderedPair> list1 = new ArrayList<OrderedPair>() ;

      	OrderedPair e0  = new OrderedPair(); e0.index=0; e0.value=1;
      	OrderedPair e1  = new OrderedPair(); e1.index=2; e1.value=2;
      	OrderedPair e2  = new OrderedPair(); e2.index=3; e2.value=3;
      	
      	ListIterator<OrderedPair> iterator = list1.listIterator();
      	iterator.add(e0);
      	iterator.add(e1);
      	iterator.add(e2);
      	
      	OrderedPair op1 = (OrderedPair) iterator.previous();
      	System.out.print("index = "); System.out.print(op1.index);
      	System.out.print(", value = "); System.out.print(op1.value);
      	System.out.println();		
      	
      	OrderedPair op2 = (OrderedPair) iterator.previous();
      	System.out.print("index = "); System.out.print(op2.index);
      	System.out.print(", value = "); System.out.print(op2.value);
      	System.out.println();	
      	
      	OrderedPair op3 = (OrderedPair) iterator.previous();
      	System.out.print("index = "); System.out.print(op3.index);
      	System.out.print(", value = "); System.out.print(op3.value);
      	System.out.println();		
      

      Above is the code block which works fine on one system but the previous() method on some other system doesnt move the iterator backwards and results in the same object . Any idea why such a behaviour? Pritha

      T Offline
      T Offline
      TorstenH
      wrote on last edited by
      #2

      does that throw an exception? (NuSuchElementException) Why do you add via the iterator? you can add directly to the ArrayList. It might also be useful to OOP that code.

      regards Torsten When I'm not working

      P D 2 Replies Last reply
      0
      • T TorstenH

        does that throw an exception? (NuSuchElementException) Why do you add via the iterator? you can add directly to the ArrayList. It might also be useful to OOP that code.

        regards Torsten When I'm not working

        P Offline
        P Offline
        prithaa
        wrote on last edited by
        #3

        thanks for your reply it doesnt throw an exception. The problem is it works on one system and doesnt work on another. should i reinstall eclipse or java libraries. thanks

        T 1 Reply Last reply
        0
        • P prithaa

          thanks for your reply it doesnt throw an exception. The problem is it works on one system and doesnt work on another. should i reinstall eclipse or java libraries. thanks

          T Offline
          T Offline
          TorstenH
          wrote on last edited by
          #4

          check the runtime, there might be the difference.

          regards Torsten When I'm not working

          P 1 Reply Last reply
          0
          • T TorstenH

            check the runtime, there might be the difference.

            regards Torsten When I'm not working

            P Offline
            P Offline
            prithaa
            wrote on last edited by
            #5

            thanks for the reply check the runtime what does it mean ?

            T 1 Reply Last reply
            0
            • P prithaa

              thanks for the reply check the runtime what does it mean ?

              T Offline
              T Offline
              TorstenH
              wrote on last edited by
              #6

              the java runtime. There is a java runtime on each machine. Those might be different versions and also 32bit or 64bit, which also can be acting different. Open a command prompt and type java -version. Latest version is V1.6.31, also good to go is V1.6.30. Version 1.6.29 hat a problem with the FileDialog under SWT/Jface.

              regards Torsten When I'm not working

              P L 2 Replies Last reply
              0
              • T TorstenH

                the java runtime. There is a java runtime on each machine. Those might be different versions and also 32bit or 64bit, which also can be acting different. Open a command prompt and type java -version. Latest version is V1.6.31, also good to go is V1.6.30. Version 1.6.29 hat a problem with the FileDialog under SWT/Jface.

                regards Torsten When I'm not working

                P Offline
                P Offline
                prithaa
                wrote on last edited by
                #7

                thanks

                1 Reply Last reply
                0
                • T TorstenH

                  does that throw an exception? (NuSuchElementException) Why do you add via the iterator? you can add directly to the ArrayList. It might also be useful to OOP that code.

                  regards Torsten When I'm not working

                  D Offline
                  D Offline
                  David Skelly
                  wrote on last edited by
                  #8

                  TorstenH. wrote:

                  Why do you add via the iterator? you can add directly to the ArrayList.

                  Why are you so insistent on telling this guy how to build his application when you have no idea what his requirements are? There are perfectly valid reasons to insert via a ListIterator. That's why the method exists on the interface.

                  1 Reply Last reply
                  0
                  • T TorstenH

                    the java runtime. There is a java runtime on each machine. Those might be different versions and also 32bit or 64bit, which also can be acting different. Open a command prompt and type java -version. Latest version is V1.6.31, also good to go is V1.6.30. Version 1.6.29 hat a problem with the FileDialog under SWT/Jface.

                    regards Torsten When I'm not working

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    check your development Java runtime envriment,exp:JDk1.5 hava the Iterator interface,but before this version,have no Iterator interface.I test the programe throught JRE1.6 of my dev computor,the follow is the message of console: aaaa index = 3, value = 3 index = 2, value = 2 index = 0, value = 1

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups