C# Programming Arrays in C# Declaring Arrays of Different Types Initializing Array Accessing Array Elements Creating User Interfaces Using Windows Standards.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Chapter 8 Arrays and Strings
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Chapter 8 Arrays and Strings
C# Programming: From Problem Analysis to Program Design1 7 Arrays and Collections.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
C# E1 CSC 298 Arrays in C#. C# E2 1D arrays  A collection of objects of the same type  array of integers of size 10 int[] a = new int[10];  The size.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Multidimensional Arrays Computer and Programming.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays Chapter 7.
Arrays and Collections
C++ LANGUAGE MULTIPLE CHOICE QUESTION SET-3
Chapter VII: Arrays.
Test 2 Review Outline.
Chapter 6: Using Arrays.
Understanding Algorithms and Data Structures
C# Programming: From Problem Analysis to Program Design
EGR 2261 Unit 10 Two-dimensional Arrays
BİL527 – Bilgisayar Programlama I
C# Arrays.
Chapter 7: Working with Arrays
Computer Programming BCT 1113
Two-Dimension Arrays Computer Programming 2.
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
Array, Strings and Vectors
A simple way to organize data
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Module 2 Arrays and strings – example programs.
JavaScript: Functions.
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Repeating Instructions And Advance collection
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Can store many of the same kind of data together
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
EKT150 : Computer Programming
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Pointers C#, pointers can only be declared to hold the memory addresses of value types int i = 5; int *p; p = &i; *p = 10; // changes the value of i to.
Introduction To Programming Information Technology , 1’st Semester
Defining methods and more arrays
Can store many of the same kind of data together
Object Oriented Programming in java
Multidimensional Arrays
Type & Typeclass Syntax in function
MSIS 655 Advanced Business Applications Programming
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Data Structures (CS212D) Week # 2: Arrays.
Module8 Multi-dimensional Array
Managing Collections of Data
Arrays Week 2.
Can store many of the same kind of data together
Introduction to Data Structure
Data Structures & Algorithms
Visual Programming COMP-315
C++ Array 1.
Arrays.
Presentation transcript:

C# Programming Arrays in C# Declaring Arrays of Different Types Initializing Array Accessing Array Elements Creating User Interfaces Using Windows Standards Form Application Types and Standards Looking at Forms Week 5

ARRAYS The array is the most common data structure, found in all programming languages. Using an array in C# involves creating an array object of System.Array type, the abstract base type for all arrays. The Array class provides a set of methods for performing tasks such as sorting and searching that programmers had to build by hand in the past.

Arrays An interesting alternative to using arrays in C# is the ArrayList class. An arrayList is an array that grows dynamically as more space is needed. For situations where you can’t accurately determine the ultimate size of an array, or where the size of the array will change quite a bit over the lifetime of a program, an arrayList may be a better choice than an array.

Arrays Arrays are indexed collections of data. The data can be of either a built-in type or a user-defined type. In fact, it is probably the simplest just to say that array data are objects. Arrays in C# are actually objects themselves because they derive from the System.Array class. An array is a declared instance of the System.Array class, so we can use of all the methods and properties of this class when using arrays In C#, arrays can be declared as fixed length or dynamic.

Declaring and Initializing Arrays Arrays are declared using the following syntax: type[] array-name; where type is the data type of the array elements. Here is an example: int[ ] intArray; intArray = new int[5];

Defining arrays of different types In C#, arrays are objects. That means that declaring an array doesn't create an array. After declaring an array, you need to instantiate an array by using the "new" operator. The following syntax defines arrays of different data types double[] doubleArray = new double[5]; char[] charArray = new char[5]; bool[] boolArray = new bool[2]; string[] stringArray = new string[10];

Initializing Arrays Once an array is declared, the next step is to initialize an array. The initialization process of an array includes adding actual data to the array. The following sample syntaxes creates an array of 3 items and values of these items are added when the array is initialized. int[] sArray = new int[3] {1, 3, 5}; Alternative, we can also add array items one at a time as listed in the following code snippet.   int[ ] sArray = new int[3]; sArray[0] = 1; sArray[1] = 3; sArray[2] = 5;

Initializing Arrays The following code declares a static type of array with integer values. int[ ] x=new int[ ] {1,3,4,5,6}; Console.WriteLine(x[3]); Console.ReadLine(); The following code declares a dynamic array with string values.   string[ ] strArray = new string[] { “kamal", “Ali", “Raheel", "Praveen", “Mujeeb" };

Accessing Arrays We can access an array item by passing the item index in the array. The following syntax creates an array of three items and displays those items on the console. int[] staticIntArray = new int[3]; staticIntArray[0] = 1; staticIntArray[1] = 3; staticIntArray[2] = 5;   Console.WriteLine(staticIntArray[0]); Console.WriteLine(staticIntArray[1]); Console.WriteLine(staticIntArray[2]);

