OOP Arrays

Jump to: navigation, search

Contents

csharp Arrays

Arrays are groups of variables of the same type Syntax

type [] identifier

single dimension arrays

string [] aryNames = new string[3];
 
aryNames [0] = "Joe";
aryNames [1] = "Mike";
aryNames [2] = "Alice";

Example single dimensions array singleArrray.aspx singleArray.cs

multi dimension arrays

string [,] aryNames = new string[3,3];
 
aryNames [0,0] = "Joe";
aryNames [0,1] = "Schmoe";
aryNames [0,2] = "111 111-1111";
aryNames [1,0] = "Mike";
aryNames [1,1] = "Orbinawitz";
aryNames [1,2] = "222 222-2222";
aryNames [2,0] = "Mary";
aryNames [2,1] = "Alice";
aryNames [2,2] = "333 333-3333";

Example multi dimensions array multiArrray.aspx


jagged arrays

string [][] aryNames = new string[3][];
 
aryNames[0] = new string[2];
aryNames[1] = new string[4];
aryNames[2] = new string[3];
 
aryNames [0][0] = "John";
aryNames [0][1] = "Doe";
 
aryNames [1][0] = "James";
aryNames [1][1] = "Bond";
aryNames [1][2] = "007";
aryNames [1][3] = "License to kill";
 
aryNames [2][0] = "Mary";
aryNames [2][1] = "Alice";
aryNames [2][2] = "Im not a number im a free woman";

Example jagged array jaggedArrray.aspx jaggedArray.cs

even more array samples

using System;
 
class Array
{
	public static void Main()
	{
		//array of ints
		int[] myInts = {5,10,15};
		Console.WriteLine("array of ints:");
		Console.WriteLine(
		"myInts[0]: {0}, myInts[1]: {1}, myInts[2]:{2}"
		,myInts[0],myInts[1],myInts[2]);
 
		//jagged array of bools
		bool[][] myBools = new bool[2][];
		myBools[0] = new bool[2];
		myBools[1] = new bool[1];
 
		myBools[0][0] = true;
		myBools[0][1] = false;
		myBools[1][0] = true;
		Console.WriteLine("jagged array of bools:");
		Console.WriteLine(
		"myBools[0][0]: {0}, myBools[1][0]: {1}",
		myBools[0][0], myBools[1][0]);
 
		//multi-dimensional array of doubles
		double[,] myDoubles = new double[2,2];
		myDoubles[0, 0] = 3.147;
		myDoubles[0, 1] = 7.157;
		myDoubles[1, 1] = 2.117;
		myDoubles[1, 0] = 56.00138917;
		Console.WriteLine("multi-dimensional array of doubles:");
		Console.WriteLine("myDoubles[0, 0]: {0}, myDoubles[1, 0]: {1}", myDoubles[0, 0], myDoubles[1, 0]);
 
 
		//array of strings
		string[] myStrings = new string[3];
		myStrings[0] = "Joe";
		myStrings[1] = "Matt";
		myStrings[2] = "Robert";
		Console.WriteLine("array of strings:");
		Console.WriteLine("myStrings[0]: {0}, myStrings[1]: {1}, myStrings[2]: {2}", myStrings[0], myStrings[1], myStrings[2]);
	}
}

moreArrys.cs