Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 53 additions & 47 deletions snippets/csharp/System.Collections/ArrayList/Add/source.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,61 @@
// <Snippet1>
using System;
using System.Collections;
public class SamplesArrayList {

public static void Main() {

// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add( "The" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );

// Creates and initializes a new Queue.
Queue myQueue = new Queue();
myQueue.Enqueue( "jumps" );
myQueue.Enqueue( "over" );
myQueue.Enqueue( "the" );
myQueue.Enqueue( "lazy" );
myQueue.Enqueue( "dog" );

// Displays the ArrayList and the Queue.
Console.WriteLine( "The ArrayList initially contains the following:" );
PrintValues( myAL, '\t' );
Console.WriteLine( "The Queue initially contains the following:" );
PrintValues( myQueue, '\t' );

// Copies the Queue elements to the end of the ArrayList.
myAL.AddRange( myQueue );

// Displays the ArrayList.
Console.WriteLine( "The ArrayList now contains the following:" );
PrintValues( myAL, '\t' );
using System;
using System.Collections;
public class SamplesArrayList
{

public static void Main()
{

// Creates and initializes a new ArrayList.
ArrayList myAL = new();
myAL.Add("The");
myAL.Add("quick");
myAL.Add("brown");
myAL.Add("fox");

// Creates and initializes a new Queue.
Queue myQueue = new();
myQueue.Enqueue("jumps");
myQueue.Enqueue("over");
myQueue.Enqueue("the");
myQueue.Enqueue("lazy");
myQueue.Enqueue("dog");

// Displays the ArrayList and the Queue.
Console.WriteLine("The ArrayList initially contains the following:");
PrintValues(myAL, '\t');
Console.WriteLine("The Queue initially contains the following:");
PrintValues(myQueue, '\t');

// Copies the Queue elements to the end of the ArrayList.
myAL.AddRange(myQueue);

// Displays the ArrayList.
Console.WriteLine("The ArrayList now contains the following:");
PrintValues(myAL, '\t');
}

public static void PrintValues( IEnumerable myList, char mySeparator ) {
foreach ( Object obj in myList )
Console.Write( "{0}{1}", mySeparator, obj );
Console.WriteLine();
public static void PrintValues(IEnumerable myList, char mySeparator)
{
foreach (object obj in myList)
{
Console.Write($"{mySeparator}{obj}");
}

Console.WriteLine();
}
}
}


/*
This code produces the following output.
/*
This code produces the following output.

The ArrayList initially contains the following:
The quick brown fox
The Queue initially contains the following:
jumps over the lazy dog
The ArrayList now contains the following:
The quick brown fox jumps over the lazy dog
*/
The ArrayList initially contains the following:
The quick brown fox
The Queue initially contains the following:
jumps over the lazy dog
The ArrayList now contains the following:
The quick brown fox jumps over the lazy dog
*/
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SamplesArrayList.Run();
MyArrayList.Run();
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<StartupObject>SamplesArrayList</StartupObject>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,49 +1,62 @@
// <Snippet1>
using System;
using System.Collections;
public class SamplesArrayList {

public static void Main() {

// Creates and initializes a new ArrayList. BinarySearch requires
// a sorted ArrayList.
ArrayList myAL = new ArrayList();
for ( int i = 0; i <= 4; i++ )
myAL.Add( i*2 );

// Displays the ArrayList.
Console.WriteLine( "The int ArrayList contains the following:" );
PrintValues( myAL );

// Locates a specific object that does not exist in the ArrayList.
Object myObjectOdd = 3;
FindMyObject( myAL, myObjectOdd );

// Locates an object that exists in the ArrayList.
Object myObjectEven = 6;
FindMyObject( myAL, myObjectEven );
// <Snippet1>
using System;
using System.Collections;
public class SamplesArrayList
{

public static void Run()
{

// Creates and initializes a new ArrayList. BinarySearch requires
// a sorted ArrayList.
ArrayList myAL = [];
for (int i = 0; i <= 4; i++)
{
myAL.Add(i * 2);
}

// Displays the ArrayList.
Console.WriteLine("The int ArrayList contains the following:");
PrintValues(myAL);

// Locates a specific object that does not exist in the ArrayList.
object myObjectOdd = 3;
FindMyObject(myAL, myObjectOdd);

// Locates an object that exists in the ArrayList.
object myObjectEven = 6;
FindMyObject(myAL, myObjectEven);
}

public static void FindMyObject( ArrayList myList, Object myObject ) {
int myIndex=myList.BinarySearch( myObject );
if ( myIndex < 0 )
Console.WriteLine( "The object to search for ({0}) is not found. The next larger object is at index {1}.", myObject, ~myIndex );
else
Console.WriteLine( "The object to search for ({0}) is at index {1}.", myObject, myIndex );
public static void FindMyObject(ArrayList myList, object myObject)
{
int myIndex = myList.BinarySearch(myObject);
if (myIndex < 0)
{
Console.WriteLine($"The object to search for ({myObject}) is not found. The next larger object is at index {~myIndex}.");
}
else
{
Console.WriteLine($"The object to search for ({myObject}) is at index {myIndex}.");
}
}

public static void PrintValues( IEnumerable myList ) {
foreach ( Object obj in myList )
Console.Write( " {0}", obj );
Console.WriteLine();
public static void PrintValues(IEnumerable myList)
{
foreach (object obj in myList)
{
Console.Write($" {obj}");
}

Console.WriteLine();
}
}
/*
This code produces the following output.

The int ArrayList contains the following:
0 2 4 6 8
The object to search for (3) is not found. The next larger object is at index 2.
The object to search for (6) is at index 3.
*/
}
/*
This code produces the following output.

The int ArrayList contains the following:
0 2 4 6 8
The object to search for (3) is not found. The next larger object is at index 2.
The object to search for (6) is at index 3.
*/
// </Snippet1>
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,29 @@ int IComparer.Compare(object x, object y)

public class MyArrayList : ArrayList
{
public static void Main()
public static void Run()
{
// Creates and initializes a new ArrayList.
MyArrayList coloredAnimals = new MyArrayList();

coloredAnimals.Add("White Tiger");
coloredAnimals.Add("Pink Bunny");
coloredAnimals.Add("Red Dragon");
coloredAnimals.Add("Green Frog");
coloredAnimals.Add("Blue Whale");
coloredAnimals.Add("Black Cat");
coloredAnimals.Add("Yellow Lion");
MyArrayList coloredAnimals =
[
"White Tiger",
"Pink Bunny",
"Red Dragon",
"Green Frog",
"Blue Whale",
"Black Cat",
"Yellow Lion",
];

// BinarySearch requires a sorted ArrayList.
coloredAnimals.Sort();

// Compare results of an iterative search with a binary search
// Compare results of an iterative search with a binary search.
int index = coloredAnimals.IterativeSearch("White Tiger");
Console.WriteLine("Iterative search, item found at index: {0}", index);
Console.WriteLine($"Iterative search, item found at index: {index}");

index = coloredAnimals.BinarySearch("White Tiger", new SimpleStringComparer());
Console.WriteLine("Binary search, item found at index: {0}", index);
Console.WriteLine($"Binary search, item found at index: {index}");
}

public int IterativeSearch(object finditem)
Expand Down
Loading
Loading