Questions tagged [enumeration]

The process of enumerating values, for example from some collection.

Use for enumeration types.

1894 questions
138
votes
10 answers

Difference between Java Enumeration and Iterator

What is the exact difference between these two interfaces? Does Enumeration have benefits over using Iterator? If anyone could elaborate, a reference article would be appreciated.
cometta
  • 35,071
  • 77
  • 215
  • 324
130
votes
7 answers

Singular or plural for enumerations?

Do you use singular or plural for enumerations? I think it makes best sense with plural in the declaration enum Weekdays { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } ... but I think it makes more…
Jan Aagaard
  • 10,940
  • 8
  • 45
  • 80
124
votes
26 answers

Are booleans as method arguments unacceptable?

A colleague of mine states that booleans as method arguments are not acceptable. They shall be replaced by enumerations. At first I did not see any benefit, but he gave me an example. What's easier to understand? file.writeData( data, true…
Thomas Koschel
  • 3,321
  • 9
  • 33
  • 38
120
votes
12 answers

Is there a predefined enumeration for Month in the .NET library?

I'm looking to see if there is an official enumeration for months in the .net framework. It seems possible to me that there is one, because of how common the use of month is, and because there are other such enumerations in the .net framework. For…
Mark Rogers
  • 96,497
  • 18
  • 85
  • 138
118
votes
6 answers

Java Enum Methods - return opposite direction enum

I would like to declare an enum Direction, that has a method that returns the opposite direction (the following is not syntactically correct, i.e, enums cannot be instantiated, but it illustrates my point). Is this possible in Java? Here is the…
Skyler
  • 2,834
  • 5
  • 22
  • 34
112
votes
7 answers

(How) can I count the items in an enum?

This question came to my mind, when I had something like enum Folders {FA, FB, FC}; and wanted to create an array of containers for each folder: ContainerClass*m_containers[3]; .... m_containers[FA] = ...; // etc. (Using maps it's much more…
fuenfundachtzig
  • 7,952
  • 13
  • 62
  • 87
107
votes
11 answers

Intelligent way of removing items from a List while enumerating in C#

I have the classic case of trying to remove an item from a collection while enumerating it in a loop: List myIntCollection = new…
John Stock
  • 1,104
  • 2
  • 9
  • 11
106
votes
10 answers

Iterate enum values using java generics

I'm trying to find a way to iterate through an enum's values while using generics. Not sure how to do this or if it is possible. The following code illustrates what I want to do. Note that the code T.values() is not valid in the following…
Tauren
  • 26,795
  • 42
  • 131
  • 167
105
votes
11 answers

What is the best way to modify a list in a 'foreach' loop?

A new feature in C# / .NET 4.0 is that you can change your enumerable in a foreach without getting the exception. See Paul Jackson's blog entry An Interesting Side-Effect of Concurrency: Removing Items from a Collection While Enumerating for…
Polo
  • 1,770
  • 4
  • 18
  • 29
94
votes
9 answers

Removing from array during enumeration in Swift?

I want to enumerate through an array in Swift, and remove certain items. I'm wondering if this is safe to do, and if not, how I'm supposed to achieve this. Currently, I'd be doing this: for (index, aString: String) in enumerate(array) { //Some…
Andrew
  • 7,693
  • 11
  • 43
  • 81
90
votes
1 answer

What is the BOOL *stop argument for enumerateObjectsUsingBlock: used for?

I've been using enumerateObjectsUsingBlock: a lot lately for my fast-enumeration needs, and I'm having a hard time understanding the usage of BOOL *stop in the enumeration block. The NSArray class reference states stop: A reference to a Boolean…
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
81
votes
9 answers

Distinction between iterator and enumerator

An interview question for a .NET 3.5 job is "What is the difference between an iterator and an enumerator"? This is a core distinction to make, what with LINQ, etc. Anyway, what is the difference? I can't seem to find a solid definition on the net.…
GurdeepS
  • 65,107
  • 109
  • 251
  • 387
81
votes
7 answers

The foreach identifier and closures

In the two following snippets, is the first one safe or must you do the second one? By safe I mean is each thread guaranteed to call the method on the Foo from the same loop iteration in which the thread was created? Or must you copy the reference…
xyz
  • 27,223
  • 29
  • 105
  • 125
81
votes
9 answers

Collection was modified; enumeration operation may not execute in ArrayList

I'm trying to remove an item from an ArrayList and I get this Exception: Collection was modified; enumeration operation may not execute. Any ideas?
Ricardo
  • 11,609
  • 8
  • 27
  • 39
80
votes
7 answers

Iterate an Enumeration in Java 8

Is it possible to iterate an Enumeration by using Lambda Expression? What will be the Lambda representation of the following code snippet: Enumeration nets = NetworkInterface.getNetworkInterfaces(); while (nets.hasMoreElements())…
Tapas Bose
  • 28,796
  • 74
  • 215
  • 331