Array Project PDF
Array Project PDF
class Program
{
static void Main(string[] args)
{
//
// Use this array of string references.
//
string[] array1 = { "cat", "dog", "carrot", "bird" };
//
// Find first element starting with substring.
//
string value1 = Array.Find(array1,
element => element.StartsWith("car", StringComparison.Ordinal));
//
// Find first element of three characters length.
//
string value2 = Array.Find(array1,
element => element.Length == 3);
//
// Find all elements not greater than four letters long.
//
string[] array2 = Array.FindAll(array1,
element => element.Length <= 4);
Console.WriteLine(value1);
Console.WriteLine(value2);
Console.WriteLine(string.Join(",", array2));
Console.ReadLine();
}
}
Output
carrot
cat
cat,dog,bird
//////////////////////////////////////////////////////////////////////
C# program that uses Array.FindLast
using System;
class Program
{
static void Main()
{
string[] array = { "dot", "net", "yes","perls" };
// Find last string of length 3.
string result = Array.FindLast(array, s => s.Length == 3);
Console.WriteLine(result); }
}
Output
yes
C# program that uses Array.FindIndex method
using System;
class Program
{
static void Main()
{
int[] array = { 5, 6, 7, 6 };
// Write results.
Console.WriteLine("{0} = {1}", index1, array[index1]);
Console.WriteLine("{0} = {1}", index2, array[index2]);
}}
Output
1 = 6
3 = 6
class Program
{
static void Main()
{
char[] array = { 'z', 'a', 'b' };
// Convert array to a string and print it.
Console.WriteLine("UNSORTED: " + new string(array));
Output
UNSORTED: zab
SORTED: abz
///////////////////////////////////////////////////////////
C# program that uses Array.Sort
using System;
class Program
{
static void Main()
{
string[] colors = new string[]
{
"orange",
"blue",
"yellow",
"aqua",
"red"
};
// Call Array.Sort method.
Array.Sort(colors);
foreach (string color in colors)
{
Console.WriteLine(color);
}
Output
aqua
blue
orange
red
yellow
C# program that sorts copy
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
string[] array = { "zebra", "parrot", "ant" };
Console.WriteLine(string.Join(",", array));
Console.WriteLine(string.Join(",", copy));
}
}
Output
zebra,parrot,ant
ant,parrot,zebra
///////////////////////////////////////////////////////////
You can search for the occurrence of a value in an array with the IndexOf and LastIndexOf member
functions. IndexOf starts the search from a lower subscript and moves forward, and LastIndexOf
starts the search from an upper subscript and moves backwards. Both functions achieve a linear
search, visiting each element sequentially until they find the match forward or backward.
Listing below illustrates a linear search through an array using the IndexOf and LastIndexOf
methods.
using System;
String[] myArray = new String[7] { "kama", "dama", "lama", "yama", "pama", "rama", "lama" };
// Search for the first occurrence of the duplicated value in a section of the
myIndex = Array.IndexOf(myArray, myString, 0, 6);
Console.WriteLine("The first occurrence of \"{myString}\" between index 0 and index 6 is at index
{myIndex}.");
// Search for the last occurrence of the duplicated value in a section of the
myIndex = Array.LastIndexOf(myArray, myString, 6, 7);
Console.ReadLine();
}
The String class provides methods for sorting, searching, and reversing that are easy to use. Note that Sort,
BinarySearch, and Reverse are all static functions and are used for single-dimensional arrays.
Listing below illustrates usage of the Sort, BinarySearch, and Reverse functions.
using System;
class linSearch
Console.WriteLine("Element {i + 1} is {a[i]}");
}
Array.Reverse(a);
Console.WriteLine("-----------Reversed-------------------------------");
Console.WriteLine("----------------------------------------------");
for (int i = 0; i < x; i++)
{
Console.WriteLine("Element {i + 1} is {a[i]}");
/////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplicationarray
{
class Program
{
static void Main(string[] args)
{
//
// Use this array of string references.
//
string[] array1 = { "cat", "dog", "carrot", "bird" };
//
Console.WriteLine(value1);
Console.WriteLine(value2);
Console.WriteLine(string.Join(",", array2));
//////////////////////////////////
//C# program that uses Array.FindLast
{
string[] array = { "dot", "net", "yes", "perls" };
// Find last string of length 3.
string result = Array.FindLast(array, s => s.Length == 3);
Console.WriteLine(result);
}
//////////////////////////////////////////////////////////////
{
int[] array = { 5, 6, 7, 6 };
// Write results.
Console.WriteLine("{0} = {1}", index1, array[index1]);
Console.WriteLine("{0} = {1}", index2, array[index2]);
}
///////////////////////////////////////////////////////////////
{
char[] array = { 'z', 'a', 'b' };
// Convert array to a string and print it.
Console.WriteLine("UNSORTED: " + new string(array));
////////////////////////////////////////////////////////////////
string[] colors = new string[]
{
"orange",
"blue",
"yellow",
"aqua",
"red"
};
// Call Array.Sort method.
Array.Sort(colors);
foreach (string color in colors)
{
Console.WriteLine(color);
}
///////////////////////////////////////////////////////////////////
{
string[] array = { "zebra", "parrot", "ant" };
Console.WriteLine(string.Join(",", array));
Console.WriteLine(string.Join(",", copy));
}
/////////////////////////////////////////////////////////////////
String[] myArray = new String[7] { "kama", "dama", "lama", "yama", "pama", "rama", "lama" };
String myString = "lama";
Int32 myIndex;
// Search for the first occurrence of the duplicated value in a section of the
myIndex = Array.IndexOf(myArray, myString, 0, 6);
Console.WriteLine("The first occurrence of \"{myString}\" between index 0 and index 6 is at index
{myIndex}.");
// Search for the last occurrence of the duplicated value in a section of the
myIndex = Array.LastIndexOf(myArray, myString, 6, 7);
Console.WriteLine("The last occurrence of \"{myString}\" between index 0 and index 6 is at index
{myIndex}.");
///////////////////////////////////////////////////////////////////////////////
{
int[] a = new int[3];
Console.WriteLine("Enter number of elements you want to hold in the array (max3)?");
string s = Console.ReadLine();
int x = Int32.Parse(s);
Console.WriteLine("--------------------------------------------------");
Console.WriteLine("\n Enter array elements \n");
Console.WriteLine("--------------------------------------------------");
Console.WriteLine("Element {i + 1} is {a[i]}");
}
Console.WriteLine("Element {i + 1} is {a[i]}");
}
Console.ReadLine();
}
}
}