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 previous() add()

iterator previous() add()

Scheduled Pinned Locked Moved Java
tutorial
14 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.
  • T TorstenH

    why are you using an iterator? it's a List. list.add(value); should work fine.

    regards Torsten When I'm not working

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

    thanks for ur reply yes that works fine but how do i go to previous once an element is added. i get an unhandled exception checkforcomodification pritha

    T 1 Reply Last reply
    0
    • P prithaa

      thanks for ur reply yes that works fine but how do i go to previous once an element is added. i get an unhandled exception checkforcomodification pritha

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

      Why do you step back? ConcurrentModificationException is thrown when more than one action is taking place at the same time on the List. A read access and an (still running) asynchronous write access is probably to much at a time. A List is not a ordered thing. You will have to sort when you want a certain lineup of values - but there are better types when you are looking for a ordered list. The question must be - do you really need a ordered list or do you just want to store values?

      regards Torsten When I'm not working

      P D 2 Replies Last reply
      0
      • T TorstenH

        Why do you step back? ConcurrentModificationException is thrown when more than one action is taking place at the same time on the List. A read access and an (still running) asynchronous write access is probably to much at a time. A List is not a ordered thing. You will have to sort when you want a certain lineup of values - but there are better types when you are looking for a ordered list. The question must be - do you really need a ordered list or do you just want to store values?

        regards Torsten When I'm not working

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

        thank you for a wonderful explanation

        1 Reply Last reply
        0
        • T TorstenH

          Why do you step back? ConcurrentModificationException is thrown when more than one action is taking place at the same time on the List. A read access and an (still running) asynchronous write access is probably to much at a time. A List is not a ordered thing. You will have to sort when you want a certain lineup of values - but there are better types when you are looking for a ordered list. The question must be - do you really need a ordered list or do you just want to store values?

          regards Torsten When I'm not working

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

          TorstenH. wrote:

          A List is not a ordered thing.

          Yes, it is. This is how the javadoc for java.util.List defines a List:

          An ordered collection (also known as a sequence).

          The fact that a List is ordered is what distinguishes it from other collection types such as a Bag (although Java does not actually have a Bag type out of the box). I ran a little test to repeat the OP's example using ArrayList and it worked perfectly, exactly as expected, no exception thrown. So I think you are right, there is something else going on here, probably to do with threads - that's typically where you see ConcurrentModification, where one thread is reading the list and another thread is updating it.

          T 1 Reply Last reply
          0
          • D David Skelly

            TorstenH. wrote:

            A List is not a ordered thing.

            Yes, it is. This is how the javadoc for java.util.List defines a List:

            An ordered collection (also known as a sequence).

            The fact that a List is ordered is what distinguishes it from other collection types such as a Bag (although Java does not actually have a Bag type out of the box). I ran a little test to repeat the OP's example using ArrayList and it worked perfectly, exactly as expected, no exception thrown. So I think you are right, there is something else going on here, probably to do with threads - that's typically where you see ConcurrentModification, where one thread is reading the list and another thread is updating it.

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

            Right, a List might be ordered in some way - The objects are placed as they come in. I don't consider that as a real order as that is only the time relevant order in which the List is filled. So Strings e.g. would be in there non alphabetical, numbers, dates and so would be mixed up. To get a real order according to what you expect/want the objects to be in the list you need to sort them: Object Ordering @ Java Tutorials[^]

            regards Torsten When I'm not working

            B D 2 Replies Last reply
            0
            • T TorstenH

              Right, a List might be ordered in some way - The objects are placed as they come in. I don't consider that as a real order as that is only the time relevant order in which the List is filled. So Strings e.g. would be in there non alphabetical, numbers, dates and so would be mixed up. To get a real order according to what you expect/want the objects to be in the list you need to sort them: Object Ordering @ Java Tutorials[^]

              regards Torsten When I'm not working

              B Offline
              B Offline
              BobJanova
              wrote on last edited by
              #8

              You're using 'ordered' to mean 'sorted' there which is rather confusing, as a list does indeed preserve the order of items and therefore to me is indeed 'ordered' (unlike, say, a map).

              T 1 Reply Last reply
              0
              • T TorstenH

                Right, a List might be ordered in some way - The objects are placed as they come in. I don't consider that as a real order as that is only the time relevant order in which the List is filled. So Strings e.g. would be in there non alphabetical, numbers, dates and so would be mixed up. To get a real order according to what you expect/want the objects to be in the list you need to sort them: Object Ordering @ Java Tutorials[^]

                regards Torsten When I'm not working

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

                TorstenH. wrote:

                To get a real order according to what you expect/want the objects to be in the list you need to sort them

                No, I don't. I can add items to the list at any position I want. One way to do this is to use the overloaded add method, which allows me to specify the index position at which I want to add the item. This allows me to add items to the list in any arbitrary order I want, even if it does not correspond to a natural sort order. For example, I can add numbers in the order 3, 1, 4, 1, 9 which are the digits of pi in sequence. But wait! I hear you cry, that's not right. So now I go back and add the digit 5 into my list at index 4 to make 3, 1, 4, 1, 5, 9. If I add the number at the end, it's wrong. If I sort it, it's wrong. I have to add it into the correct position to get the order I want and expect.

                T 1 Reply Last reply
                0
                • D David Skelly

                  TorstenH. wrote:

                  To get a real order according to what you expect/want the objects to be in the list you need to sort them

                  No, I don't. I can add items to the list at any position I want. One way to do this is to use the overloaded add method, which allows me to specify the index position at which I want to add the item. This allows me to add items to the list in any arbitrary order I want, even if it does not correspond to a natural sort order. For example, I can add numbers in the order 3, 1, 4, 1, 9 which are the digits of pi in sequence. But wait! I hear you cry, that's not right. So now I go back and add the digit 5 into my list at index 4 to make 3, 1, 4, 1, 5, 9. If I add the number at the end, it's wrong. If I sort it, it's wrong. I have to add it into the correct position to get the order I want and expect.

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

                  and how do you determine what is missing or what you're searching for? I guess one should sort to get the right order.

                  regards Torsten When I'm not working

                  D 1 Reply Last reply
                  0
                  • B BobJanova

                    You're using 'ordered' to mean 'sorted' there which is rather confusing, as a list does indeed preserve the order of items and therefore to me is indeed 'ordered' (unlike, say, a map).

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

                    right, sort and order might be a bit mixed up.

                    regards Torsten When I'm not working

                    1 Reply Last reply
                    0
                    • T TorstenH

                      and how do you determine what is missing or what you're searching for? I guess one should sort to get the right order.

                      regards Torsten When I'm not working

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

                      Are you saying that the correct solution is always to add to the end of a list and then sort it? That inserting into a specific index position within a list is always wrong?

                      T 1 Reply Last reply
                      0
                      • D David Skelly

                        Are you saying that the correct solution is always to add to the end of a list and then sort it? That inserting into a specific index position within a list is always wrong?

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

                        no, that is your thought. One has a List of complex Objects(e.g. cars) A new Object should be added to the List. Position refers to an specified argument of the Objects (e.g. power of car). It is easier to add the object to the end of the List and then to sort the List with a simple, fixed Comparator. I don't know what you are coding, but I have seldom simple types in my Collections.

                        regards Torsten When I'm not working

                        D 1 Reply Last reply
                        0
                        • T TorstenH

                          no, that is your thought. One has a List of complex Objects(e.g. cars) A new Object should be added to the List. Position refers to an specified argument of the Objects (e.g. power of car). It is easier to add the object to the end of the List and then to sort the List with a simple, fixed Comparator. I don't know what you are coding, but I have seldom simple types in my Collections.

                          regards Torsten When I'm not working

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

                          It may be easier to add at the end of the list, but that doesn't mean it's more efficient. I'm guessing you've never heard of an insertion sort algorithm.

                          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