iterator previous() add()
-
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
-
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
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.
-
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.
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 theList
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
-
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 theList
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
-
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 theList
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
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.
-
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.
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
-
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).
right, sort and order might be a bit mixed up.
regards Torsten When I'm not working
-
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
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?
-
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?
no, that is your thought. One has a
List
of complexObjects
(e.g. cars) A newObject
should be added to theList
. Position refers to an specified argument of theObjects
(e.g. power of car). It is easier to add the object to the end of theList
and then to sort theList
with a simple, fixedComparator
. I don't know what you are coding, but I have seldom simple types in myCollections
.regards Torsten When I'm not working
-
no, that is your thought. One has a
List
of complexObjects
(e.g. cars) A newObject
should be added to theList
. Position refers to an specified argument of theObjects
(e.g. power of car). It is easier to add the object to the end of theList
and then to sort theList
with a simple, fixedComparator
. I don't know what you are coding, but I have seldom simple types in myCollections
.regards Torsten When I'm not working
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.