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); ...

Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.