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);
}
}
How to create a program that searches value 30 inside the array and if the value is found, its positionis returned; otherwise -1 is returned
int[] sArray = new int[] { 10, 20, 30, 40, 50};
bool found = false;
int index = 0;
int position = -1;
while(!found && index < sArray.Length)
{
if ( sArray[index] == 30 )
{
found = true;
position = index;
MessageBox.Show(position.ToString());
}
index++; }
How to use for and foreach loop to create a second array and copy the individual element of the source array to the target array?
for ( int i = 0 ; i < firstArray.Length; i++ )
{
secondArray[i] = firstArrary[i];
}
int[] firstArray = { 3, 6, 9 };
int[] secondArray = new int[firstArray.Length];
int index = 0;
foreach ( var num in firstArray )
{
secondArray[index] = num
index ++;
}
How to compare array with length and elements?
if (firstArray.Length != secondArray.Length) { return false; }
if (firstArray[index] != secondArray[index]) { return false ; }
How to total up the value of an array?
for (int i = 0; index < units.length; index++ )
{
total += units[index];
}
How to find out the highes value in an array?
int[] numbers = { 8 , 1 , 12 , 6 , 2 }
int highest = numbers[0];
for (int i = 1; i < numbers.Length; i ++ )
{
if (numbers[index] > highest)
{
highest = numbers[index];
}
}
What is the difference between list and array?
- List can be resized dynamically but arrays can’t
- Proivides the methods to add, search, sort and manipulate lists
What generic class is list under?
- System.Collections.Generic
How to add 26, 15,12 into list numList?
numList.Add(26);
numList.Add(15);
numList.Add(12);
How to insert number 53 into the second element?
numList.Insert(2,53);
How to insert a list into another list?
int[] num = {11,22,33}
numList.InsertRange(4,num);
How to remove an element 11 from a list?
numList.Remove(11);
How to remove the element in a index of 4 in list?
numList.RemoveAt(4);
List out some other methods in list ( 5 )
- Clear() - Remove all items in list
- Find() - Search item in list
- Sort() - Sort list in ascending order
- Reverse - Reverse items in list
- Count - Returns the size of list