Arrays - Long Island University

Download Report

Transcript Arrays - Long Island University


From http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx


Arrays are “0 indexed”
When declaring an array
◦ Square brackets come after the type (not the
identifier)
◦ int[ ] myTable;
◦ int[ ] numbers;
 Delcares “numbers” on int type, array of “any size”
◦ numbers = new int[10];
 Now it’s a ten element array…

Declaring an array does not actually create an array
◦ Single dimension array
 int[ ] numbers;
◦ Multidimensional array
 string[,] names;
◦ Array of arrays (jagged array)
 byte[][] scores;
◦ To create an array (which is an object), you must instantiate
it
 int[ ] numbers = new int[5];
 string[,] names = new string[5,4];
 byte[][] scores = new byte[5][];




for (int x = 0; x < scores.Length; x++)
{
scores[x] = new byte[4];
}

Larger arrays
◦ int[,,] buttons = new int[4,5,3];

Mixing rectangular and jagged arrays
◦ int[][,,][,] numbers;

You can initialize an array at declaration
◦ Array members are automatically initialized to a
“default value” for that type if you don’t initilize
◦ Enclose the initial values in curly braces
int[] numbers = new int[5] {1, 2, 3, 4, 5};
string[] names = new string[3] {"Matt", "Joanne",
"Robert"};
◦ You can leave out the size of the array
int[] numbers = new int[] {1, 2, 3, 4, 5};
int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = new string[,] { {"Mike","Amy"},
{"Mary","Albert"} };

You can omit the new operator if an initializer
is provided
int[,] numbers = { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = { {"Mike", "Amy"}, {"Mary", "Albert"} };
◦ Jagged arrays
int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
◦ Omit size of first array
int[][] numbers = new int[][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
or
int[][] numbers = { new int[] {2,3,4}, new int[] {5,6,7,8,9} };

Simple array
int[] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
numbers[4] = 5;

Mutlidimensional array
int[,] numbers = { {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10} };
numbers[1, 1] = 5;

http://www.csharpcorner.com/uploadfile/mahesh/workingwitha
rrays11232005064036am/workingwitharrays.aspx

System.Array
◦ Base class
◦ All arrays you create inherit the properties and
methods of this class
 Let’s examine code!



Locating the last occurrence of a value in an
array
Reversing the order of an array
Note:
◦ The usage of the abstract Array class, rather that
the actual instantiated object
 The object is passed to the Array class as a parameter

The “Array” class is an
abstract base class

◦ Has a “CreateInstance”
method
◦ SetValue method can add
items to the array
◦ // Creates and initializes a
new Array of type Int32.
oddArray.SetValue(1, 0);
oddArray.SetValue(3, 1);
oddArray.SetValue(5, 2);
oddArray.SetValue(7, 3);
oddArray.SetValue(9, 4);
◦ Create
◦ Manipulate
 Insert
 Delete
 Copy
Array x = Array.CreateInstance(
typeof(String),2,4);
Array oddArray =
Array.CreateInstance(
Type.GetType("System.Int32"), 5 );
Array class has methods to
◦ Search
◦ Sort
◦ Reverse

Boolean properties

Other Properties
◦ IsFixedSize
◦ IsReadOnly
◦ IsSynchronized
◦ Length
◦ Rank

BinarySearch
◦ Two parameters: array and object you’re looking for
◦ Found? Index returned; not found? Negative value returned
int [] intArray = new int[3] {0, 1, 2};
string[] names = new string[] {"Rosy","Amy", "Peter","Albert"};
object obj1 = "Peter";
object obj2 = 1;
int retVal = Array.BinarySearch(names, obj1);
if (retVal >=0)
Console.WriteLine("Item index " +retVal.ToString() );
else
Console.WriteLine("Item not found");
retVal = Array.BinarySearch(intArray, obj2);
if (retVal >=0)
Console.WriteLine("Item index " +retVal.ToString() );
else
Console.WriteLine("Item not found");

IndexOf
 Locates and returns the index of the first occurrence in
a one-dimension (or portion of) array

LastIndexOf
 Locates and returns the index of the last occurrence in
a one-dimension (or portion of) array

Overloaded forms
◦ Simplest: parameter is the array you wish to sort
string[] names = new string[] {"Rosy","Amy", "Peter","Albert"};
Console.WriteLine("Original Array:");
foreach (string str in names)
{
Console.WriteLine(str);
}
Console.WriteLine("Sorted Array:");
Array.Sort(names);
foreach (string str in names)
{
Console.WriteLine(str);
}

Reverse paramter is the name of array
Array.Reverse(x)

Clear
◦ Removes all elements and sets length to zero
◦ Paramters: array objects, starting index of array,
number of elements
Array.Clear(x,1,2)
 Removes 2 elements starting at “sub 1”




GetLength
GetUpperBound
GetLowerBound
All three take the parameter of the “rank” of the
array
◦ 0 if single dimension, otherwise specify the dimension
number (zero-based)

Copy
◦ Copies a section of an array to another array

CopyTo
◦ Copies all the elements of an array to another onedimension array


Create an array of “Object” type
Copy an array to this one, using a for loop
instead of a foreach loop
Array objArray = Array.CreateInstance( Type.GetType("System.Object"), 5 );
Array.Copy(oddArray, oddArray.GetLowerBound(0), objArray,
objArray.GetLowerBound(0), 4 );
int items1 = objArray.GetUpperBound(0);
for ( int i =0; i < items1; i++ )
Console.WriteLine(objArray.GetValue(i).ToString());