Arrays

0.0(0)
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/35

flashcard set

Earn XP

Description and Tags

2Y2 | Midterms

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

36 Terms

1
New cards

array

is a set of fixed number of values called elements that can be accessed using integer index. Elements on an array are of the same data type. Indices in an array starts at 0.

2
New cards

array

is used to store multiple values of the same data type at a time without declaring a different variable name for each value.

3
New cards

Array elements can be of any data type.

4
New cards

one-dimensional array

is an array in which all elements are arranged like a list.

5
New cards

In C#, arrays are implemented as objects.

6
New cards

To define an array, declare a variable first that refers to an array, followed by creating an instance of the array through the new operator.

7
New cards

The following general syntax defines a one-dimensional array:

data_type[] array_name = new data_type[array_size];

8
New cards

data_type

refers to the type of data that will be stored in the array, and the array_size in square brackets specifies the fixed number of elements can be stored in the array.

9
New cards

new operator

creates the array and initializes the array elements to their default values.

10
New cards

The following example of one-dimensional array that can store 10 integers and all the array elements are initialized to zero:

int[] numbers = new int[10];

11
New cards

The following example shows how to assign values to individual array elements by using an index number:

numbers[0] = 45;

numbers[4] = 23;

12
New cards

The example below shows how to initialize an array upon declaration:

double[] grades = { 2.50, 2.75, 1.25, 5.0, 1.50 };

13
New cards

In the given example, the declared array grades is initialized with five (5) values placed between braces and separated by commas.

double studentGrade = grades[1];

Console.WriteLine(studentGrade); //prints 2.75

14
New cards

Array elements can be accessed by using index number.

15
New cards

The following example accesses the element of array grades by placing the index of the element on the square brackets after the name of the array.

16
New cards

It gets the array element of index 1 which is 2.75 then copies its value and stores it on the variable studentGrade:

17
New cards

foreach

The _ statement in C# is used to process the elements of an array starting with index 0 up to the ending index. This iterates through each item of the array.

18
New cards

The following example shows how to use a foreach statement to access the all the elements of the array grades:

foreach (double grade in grades)

{

Console.Write(grade + " / ");

}

Output:

2.5 / 2.75 / 1.25 / 5 / 1.5 /

19
New cards

In the given example of foreach loop, the in keyword is used to iterate over the elements of the array. This gets an item from the array on each iteration and stores in on the declared variable—in the given example—grade. The number of times the foreach loop will execute is equal to the number of elements in the array. In the given example, the foreach loop iterates five (5) times.

20
New cards

two-dimensional array

the array elements are arranged in rows and columns. The elements in this array are arranged in a tabular form and are therefore stored and accessed by using two (2) indices: one (1) index refers to the row, and the other refers to the column location.

21
New cards

The following is the general syntax for defining a two-dimensional array:

data_type[,] array_name = new data_type[row_size, col_size];

22
New cards

code datatype[,]

defines the two-dimensional array being defined.

23
New cards

row_size and col_size

refer to the number of rows and columns in the array, respectively.

24
New cards

The following example defines a two-dimensional array consisting of two (2) rows and four (4) columns:

int[,] table = new int[2, 4];

25
New cards

The example below shows how to assign values to individual array elements by specifying the row and column numbers:

table[0, 1] = 18;

table[1, 3] = 4;

26
New cards

The following example shows how to initialize a two-dimensional array upon declaration:

int[,] table = {

{ 2, 3 },

{ 12, 5},

{ 3, 8 },

{ 18, 3 }

};

27
New cards

The following shows how to access the element of a two-dimensional array:

Console.WriteLine(table[3, 0]); //this prints the 18

28
New cards

ArrayList class

in C# is a collection that behaves as a dynamic array where the array size can dynamically increase as required.

29
New cards

ArrayList class

is defined in the System.Collections namespace.

30
New cards

The following is the general syntax for defining an array list. (*Note: The namespace System.Collections must be declared first in order to use the ArrayList class.)

ArrayList name_of_list = new ArrayList();

31
New cards

The following example shows how to create an ArrayList:

ArrayList nameList = new ArrayList();

32
New cards

Add()

The _ method is used to add an element to the list.

33
New cards

The following is the example of adding elements on the ArrayList named nameList:

nameList.Add("Jack Paul");

nameList.Add("Adrian Castro");

nameList.Add("Peter Cruz");

nameList.Add("Angela Cruz");

34
New cards

The elements of an ArrayList can be accessed by specifying an integer index. For example:

Console.WriteLine(nameList[2]); //this will print Peter Cruz

35
New cards

The foreach loop

is used to process all the elements of an ArrayList.

36
New cards

For example:

foreach (string name in nameList)

{

Console.Write(name + ", ");

}

Output:

Jack Paul, Adrian Castro, Peter Cruz, Angela Cruz