Accessing an array using a foreach Loop The foreach control statement is used to iterate through the items of an array. For example, the following code uses foreach loop to read all items of an array of strings. string[ ] strArray = new string[] { “kamal", “Ali", “Raheel", "Praveen", “Mujeeb" };   foreach (string str in strArray) { Console.WriteLine(str); } The following code will access the elements of integer type array foreach (int x in myArray) { Console.WriteLine(x);

Setting and Accessing Array Elements Elements are stored in an array either by direct access or by calling the Array class method SetValue. Direct access involves referencing an array position by index on the left-hand side of an assignment statement: Names[2] = "Raymond"; Sales[19] = 23123; The SetValue method provides a more object- oriented way to set the value of an array element. The method takes two arguments, an index number and the value of the element. names.SetValue["Raymond“,2]; sales.SetValue[23123,5];

Setting and Accessing Array Elements Array elements are accessed either by direct access or by calling the GetValue method. The GetValue method takes a single argument—an index. myName = names[2]; monthSales = sales.GetValue[19]; Console.WriteLine(sales.GetValue[2]); It is common to loop through an array in order to access every array element using a For loop. A frequent mistake programmers make when coding the loop is to either hard-code the upper value of the loop call a function that accesses the upper bound of the loop for each iteration of the loop: (for int i = 0; i <= sales.GetUpperBound(0); i++) totalSales = totalSales + sales[i];

Methods and Properties for Retrieving Array Metadata The Array class provides several properties for retrieving metadata about an array Length: Returns the total number of elements in all dimensions of an array. GetLength: Returns the number of elements in specified dimension of an array Rank: Returns the number of dimensions of an array GetType: Returns the Type of the current array instance

Assignment 2 Write a program in which all the following functions using two different data types of arrays. i.e. length, rank, getlength, get type

Array Types Arrays can be divided into the following four categories. Single-dimensional arrays Multidimensional arrays or rectangular arrays Jagged arrays Mixed arrays.

Single Dimension Arrays Single-dimensional arrays are the simplest form of arrays. These types of arrays are used to store number of items of a predefined type. All items in a single dimension array are stored contiguously starting from 0 to the size of the array -1. The following code declares an integer array that can store 3 items. int[] intArray; intArray = new int[3];

Multi-Dimensional Arrays A multi-dimensional array, also known as a rectangular array is an array with more than one dimension. The form of a multi- dimensional array is a matrix. Syntax int[ , ] xarray=new int[row,col]; Declaring a multi-dimensional array A multi dimension array is declared as following: string[,] mutliDimStringArray; A multi-dimensional array can be fixed-sized or dynamic sized.

Initializing multi-dimensional arrays The following code is an example of fixed-sized multi-dimensional arrays that defines two multi dimension arrays with a matrix of 3x2 and 2x2. The first array can store 6 items and second array can store 4 items. Both of these arrays are initialized during the declaration. int[,] numbers = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } }; string[,] names = new string[2, 2] { { “Isb", “Lhr” }, { “DHA", “Gulbarg" } };

Accessing multi-dimensional arrays A multi-dimensional array items are represented in a matrix format and to access it's items, we need to specify the matrix dimension. The following code shows how to access numbers array defined in the above code. Console.WriteLine(numbers[0,0]); Console.WriteLine(numbers[0, 1]); Console.WriteLine(numbers[1, 0]); Console.WriteLine(numbers[1, 1]); Console.WriteLine(numbers[2, 0]); Console.WriteLine(numbers[2, 2])

Parameter Arrays A parameter array is specified in the parameter list of a method definition by using the keyword ParamArray. The following method definition allows any amount of numbers to be supplied as parameters, with the total of the numbers returned from the method static int sumNums(params int[] nums) { int sum = 0; for(int i = 0; i <= nums.GetUpperBound(0); i++) sum += nums[i]; return sum; }

Jagged Arrays Data comes in various shapes. Sometimes the shape is uneven. The C# language provides jagged arrays, which can store efficiently many rows of varying lengths. Any type of data, reference or value, can be used.

Initializing Jagged Arrays Jagged array can be used, its items must be initialized. The following code initializes a jagged array; intJaggedArray[0] = new int[2]; intJaggedArray[1] = new int[4]; intJaggedArray[2] = new int[6]; We can also initialize a jagged array's items by providing the values of the array's items. The following code initializes item an array's items directly during the declaration.   intJaggedArray[0] = new int[2]{2, 12}; intJaggedArray[1] = new int[4]{4, 14, 24, 34}; intJaggedArray[2] = new int[6] {6, 16, 26, 36, 46, 56 };

Accessing Jagged Arrays We can access jagged array using loop through all of the items of a jagged array. The Length property of an array helps a lot; it gives us the number of items in an array. The following code loops through all of the items of a jagged array and displays them on the screen.   for (int i = 0; i < intJaggedArray3.Length; i++) { System.Console.Write("Element({0}): ", i); for (int j = 0; j < intJaggedArray3[i].Length; j++) System.Console.Write("{0}{1}", intJaggedArray3[i][j], j == (intJaggedArray3[i].Length - 1) ? "" : " "); } System.Console.WriteLine();

Accessing Jagged Arrays using System; class Program { static void Main() int[ ][ ] jagged = new int[3][ ]; jagged[0] = new int[2]; jagged[0][0] = 1; jagged[0][1] = 2; jagged[1] = new int[1]; jagged[2] = new int[3] { 3, 4, 5 }; for (int i = 0; i < jagged.Length; i++) { int[] innerArray = jagged[i]; for (int a = 0; a < innerArray.Length; a++) { Console.Write(innerArray[a] + " "); } Console.WriteLine(); } Console.ReadLine(); } Output "1 2" "0" "3 4 5"

Mixed Arrays Mixed arrays are a combination of multi- dimension arrays and jagged arrays. The mixed arrays type is removed from .NET 4.0.