Why Isn't Arraylist Marked [Obsolete]

Why isn't ArrayList marked [Obsolete]?

I think it should be considered effectively obsolete for new code, but there's no compelling reason to mark it obsolete and create warnings in all code which was written before 2.0 was released.

In my experience, most of the types and members which have been marked obsolete by Microsoft are actively dangerous in some respect, and should really be fixed if you still have a codebase using them. While using ArrayList is painful and (at least theoretically) prone to discovering type-related bugs at execution time rather than compile time, the type does its job well enough... often there's really no compelling reason to change existing code. It's the kind of change I'd generally consider when I already happened to be working on an area of code which was using ArrayList, rather than actively seeking out every usage of it.

Is there any point in using the ArrayList class anymore?

I agree with Daniel.

In addition, however, there is specific performance information from Microsoft on MSDN comparing List<T> and ArrayList that is worth a read.

http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

In deciding whether to use the List or ArrayList class, both of
which have similar functionality, remember that the List class
performs better in most cases and is type safe. If a reference type is
used for type T of the List class, the behavior of the two classes
is identical. However, if a value type is used for type T, you need to
consider implementation and boxing issues.

If a value type is used for type T, the compiler generates an
implementation of the List class specifically for that value type.
That means a list element of a List object does not have to be
boxed before the element can be used, and after about 500 list
elements are created the memory saved not boxing list elements is
greater than the memory used to generate the class implementation.

Make certain the value type used for type T implements the
IEquatable generic interface. If not, methods such as Contains must
call the Object.Equals(Object) method, which boxes the affected list
element. If the value type implements the IComparable interface and
you own the source code, also implement the IComparable generic
interface to prevent the BinarySearch and Sort methods from boxing
list elements. If you do not own the source code, pass an IComparer
object to the BinarySearch and Sort methods

It is to your advantage to use the type-specific implementation of the
List class instead of using the ArrayList class or writing a
strongly typed wrapper collection yourself. The reason is your
implementation must do what the .NET Framework does for you already,
and the common language runtime can share Microsoft intermediate
language code and metadata, which your implementation cannot.

Why is Java Vector (and Stack) class considered obsolete or deprecated?

Vector synchronizes on each individual operation. That's almost never what you want to do.

Generally you want to synchronize a whole sequence of operations. Synchronizing individual operations is both less safe (if you iterate over a Vector, for instance, you still need to take out a lock to avoid anyone else changing the collection at the same time, which would cause a ConcurrentModificationException in the iterating thread) but also slower (why take out a lock repeatedly when once will be enough)?

Of course, it also has the overhead of locking even when you don't need to.

Basically, it's a very flawed approach to synchronization in most situations. As Mr Brian Henk pointed out, you can decorate a collection using the calls such as Collections.synchronizedList - the fact that Vector combines both the "resized array" collection implementation with the "synchronize every operation" bit is another example of poor design; the decoration approach gives cleaner separation of concerns.

As for a Stack equivalent - I'd look at Deque/ArrayDeque to start with.

c# How to add a value into a public arraylist by a function

To be able to add items to an ArrayList, you first have to create instance of ArrayList using new keyword and assign it to the MyPointList property. Otherwise no ArrayList instance exists, MyPointList property contains default value null ("no value") and hence the "Object reference not set to an instance of an object" exception is thrown. You can do this either using property initializer (property will be initialized during StuffIneed instance creation):

public ArrayList MyPointList { get; set; } = new ArrayList();

or in the DataFetcher method:

MyPointList = new ArrayList();
MyPointList.Add(TheObject.Current.EndPoint);

Note that this has nothing to do with MyPointList being declared as get or set - these just let you read (or write) instance of ArrayList to/from the MyPointList property, but they don't affect the ability to call methods (such as .Add()) on the object instance contained in this property.

See also What is a NullReferenceException, and how do I fix it?

Also note that ArrayList class is now mostly considered obsolete and List or similar generic classes are used instead, e.g.:

public List<TSG3D.Point> MyPointList { get; set; } = new List<TSG3D.Point>();

They offer better type safety and saves you the hassle of type-casting object when reading from it.

Java 6 SE are vectors obsolete?

Use of Vectors have been discouraged for some time now. They are replaced by ArrayList, which has more or less the same functionality (but isn't synchronised).

C# Generic inheritance from ArrayList

ArrayList is already generic by its defininition, as it's an array of objects.

There is nothing more generic then object in CLR.

If you mean Generics (<T>), just use List<T>.

Cant split a line

Don't you want to use fileList2.AddRange() instead of fileList2.Add() ?
It seems to me that you are adding one item to the fileList now. That item is an array that contains all items you actually wanted to add to the list. If you get that array first and than use the addRange method, It should be fine.



Related Topics



Leave a reply



Submit