Lecture 12 Arrays Richard Gesick Figures from Lewis, “C# Software Solutions”, Addison Wesley CSE 1301 Overview • • • • • Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays – Initialization – Searching CSE 1301 Arrays and Their Properties • Hold several values of the same type (homogeneous) • Based on a slot number (the index number) • Instant access • Linear (one after the other) • Static – once their size is set, it’s set… CSE 1301 What arrays look like • Things to notice 0 1 2 3 4 5 6 myArray • There are 7 slots, with index numbers 0 – 6 • The name of the array is myArray • Easy to be off by one CSE 1301 Creating Arrays <data type>[] <name> = new <data type> [<size>]; • new brings complex variables to life in C# • Notice that we can create an array of any data type, just by changing the data type! CSE 1301 Examples • An array of shorts: short[] someArray = new short[50]; • An array of floats: float[] myArray = new float[25]; • An array of booleans: bool[] list = new bool[65]; • An array of chars: char[] characters = new char[255]; CSE 1301 Initializing Arrays with Values <data type>[] <name> = new <data type> [<size>] {VALUES}; Or <data type>[] <name> = {VALUES}; CSE 1301 Modifying an Array • You must specify which slot you are putting information in • Example: int[] myArray = new int[50]; myArray[3] = 12; • This won’t work: int[] myArray = new int[50]; myArray = 12; – Data type on the left of = is an array – Data type on right of = is an int CSE 1301 0 1 2 3 4 12 … 49 Accessing Information • Copying information out of a particular slot • Example: int clientAge; clientAge = myArray[4]; • This copies information from the fifth slot (slot four) into the variable clientAge CSE 1301 Initializing Arrays (large arrays) • For most arrays, you will use a loop to initialize • Problem: create an array of 50 bytes and fill each slot with the number 42 • Solution: byte[] myList = new byte[50]; for (int counter=0; counter < 50; counter++) { myList[counter] = 42; } CSE 1301 Line by Line (a smaller example) byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } CSE 1301 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } CSE 1301 0 1 2 3 4 0 0 0 0 0 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter CSE 1301 0 1 2 3 4 0 0 0 0 0 0 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter CSE 1301 0 1 2 3 4 0 0 0 0 0 0 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter 0 CSE 1301 0 1 2 3 4 0 0 0 0 0 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter CSE 1301 0 1 2 3 4 42 0 0 0 0 0 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter CSE 1301 0 1 2 3 4 42 0 0 0 0 1 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter CSE 1301 0 1 2 3 4 42 0 0 0 0 1 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter CSE 1301 0 1 2 3 4 42 0 0 0 0 1 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter CSE 1301 0 1 2 3 4 42 42 0 0 0 1 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter CSE 1301 0 1 2 3 4 42 42 0 0 0 2 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter CSE 1301 0 1 2 3 4 42 42 0 0 0 2 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter CSE 1301 0 1 2 3 4 42 42 0 0 0 2 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter CSE 1301 0 1 2 3 4 42 42 42 0 0 2 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter CSE 1301 0 1 2 3 4 42 42 42 0 0 3 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter CSE 1301 0 1 2 3 4 42 42 42 0 0 3 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter CSE 1301 0 1 2 3 4 42 42 42 0 0 3 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter CSE 1301 0 1 2 3 4 42 42 42 42 0 3 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter CSE 1301 0 1 2 3 4 42 42 42 42 0 4 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter CSE 1301 0 1 2 3 4 42 42 42 42 0 4 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter CSE 1301 0 1 2 3 4 42 42 42 42 0 4 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter CSE 1301 0 1 2 3 4 42 42 42 42 42 4 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; } counter CSE 1301 0 1 2 3 4 42 42 42 42 42 5 Line by Line byte[ ] myList = new byte[5]; for (int counter = 0; counter < 5; counter++) { myList [counter] = 42; false } counter CSE 1301 0 1 2 3 4 42 42 42 42 42 5 Finding the Smallest Element • If you were given an array of numbers, how would YOU do it? • Computers can only compare two at a time • Go through entire list • Keep track of smallest so far • Let’s assume – We have an array of 5 integers – The array contains random unknown numbers – The name of the array is called randomArray CSE 1301 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for CSE 1301 0 1 2 3 4 42 17 42 -8 4 smallestSoFar Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for CSE 1301 0 1 2 3 4 42 17 42 -8 4 smallestSoFar Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if }//for CSE 1301 0 1 2 3 4 smallestSoFar 42 17 42 -8 4 42 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if counter }//for 1 CSE 1301 0 1 2 3 4 smallestSoFar 42 17 42 -8 4 42 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if counter }//for 1 CSE 1301 0 1 2 3 4 smallestSoFar 42 17 42 -8 4 42 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if counter }//for 1 CSE 1301 0 1 2 3 4 smallestSoFar 42 17 42 -8 4 42 Is 42 > 17? Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if counter }//for 1 CSE 1301 0 1 2 3 4 smallestSoFar 42 17 42 -8 4 17 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if counter }//for 1 CSE 1301 0 1 2 3 4 smallestSoFar 42 17 42 -8 4 17 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if counter }//for 2 CSE 1301 0 1 2 3 4 smallestSoFar 42 17 42 -8 4 17 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if counter }//for 2 CSE 1301 0 1 2 3 4 smallestSoFar 42 17 42 -8 4 17 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if counter }//for Is 17 > 42? 2 CSE 1301 0 1 2 3 4 smallestSoFar 42 17 42 -8 4 17 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if counter }//for 2 CSE 1301 0 1 2 3 4 smallestSoFar 42 17 42 -8 4 17 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if counter }//for 3 CSE 1301 0 1 2 3 4 smallestSoFar 42 17 42 -8 4 17 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if counter }//for 3 CSE 1301 0 1 2 3 4 smallestSoFar 42 17 42 -8 4 17 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if counter }//for 3 CSE 1301 Is 17 > -8? 0 1 2 3 4 smallestSoFar 42 17 42 -8 4 17 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if counter }//for 3 CSE 1301 0 1 2 3 4 smallestSoFar 42 17 42 -8 4 -8 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if counter }//for 3 CSE 1301 0 1 2 3 4 smallestSoFar 42 17 42 -8 4 -8 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if counter }//for 4 CSE 1301 0 1 2 3 4 smallestSoFar 42 17 42 -8 4 -8 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if counter }//for 4 CSE 1301 0 1 2 3 4 smallestSoFar 42 17 42 -8 4 -8 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if counter }//for 4 CSE 1301 0 1 2 3 4 smallestSoFar 42 17 42 -8 4 -8 Is -8 > 4? Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if counter }//for 4 CSE 1301 0 1 2 3 4 smallestSoFar 42 17 42 -8 4 -8 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if counter }//for 5 CSE 1301 0 1 2 3 4 smallestSoFar 42 17 42 -8 4 -8 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if counter }//for 5 CSE 1301 0 1 2 3 4 smallestSoFar 42 17 42 -8 4 -8 Another Trace int smallestSoFar; smallestSoFar = randomArray[0]; for (int counter = 1; counter < 5; counter++) { if (smallestSoFar > randomArray[counter]) { smallestSoFar = randomArray[counter]; }//if counter }//for 5 CSE 1301 0 1 2 3 4 smallestSoFar 42 17 42 -8 4 -8 Summary • • • • • CSE 1301 Arrays hold values that are all the same type Arrays are static in size Creating arrays always follows a format You can create an array of any type To initialize or search arrays, you’ll almost always use a loop