illustrates the use of an ArrayList that contains objects of the Car class : ArrayList « Collections Data Structure « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Collections Data Structure » ArrayListScreenshots 
illustrates the use of an ArrayList that contains objects of the Car class
illustrates the use of an ArrayList that contains objects of the Car class
  
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example11_3.cs illustrates the use of an ArrayList that contains
  objects of the Car class
*/

using System;
using System.Collections;


// declare the Car class
class Car : IComparable
{

  // declare the fields
  public string model;
  public int yearBuilt;

  // define the constructor
  public Car(string model, int yearBuilt)
  {
    this.model = model;
    this.yearBuilt = yearBuilt;
  }

  // override the ToString() method
  public override string ToString()
  {
    return "model is " + model + ", yearBuilt is " + yearBuilt;
  }

  // implement the Compare() method of IComparer
  public int Compare(object lhs, object rhs)
  {
    Car lhsCar = (Carlhs;
    Car rhsCar = (Carrhs;
    if (lhsCar.yearBuilt < rhsCar.yearBuilt)
    {
      return -1;
    }
    else if (lhsCar.yearBuilt > rhsCar.yearBuilt)
    {
      return 1;
    }
    else
    {
      return 0;
    }
  }

  // implement the CompareTo() method of IComparable
  public int CompareTo(object rhs)
  {
    return Compare(this, rhs);
  }

  // alternative CompareTo() method that simply calls the
  // CompareTo() method that comes with the int type
  // (currently commented out)
  /* public int CompareTo(object rhs)
  {
    Car rhsCar = (Car) rhs;
    return this.yearBuilt.CompareTo(rhsCar.yearBuilt);
  }*/

}


public class Example11_3
{

  // the DisplayArrayList() method displays the elements in the
  // supplied ArrayList
  public static void DisplayArrayList(
    string arrayListName, ArrayList myArrayList
  )
  {
    for (int counter = 0; counter < myArrayList.Count; counter++)
    {
      Console.WriteLine(arrayListName + "[" + counter + "] = " +
        myArrayList[counter]);
    }
  }

  public static void Main()
  {

    // create an ArrayList object
    ArrayList myArrayList = new ArrayList();

    // add four Car objects to myArrayList using the Add() method
    Console.WriteLine("Adding four Car objects to myArrayList");
    Car myMR2 = new Car("MR2"2001);
    Car myBoxster = new Car("Boxster"2001);
    Car myCorvette = new Car("Corvette"1999);
    Car myThunderbird = new Car("Thunderbird"1979);
    myArrayList.Add(myMR2);
    myArrayList.Add(myBoxster);
    myArrayList.Add(myCorvette);
    myArrayList.Add(myThunderbird);
    DisplayArrayList("myArrayList", myArrayList);

    // use the Contains() method to determine if myBoxster
    // is in the ArrayList; if it is, then use the IndexOf()
    // method to display the index
    if (myArrayList.Contains(myBoxster))
    {
      Console.WriteLine("myArrayList does contain myBoxster");
      int index = myArrayList.IndexOf(myBoxster);
      Console.WriteLine("myBoxster occurs at index " + index);
    }

    // remove myBoxster from myArrayList
    Console.WriteLine("Removing myBoxster from myArrayList");
    myArrayList.Remove(myBoxster);
    DisplayArrayList("myArrayList", myArrayList);

    // use the Sort() method to sort myArrayList
    Console.WriteLine("Using the Sort() method to sort myArrayList");
    myArrayList.Sort();
    DisplayArrayList("myArrayList", myArrayList);

    // use the BinarySearch() method to search myArrayList for
    // myCorvette
    Console.WriteLine("Using the BinarySearch() method to search myArrayList\n" +
      " for myCorvette");
    int index2 = myArrayList.BinarySearch(myCorvette);
    Console.WriteLine("Found myCorvette at index " + index2);

    // use the GetRange() method to get a range of elements
    // from myArrayList
    Console.WriteLine("Using the GetRange() method to get two\n" +
      " elements from myArrayList, starting at index 1");
    ArrayList anotherArrayList = myArrayList.GetRange(12);
    DisplayArrayList("anotherArrayList", anotherArrayList);

    // get an enumerator using the GetEnumerator() method
    // and use it to read the elements in myArrayList
    Console.WriteLine("Using the GetEnumerator() method to get an enumerator");
    IEnumerator myEnumerator = myArrayList.GetEnumerator();
    while (myEnumerator.MoveNext())
    {
      Console.WriteLine("myEnumerator.Current = " + myEnumerator.Current);
    }

    // use the Reset() method and access the first row again using MoveNext()
    Console.WriteLine("Using the Reset() method and accessing\n" +
      " the first row again using MoveNext()");
    myEnumerator.Reset();
    myEnumerator.MoveNext();
    Console.WriteLine("myEnumerator.Current = " + myEnumerator.Current);

    // Use a foreach statement to read the contents of myArrayList
    Console.WriteLine("Using a foreach statement to read the contents of myArrayList");
    foreach (Car myCar in myArrayList)
    {
      System.Console.WriteLine("myCar = " + myCar);
    }

  }

}

           
         
    
  
Related examples in the same category
1.Add items to ArrayList and use foreach loop to check
2.Add array to ArrayList
3.Remove range from ArrayList
4.Clone an ArrayList
5.CopyTo, ToArray(), ToArray(typeof(String))
6.Using foreach to loop through ArrayList and use Indexer of ArrayList
7.Check InvalidCastException when using foreach loop with ArrayList
8.Binary Search an ArrayList
9.Inserting into an ArrayList by index
10.Checks time needed for list operations using an ArrayList implementationChecks time needed for list operations using an ArrayList implementation
11.ArrayList Demo: hold classArrayList Demo: hold class
12.illustrates the use of ArrayList properties and methodsillustrates the use of ArrayList properties and methods
13.illustrates the use of ArrayLists 2illustrates the use of ArrayLists 2
14.Convert an ArrayList into an arrayConvert an ArrayList into an array
15.Sort and search an ArrayListSort and search an ArrayList
16.Demonstrate ArrayListDemonstrate ArrayList
17.Search for the first occurrence of the duplicated value
18.Search for the first occurrence of the duplicated value in the last section of the ArrayList
19.Search for the first occurrence of the duplicated value in a section of the ArrayList
20.Search for the first occurrence of the duplicated value in a small section at the end of the ArrayList
21.Convert a ArrayList object to a array
22.Represents a loose abstraction of an arraylist, wheres the buffer array is available for public access.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.