Csharp/C Sharp by API/System.Collections/ArrayList
Содержание
- 1 ArrayList.Add
- 2 ArrayList.AddRange
- 3 ArrayList.BinarySearch
- 4 ArrayList.Capacity
- 5 ArrayList.Clear()
- 6 ArrayList.Clone
- 7 ArrayList.Contains
- 8 ArrayList.CopyTo
- 9 ArrayList.GetEnumerator()
- 10 ArrayList.GetRange
- 11 ArrayList.IndexOf
- 12 ArrayList.Insert
- 13 ArrayList.InsertRange()
- 14 ArrayList.IsFixedSize
- 15 ArrayList.IsReadOnly
- 16 ArrayList.LastIndexOf
- 17 ArrayList.Remove
- 18 ArrayList.RemoveAt
- 19 ArrayList.RemoveRange
- 20 ArrayList.Reverse()
- 21 ArrayList.SetRange
- 22 ArrayList.Sort()
- 23 ArrayList.Synchronized
- 24 ArrayList.ToArray
- 25 ArrayList.ToArray(typeof(T))
- 26 ArrayList.TrimToSize()
- 27 new ArrayList()
- 28 new ArrayList(Collection c)
ArrayList.Add
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
class Program {
static void Main(string[] args) {
ArrayList baseballTeams = new ArrayList();
baseballTeams.Add("S");
baseballTeams.Add("r");
baseballTeams.Add("F");
string[] myStringArray = new string[2];
myStringArray[0] = "G";
myStringArray[1] = "L";
baseballTeams.AddRange(myStringArray);
foreach (string item in baseballTeams) {
Console.Write(item + "\n");
}
}
}
ArrayList.AddRange
using System;
using System.Collections;
using System.Collections.Specialized;
class MyClass{
public string MyName="";
}
class MainClass
{
static void Main(string[] args)
{
ArrayList classList = new ArrayList();
classList.AddRange(new MyClass[] { new MyClass(),
new MyClass(),
new MyClass()});
Console.WriteLine("Items in List: {0}", classList.Count);
// Print out current values.
foreach(MyClass c in classList)
{
Console.WriteLine("MyClass name: {0}", c.MyName);
}
}
}
ArrayList.BinarySearch
using System;
using System.Collections;
class Album : IComparable, ICloneable {
private string _Title;
private string _Artist;
public Album(string artist, string title) {
_Artist = artist;
_Title = title;
}
public string Title {
get {
return _Title;
}
set {
_Title = value;
}
}
public string Artist {
get {
return _Artist;
}
set {
_Artist = value;
}
}
public override string ToString() {
return _Artist + ",\t" + _Title;
}
public int CompareTo(object o) {
Album other = o as Album;
if (other == null)
throw new ArgumentException();
if (_Artist != other._Artist)
return _Artist.rupareTo(other._Artist);
else
return _Title.rupareTo(other._Title);
}
public object Clone() {
return new Album(_Artist, _Title);
}
}
class TitleComparer : IComparer {
public int Compare(object l, object r) {
Album left = l as Album;
Album right = r as Album;
if ((left == null) || (right == null))
throw new ArgumentException();
if (left.Title != right.Title)
return left.Title.rupareTo(right.Title);
else
return left.Artist.rupareTo(right.Artist);
}
}
class Class1 {
static void Main(string[] args) {
ArrayList arr = new ArrayList();
arr.Add(new Album("G", "A"));
arr.Add(new Album("B", "G"));
arr.Add(new Album("S", "A"));
arr.Sort();
try {
foreach (Album a in arr) {
Console.WriteLine(a);
}
} catch (System.InvalidCastException e) {
}
arr.Sort(new TitleComparer());
foreach (Album a in arr) {
Console.WriteLine(a);
}
Album l = new Album("L", "G");
arr.Sort();
int index = arr.BinarySearch(l);
Console.WriteLine(index.ToString());
arr.Sort(new TitleComparer());
index = arr.BinarySearch(l, new TitleComparer());
Console.WriteLine(index.ToString());
}
}
ArrayList.Capacity
using System;
using System.Collections;
public class ArrayListDemo {
public static void Main() {
ArrayList al = new ArrayList();
Console.WriteLine("Initial capacity: " + al.Capacity);
Console.WriteLine("Initial number of elements: " + al.Count);
Console.WriteLine();
Console.WriteLine("Adding 6 elements");
// Add elements to the array list
al.Add("C");
al.Add("A");
al.Add("E");
al.Add("B");
al.Add("D");
al.Add("F");
Console.WriteLine("Current capacity: " +
al.Capacity);
Console.WriteLine("Number of elements: " +
al.Count);
// Display the array list using array indexing.
Console.Write("Current contents: ");
for(int i=0; i < al.Count; i++)
Console.Write(al[i] + " ");
Console.WriteLine("\n");
Console.WriteLine("Removing 2 elements");
// Remove elements from the array list.
al.Remove("F");
al.Remove("A");
Console.WriteLine("Current capacity: " +
al.Capacity);
Console.WriteLine("Number of elements: " +
al.Count);
// Use foreach loop to display the list.
Console.Write("Contents: ");
foreach(char c in al)
Console.Write(c + " ");
Console.WriteLine("\n");
Console.WriteLine("Adding 20 more elements");
// Add enough elements to force al to grow.
for(int i=0; i < 20; i++)
al.Add((char)("a" + i));
Console.WriteLine("Current capacity: " +
al.Capacity);
Console.WriteLine("Number of elements after adding 20: " +
al.Count);
Console.Write("Contents: ");
foreach(char c in al)
Console.Write(c + " ");
Console.WriteLine("\n");
// Change contents using array indexing.
Console.WriteLine("Change first three elements");
al[0] = "X";
al[1] = "Y";
al[2] = "Z";
Console.Write("Contents: ");
foreach(char c in al)
Console.Write(c + " ");
Console.WriteLine();
}
}
ArrayList.Clear()
using System;
using System.Collections;
class MainClass
{
static void Main(string[] args)
{
ArrayList a = new ArrayList(10);
int x = 0;
a.Add( ++x);
a.Add( ++x);
a.Add( ++x);
a.Add( ++x);
a.Remove(x);
a.RemoveAt(0);
a.Clear();
}
}
ArrayList.Clone
using System;
using System.Collections;
public class Starter {
public static void Main(string[] argv) {
ArrayList al1 = new ArrayList();
foreach (string arg in argv) {
al1.Add(int.Parse(arg));
}
al1.Sort();
ArrayList al2 = (ArrayList)al1.Clone();
for (int count = 0; count < al2.Count; ++count) {
al2[count] = ((int)al2[count]) * 2;
}
foreach (int number in al2) {
Console.WriteLine(number);
}
}
}
ArrayList.Contains
using System;
using System.Collections;
class MainClass
{
public static void Main()
{
ArrayList myArrayList = new ArrayList();
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
string[] anotherStringArray = {"Here"s", "some", "more", "text"};
myArrayList.SetRange(0, anotherStringArray);
if (myArrayList.Contains("text"))
{
int index = myArrayList.IndexOf("text");
Console.WriteLine("myArrayList does contain the word "text"");
Console.WriteLine(""text" first occurs at index " + index);
index = myArrayList.LastIndexOf("text");
Console.WriteLine(""text" last occurs at index " + index);
}
}
}
ArrayList.CopyTo
using System;
using System.Collections;
class MainClass
{
public static void Main(string[] args)
{
// Create a new ArrayList and populate it.
ArrayList list = new ArrayList(5);
list.Add("B");
list.Add("G");
list.Add("J");
list.Add("S");
list.Add("M");
string[] array1 = new string[list.Count];
list.CopyTo(array1, 0);
Console.WriteLine("Array 1:");
foreach (string s in array1)
{
Console.WriteLine("\t{0}",s);
}
}
}
ArrayList.GetEnumerator()
using System;
using System.Collections;
class MainClass {
public static void Main() {
ArrayList list = new ArrayList(1);
for(int i=0; i < 10; i++)
list.Add(i);
IEnumerator etr = list.GetEnumerator();
while(etr.MoveNext())
Console.Write(etr.Current + " ");
Console.WriteLine();
etr.Reset();
while(etr.MoveNext())
Console.Write(etr.Current + " ");
Console.WriteLine();
}
}
ArrayList.GetRange
using System;
using System.Collections;
class MainClass
{
public static void Main()
{
ArrayList myArrayList = new ArrayList();
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
string[] anotherStringArray = {"Here"s", "some", "more", "text"};
myArrayList.SetRange(0, anotherStringArray);
ArrayList anotherArrayList = myArrayList.GetRange(1, 2);
DisplayArrayList("anotherArrayList", anotherArrayList);
}
public static void DisplayArrayList(string arrayListName, ArrayList myArrayList)
{
for (int i = 0; i < myArrayList.Count; i++){
Console.WriteLine(arrayListName + "[" + i + "] = " +
myArrayList[i]);
}
}
}
ArrayList.IndexOf
using System;
using System.Collections;
class MainClass
{
public static void Main()
{
ArrayList myArrayList = new ArrayList();
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
string[] anotherStringArray = {"Here"s", "some", "more", "text"};
myArrayList.SetRange(0, anotherStringArray);
if (myArrayList.Contains("text"))
{
int index = myArrayList.IndexOf("text");
Console.WriteLine("myArrayList does contain the word "text"");
Console.WriteLine(""text" first occurs at index " + index);
index = myArrayList.LastIndexOf("text");
Console.WriteLine(""text" last occurs at index " + index);
}
}
}
ArrayList.Insert
using System;
using System.Collections;
using System.Collections.Specialized;
class MyClass{
public string MyName="";
}
class MainClass
{
static void Main(string[] args)
{
ArrayList classList = new ArrayList();
classList.AddRange(new MyClass[] { new MyClass(),
new MyClass(),
new MyClass()});
Console.WriteLine("Items in List: {0}", classList.Count);
classList.Insert(2, new MyClass());
Console.WriteLine("Items in classList: {0}", classList.Count);
// Print out current values.
foreach(MyClass c in classList)
{
Console.WriteLine("MyClass name: {0}", c.MyName);
}
}
}
ArrayList.InsertRange()
using System;
using System.Collections;
class MainClass
{
public static void Main()
{
ArrayList myArrayList = new ArrayList();
myArrayList.Add("is");
myArrayList.Insert(1, "is");
string[] myStringArray = {"a", "test"};
myArrayList.AddRange(myStringArray);
string[] anotherStringArray = {"Here"s", "some", "more", "text"};
myArrayList.InsertRange(myArrayList.Count, anotherStringArray);
DisplayArrayList("myArrayList", myArrayList);
}
public static void DisplayArrayList(string arrayListName, ArrayList myArrayList)
{
for (int i = 0; i < myArrayList.Count; i++){
Console.WriteLine(arrayListName + "[" + i + "] = " +
myArrayList[i]);
}
}
}
ArrayList.IsFixedSize
using System;
using System.Collections;
class MainClass
{
public static void Main()
{
ArrayList myArrayList = new ArrayList();
Console.WriteLine("myArrayList.IsFixedSize = " + myArrayList.IsFixedSize);
Console.WriteLine("myArrayList.IsReadOnly = " + myArrayList.IsReadOnly);
}
}
ArrayList.IsReadOnly
using System;
using System.Collections;
class MainClass
{
public static void Main()
{
ArrayList myArrayList = new ArrayList();
Console.WriteLine("myArrayList.IsFixedSize = " + myArrayList.IsFixedSize);
Console.WriteLine("myArrayList.IsReadOnly = " + myArrayList.IsReadOnly);
}
}
ArrayList.LastIndexOf
using System;
using System.Collections;
class MainClass
{
public static void Main()
{
ArrayList myArrayList = new ArrayList();
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
string[] anotherStringArray = {"Here"s", "some", "more", "text"};
myArrayList.SetRange(0, anotherStringArray);
if (myArrayList.Contains("text"))
{
int index = myArrayList.IndexOf("text");
Console.WriteLine("myArrayList does contain the word "text"");
Console.WriteLine(""text" first occurs at index " + index);
index = myArrayList.LastIndexOf("text");
Console.WriteLine(""text" last occurs at index " + index);
}
}
}
ArrayList.Remove
using System;
using System.Collections;
class MainClass
{
public static void Main()
{
ArrayList myArrayList = new ArrayList();
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
string[] anotherStringArray = {"Here"s", "some", "more", "text"};
myArrayList.SetRange(0, anotherStringArray);
myArrayList.RemoveAt(0);
myArrayList.Remove("text");
myArrayList.RemoveRange(3, 2);
DisplayArrayList("myArrayList", myArrayList);
}
public static void DisplayArrayList(string arrayListName, ArrayList myArrayList)
{
for (int i = 0; i < myArrayList.Count; i++){
Console.WriteLine(arrayListName + "[" + i + "] = " +
myArrayList[i]);
}
}
}
ArrayList.RemoveAt
using System;
using System.Collections;
class MainClass
{
static void Main(string[] args)
{
ArrayList a = new ArrayList(10);
int x = 0;
a.Add( ++x);
a.Add( ++x);
a.Add( ++x);
a.Remove(x);
a.RemoveAt(0);
}
}
ArrayList.RemoveRange
using System;
using System.Collections;
class MainClass
{
public static void Main()
{
ArrayList myArrayList = new ArrayList();
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
string[] anotherStringArray = {"Here"s", "some", "more", "text"};
myArrayList.SetRange(0, anotherStringArray);
myArrayList.RemoveAt(0);
myArrayList.Remove("text");
myArrayList.RemoveRange(3, 2);
DisplayArrayList("myArrayList", myArrayList);
}
public static void DisplayArrayList(string arrayListName, ArrayList myArrayList)
{
for (int i = 0; i < myArrayList.Count; i++){
Console.WriteLine(arrayListName + "[" + i + "] = " +
myArrayList[i]);
}
}
}
ArrayList.Reverse()
using System;
using System.Collections;
class MainClass
{
public static void Main()
{
ArrayList myArrayList = new ArrayList();
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
string[] anotherStringArray = {"Here"s", "some", "more", "text"};
myArrayList.SetRange(0, anotherStringArray);
myArrayList.Reverse();
DisplayArrayList("myArrayList", myArrayList);
}
public static void DisplayArrayList(string arrayListName, ArrayList myArrayList)
{
for (int i = 0; i < myArrayList.Count; i++){
Console.WriteLine(arrayListName + "[" + i + "] = " +
myArrayList[i]);
}
}
}
ArrayList.SetRange
using System;
using System.Collections;
class MainClass
{
public static void Main()
{
ArrayList myArrayList = new ArrayList();
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
string[] anotherStringArray = {"Here"s", "some", "more", "text"};
myArrayList.SetRange(0, anotherStringArray);
DisplayArrayList("myArrayList", myArrayList);
}
public static void DisplayArrayList(string arrayListName, ArrayList myArrayList)
{
for (int i = 0; i < myArrayList.Count; i++){
Console.WriteLine(arrayListName + "[" + i + "] = " +
myArrayList[i]);
}
}
}
ArrayList.Sort()
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Sort and search an ArrayList.
using System;
using System.Collections;
public class SortSearchDemo {
public static void Main() {
// create an array list
ArrayList al = new ArrayList();
// Add elements to the array list
al.Add(55);
al.Add(43);
al.Add(-4);
al.Add(88);
al.Add(3);
al.Add(19);
Console.Write("Original contents: ");
foreach(int i in al)
Console.Write(i + " ");
Console.WriteLine("\n");
// Sort
al.Sort();
// Use foreach loop to display the list.
Console.Write("Contents after sorting: ");
foreach(int i in al)
Console.Write(i + " ");
Console.WriteLine("\n");
Console.WriteLine("Index of 43 is " +
al.BinarySearch(43));
}
}
ArrayList.Synchronized
using System;
using System.Collections;
class MainClass
{
static void Main(string[] args)
{
ArrayList a = new ArrayList(10);
ArrayList.Synchronized(a);
}
}
ArrayList.ToArray
using System;
using System.Collections;
class MainClass
{
public static void Main(string[] args)
{
// Create a new ArrayList and populate it.
ArrayList list = new ArrayList(5);
list.Add("B");
list.Add("G");
list.Add("J");
list.Add("S");
list.Add("M");
object[] array2 = list.ToArray();
Console.WriteLine("Array 2:");
foreach (string s in array2)
{
Console.WriteLine("\t{0}", s);
}
}
}
ArrayList.ToArray(typeof(T))
using System;
using System.Collections;
class MainClass
{
public static void Main(string[] args)
{
// Create a new ArrayList and populate it.
ArrayList list = new ArrayList(5);
list.Add("B");
list.Add("G");
list.Add("J");
list.Add("S");
list.Add("M");
string[] array3 = (string[])list.ToArray(typeof(String));
Console.WriteLine("Array 3:");
foreach (string s in array3)
{
Console.WriteLine("\t{0}", s);
}
}
}
ArrayList.TrimToSize()
using System;
using System.Collections;
class MainClass
{
public static void Main()
{
ArrayList myArrayList = new ArrayList();
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
string[] anotherStringArray = {"Here"s", "some", "more", "text"};
myArrayList.SetRange(0, anotherStringArray);
myArrayList.TrimToSize();
Console.WriteLine("myArrayList.Capacity = " + myArrayList.Capacity);
}
}
new ArrayList()
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
class Program {
static void Main(string[] args) {
ArrayList baseballTeams = new ArrayList();
baseballTeams.Add("s");
baseballTeams.Add("r");
baseballTeams.Add("F");
foreach (string item in baseballTeams) {
Console.Write(item + "\n");
}
}
}
new ArrayList(Collection c)
using System;
using System.Collections;
class MainClass
{
public static void Main()
{
ArrayList myArrayList = new ArrayList();
myArrayList.Add("This");
myArrayList.Add("is");
myArrayList.Add("a");
myArrayList.Add("test");
ArrayList anotherArrayList = new ArrayList(myArrayList);
Console.WriteLine(anotherArrayList);
}
}