2

I'm working on enhancing the platforms in my company and during my research, I've come across a lot of comments from people saying that ArrayLists should be avoided. However, they never gave much explanation why and I can't find any articles about why they're so awful.

4 Answers4

4

"Arraylists" (also sometimes called "vectors") are perfectly fine data structures; it's just that ArrayList is a class that should be avoided in C#.

The reason is that it isn't generic, so you can store any object inside it, breaking type-safety.
Use List<T> instead.

user541686
  • 205,094
  • 128
  • 528
  • 886
  • This is a good explanation and answer, but a little further clarity is that an ArrayList will allow you to store multiple types of objects in a single ArrayList at the same time. This means you could store string, int, object, etc all in the same ArrayList, so you have to deal with making sure the data type stored is consistent before storage or do type checking on the way out. Either way it is more work for the programmer. Generics are just easier because they do type safety automatically. – Nick Zimmerman Jun 21 '13 at 15:21
  • What if you want multiple types of object in a single list? You're writing an 'Asteroids' game and you want to store all Asteroids, Bullets, Player space ship etc. in a single list of GameObjects? Presumably an arraylist would be ok, and you can run operations by looping through the ArrayList and doing if (ArrayList[i] Is SomeInterface)...? – NibblyPig Jun 21 '13 at 15:22
  • SLC a generic would still be better. Either store it as List GameObjects or create a class structure that provides for common type. See my above comment about avoiding having to manually handle type safety. – Nick Zimmerman Jun 21 '13 at 15:24
  • @SLC In that case you can use `List` or `List`. – Sam Harwell Jun 21 '13 at 15:26
  • Is `List` really any different from `ArrayList`? – NibblyPig Jun 21 '13 at 15:27
  • Is there any impact on performance using ArrayList vs HashSet? – user2460328 Jun 21 '13 at 15:33
  • @user2460328 Depends on whether you need to cast, or box. If List will do the job, then it will perform much better than List or ArrayList with a cast for instance. – Tony Hopkinson Jun 21 '13 at 15:38
  • @TonyHopkinson that was the answer I was looking for! Thank you so much! – user2460328 Jun 21 '13 at 15:41
2

Because they're not strongly typed. They're a bag that can store anything. That means you have to cast when retrieving elements. It also means that there may or may not be boxing/unboxing involved in storing and retrieving values.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • Yes, I get that. What about impact on performance? How does performance of HashSet compare to ArrayList? – user2460328 Jun 21 '13 at 15:19
  • Depends on what you are doing with it. Boxing and unboxing a lot is expensive. – Tony Hopkinson Jun 21 '13 at 15:33
  • @user2460328 You shouldn't compare them at all. A `List` is exactly the same as an `ArrayList` except better. If you want a list-based structure you use a `List`. The question of whether to use a `HashSet` or a `List` (which is a valid one) is entirely separate from this discussion, and is based on the type of data you have and how you plan to use it. – Servy Jun 21 '13 at 15:39
1

They're not generic. A List is exactly the same thing, but it's generic. This means you're ensuring the objects are all of a common type, and that constraint is enforced by the compiler. It also avoids boxing of value types.

Servy
  • 202,030
  • 26
  • 332
  • 449
0

It's not so much awful as painful. Think of them as List, sort of, ish.

So you have to add a lots of rules to enforce consistency in the collection and use them "everywhere" or deal with the errors that result from having no consistency,"everywhere"

So that a lot of extra code, or you cross your fingers real tight and hope no "dimwit" programmer ever puts something in there you can't deal with.

If your collection could consist of anything, then stick with it, though personally I'd say that if it did, there's almost certainly some sort of major design flaw.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39