O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

19.1. ArrayList

An ArrayList encapsulates an ordered list of objects, much like Java's Vector or ArrayList classes. Each ArrayList object has an initial capacity (accessible via the public property Capacity) which automatically expands as more elements are inserted into the ArrayList object.

The example below shows some common methods which you can use with ArrayList.

 1: using System;
 2: using System.Collections;
 3:
 4: public class TestClass{
 5:   public static void Main(){
 6:
 7:     String item1 = "apple",
 8:            item2 = "orange",
 9:            item3 = "banana",
10:            item4 = "durian",
11:            item5 = "strawberry";
12:
13:     ArrayList al = new ArrayList();
14:
15:     // Use of Add. New elements are inserted behind. 16: al.Add(item1); 17: al.Add(item2); 18: al.Add(item3); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required