Transcript Arrays

Arrays
Overview
•
•
•
•
•
Arrays and their properties
Creating arrays
Accessing array elements
Modifying array elements
Loops and arrays
– Initialization
– Searching
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…
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
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!
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];
Initializing Arrays with Values
<data type>[] <name> =
new <data type> [<size>]
{VALUES};
Or
<data type>[] <name> = {VALUES};
Modifying an Array
• You must specify which slot you are putting information
in
0
1
2
3
4
49
…
• 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
12
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
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;
}
Line by Line
(a smaller example)
byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) {
myList [counter] = 42;
}
Line by Line
byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) {
myList [counter] = 42;
}
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
}
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
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
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
0
1
2
3
4
42
42
42
42
42
5
Line by Line
false
byte[ ] myList = new byte[5];
for (int counter = 0; counter < 5; counter++) {
myList [counter] = 42;
counter
}
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
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
}//if
}//for
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
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
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];
counter
}//if
1
}//for
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];
counter
}//if
1
}//for
0
1
2
3
4
smallestSoFar
42
17
42
-8
4
42
Another Trace
int smallestSoFar;
Is 42 > 17?
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
}//if
1
}//for
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];
counter
}//if
1
}//for
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];
counter
}//if
1
}//for
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];
counter
}//if
2
}//for
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];
counter
}//if
2
}//for
0
1
2
3
4
smallestSoFar
42
17
42
-8
4
17
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
Is 17 > 42?
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
}//if
counter
}//for
2
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];
counter
}//if
2
}//for
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];
counter
}//if
3
}//for
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];
counter
}//if
3
}//for
0
1
2
3
4
smallestSoFar
42
17
42
-8
4
17
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
Is 17 > -8?
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
}//if
3
}//for
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];
counter
}//if
3
}//for
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];
counter
}//if
3
}//for
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];
counter
}//if
4
}//for
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];
counter
}//if
4
}//for
0
1
2
3
4
smallestSoFar
42
17
42
-8
4
-8
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
Is -8 > 4?
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
}//if
4
}//for
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];
counter
}//if
4
}//for
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];
counter
}//if
5
}//for
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];
counter
}//if
5
}//for
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];
counter
}//if
5
}//for
0
1
2
3
4
smallestSoFar
42
17
42
-8
4
-8
Summary
• 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