Visual Programming (CSC-209) : Teacher: Arjumand Yar Khan 7/8/10 September 2015 Lecture 7
Visual Programming (CSC-209) : Teacher: Arjumand Yar Khan 7/8/10 September 2015 Lecture 7
(CSC-209)
Teacher: Arjumand Yar Khan
7/8/10 September 2015 Lecture 7
Topics to be covered
Understanding Arrays
Suppose, you need to store years of 100 cars. Will you define 100
variables?
int y1, y2,, y100;
The data structures LIKE arrays, array lists, and Ragged arrays are useful to
solve many (such)types of programming problems.
WHAT IS AN ARRAY?
An array is a sequence of objects of same data types for instance strings.
The object of an array is known as element of an array.
13
memory
24
C[0]
59
C[1]
35
C[2]
C[3]
...
C[4]
...
C[5]
...
C[6]
...
C[7]
...
Arrays are declaredC[8]
much like variables,
... with a set of [] brackets after the
data type, like this: C[9]
...
C# arrays are zero indexed; that is, the array indexes start at zero.
Arrays in C# work similarly to how arrays work in most other popular
languages.
There are, however, a few differences in which an important one is:
When declaring an array, the square brackets ([]) must come after the type,
not the identifier. Placing the brackets after the identifier is not legal
syntax in C#.
string[] names;
You need to instantiate the array to use it, which is done like this:
new type Specifier [numberOf Elements];
E.g: string[] names = new string[2];
where:
typeSpecifier is the data type you want to use.
arrayName is the name you want to use for the array.
numberOfElements is the number of array elements (size) you want.
Array Details
In C#, arrays are actually objects. System.Array is the abstract base type
of all array types. You can use the properties, and other class members,
that System.Array has.
The System.Array class provides many other useful methods/properties,
such as methods for sorting, searching, and copying arrays.
An example of this would be using the Length property to get the length
of an array. The following code assigns the length of the numbers array,
which is 5, to a variable called LengthOfNumbers:
int[] numbers = {1, 2, 3, 4, 5};
int LengthOfNumbers = numbers.Length;
Important Note
TwodimensionalArray
int[,]number=newint[2,3]{{1,2,3},{4,5,6}};
ThreeDimensionalArray
Int[,,]number=newint[1,2,3].
FourDimensionalArray
Int[1,2,3,4]number=newint[1,2,3,4]{{{{1,2},{1,2},
{1,2}},{{1,2},{1.2},{1,2}}}};
Jagged Arrays
A Jagged array is an array of arrays.
A jagged array is an array whose elements are arrays. The
elements of a jagged array can be of different dimensions
and
sizes.
Compared to a 2D array where each row has equal number
of columns a Jagged array has different number of columns.
Important Note
Jagged Arrays
A jagged array is the same in any language, but it's
where you have a 2+ dimensional array with
different array lengths in the second and beyond
array.
DECLARATION
Declaring a Jagged array
You can declare a jagged array scores of int
values as:
int [][] scores
Declaring an array, does not create the array in
memory. To create the above array:
int[][] scores = new int[3][];
INITIALIZATION
jaggedArray[0] = newint[5];
jaggedArray[1] = newint[4];
jaggedArray[2] = newint[2];
Each of the elements is a single-dimensional
array of integers. The first element is an array
of 5 integers, the second is an array of 4
integers, and the third is an array of 2 integers.
jaggedArray[0] = newint[] { 1, 3, 5, 7,
9 }; jaggedArray[1] = newint[] { 0, 2, 4, 6
}; jaggedArray[2] = newint[] { 11, 22 };
jaggedArray3[0][1] = 77;
// Assign 88 to the second element ([1]) of the third array ([2]):
jaggedArray3[2][1] = 88;
Important Note
ARRAYLIST OBJECTS
The arrays discussed thus far are called static arrays. This is because
when you set their element sizes, they cannot be changed. Indeed, you
cannot use a static array until its dimension or dimensions have been
determined.
However, life isnt always that simple. Quite often you have a situation
in which you know you need to store the data in an array but dont have a
clue how many elements you might need.
For example, you might write a program that records the names and
addresses of friends in an object called Friends. When you run the
program, you might need 50 array elements, but another person might
need only only 20 elements. The issue is, How do you decide how many
elements to allocate for the array?
ARRAYLIST
With static arrays, the usual solution is to settle for a worst-case design. A worstcase design is one in which you try to guess the largest reasonable value you will
ever need for the size of the array and then set the dimension for the array to that
size.
This can be inefficient because you will likely overestimate the required size for the
array most of the time.
The ArrayList object overcomes this limitation of static arrays. ArrayList objects
have the effect to create dynamic arrays for which you dont have to specify a size.
Now see how this works.
ArrayList objects offer some features that are not available with simple array. This
is especially true for those arrays where you dont know the size of the array at
compile time.
Key Points
Creation of ArrayList
To create the ArrayList the following code
is used:
ArrayListarr =newArrayList();
Methods of ARRAYLIST
Note:
Complete applications as an Example on Methods of Array List are
attached as separate files
Assignment
* Bucket Analogy
* Array List class (Constructor, other important
Methods)
Note:
Complete application as an Example on Array List with type casting is
attached as a separate file.