Chapter 6 : Arrary Flashcards
List out 2 categories that data types in C#
- Values Types ( x = “Hello” )
- Reference Types
What actually holds the data in a value type ?
- Value Type of Variable
What will compiler allocates when I declare a value type variable?
- The compiller will allocate a chunk of memory that is big enough for the variable
- When I’m working with a value type, I’m actually using a variable that holds a piece of data
List out 2 things when I work with a reference type
- An object that is created in memory
- A variable that references the project
What does array stores?
- A group of the same data type together in memory
What data isn’t ideal for array?
- Storing and Processing lists of data
- Each variable can only store 1 data
What type of data type is array? ( Value Type / References Type )
- Reference Type Objects
What do I need to do to create an array?
- Declare a reference type object
- Create the obkect and associate it with the reference variable
What is the format for array?
- dataType[] arraryName;
int[] numbersArray
What is the format for adding new values for array?
- arrayName = new DataType[ArrarySize]
numbersArrary = new int[6]
What is the storage location known as in array?
- Elements
What is noun for the unique number assigned at each element inside an array?
- Subscript
What is the subscript value for the first elements?
- 0
How to create a array with assigning the maximum number of elements in it?
const int size = 5;
int numbers = new int[size];
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
numbers[4] = 5;
How to get the third element of array?
- number[2]
List out 3 ways to initialize array
- int[] num = { 10 , 20 , 30 }
- int[] num = new int { 10, 20, 30 }
- const int size = 3
int[] num = new int[size] {10,20,30};
How to loop through arrary with adding value 99 into the array 3 times?
const int size = 3;
int[] myValues = new int[size];
for ( int i = 0; i < size; i++ )
{
myValues[i] = 99;
}
- Please make sure that this code i < size musn’t be i <= size since the array value is only 0 , 1 , 2 . 3 will return an exception
What is the length property does?
- Set the number of elements in the array
How to set 25 elements in the array? How to output the length of an array?
- double[] temp = new double[25]
- MessageBox.Show(temperatures.Length.ToString());
How to use the length property to loop through every single item in an array?
for ( int i = 0; i < tem.Length; i ++ )
{
MessageBox.Show(tem[i].ToString());
}
What is the foreach loop used for?
- Special loop used to simplify array processing
- It is designed to work a temporary, read-only variable known as iteration variable
What is the format for foreach loop?
foreach ( Type VariableName in ArraryName )
{
statement(s);
}
- Type = data type of array
VariableName = name of temporary iteration variable
How to use foreach loop through the arrary
int[] numbers = { 3 , 6 , 9 }?
int[] numbers = { 3 , 6 , 9 }
foreach ( int val in numbers )
{
MessageBox.Show(val.ToString());
}
How to use a method that passed in array as argument and show the content inside it?
string[] people = { “Bill” , “Jill”}
showArrary(people);
private void showArray(string[] strArray)
{
foreach ( string str in strArrary )
{
MessageBox.Show(str);
}
}