C# Arrays.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

For use of IST410 Students only Arrays-1 Arrays. For use of IST410 Students only Arrays-2 Objectives l Declaring arrays l Instantiating arrays l Using.
1 2-D Arrays Overview l Why do we need Multi-dimensional array l 2-D array declaration l Accessing elements of a 2-D array l Declaration using Initializer.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
1.  Collections are data structures that holds data in different ways for flexible operations  C# Collection classes are defined as part of the ◦ System.Collections.
Arrays. Arrays in Java  Arrays in Java are objects.  Like all objects are created with the new keyword.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
VB and C# Programming Basics. Overview Basic operations String processing Date processing Control structures Functions and subroutines.
Section 3 - Arrays and Methods. Arrays Array: collection of group of data variables of same type, sharing the same name for convenience - Easy to search.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
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 in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
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 An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Arrays Chapter 7.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter 6: Using Arrays.
C# — Console Application
Two-Dimensional Arrays
EEC-693/793 Applied Computer Vision with Depth Cameras
Multidimensional Arrays
Computer Programming BCT 1113
Two-Dimension Arrays Computer Programming 2.
Fifth Lecture Arrays and Lists
C# Programming Arrays in C# Declaring Arrays of Different Types Initializing Array Accessing Array Elements Creating User Interfaces Using Windows Standards.
Advanced Programming Chapter 8: Arrays
EEC-693/793 Applied Computer Vision with Depth Cameras
Yong Choi School of Business CSU, Bakersfield
Java How to Program, Late Objects Version, 10/e
Pass by Reference, const, readonly, struct
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
7 Arrays.
Chapter 8 Multi-Dimensional Arrays
Chapter 8 Slides from GaddisText
Arrays 6-Dec-18.
Introduction To Programming Information Technology , 1’st Semester
Multidimensional Arrays
CS2011 Introduction to Programming I Arrays (I)
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
MSIS 655 Advanced Business Applications Programming
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Module8 Multi-dimensional Array
Multidimensional array
Arrays ICS2O.
7 Arrays.
Arrays in Java.
When an argument to method is an entire array or an individual element of reference type, the called method receives a copy of reference. However an argument.
Thanachat Thanomkulabut
C++ Array 1.
Arrays.
Presentation transcript:

C# Arrays

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 exist 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#. int[] table; // not int table[];

Another detail is that the size of the array is not part of its type as it is in the C language. This allows you to declare an array and assign any array of int objects to it, regardless of the array's length. // declare numbers as an int array of any size int[] numbers; // numbers is a 10-element array numbers = new int[10]; // now it's a 20-element array numbers = new int[20]; In fact, providing an explicit size generates a compile-time error—for example, string [] text; // OK string [ 10 ] text; // error

An attempt to access an undefined element, such as the following indexing of an undefined fifth element for our messages array: messages[ 6 ] = "Well, OK: one more try";; results in a runtime exception being thrown (IndexOutofRange Exception)rather than a compile-time error:

string [,] two_dimensions; string [,,] three_dimensions; string [,,,] four_dimensions;

Declaring Arrays C# supports single-dimensional arrays, multidimensional arrays (rectangular arrays), and array-of-arrays (jagged arrays). The following examples show how to declare different kinds of arrays: Single-dimensional arrays: int[] numbers; Multidimensional arrays: int[,] matrix; Array-of-arrays (jagged): byte[][] scores;

Creating Arrays Declaring them (as shown above) does not actually create the arrays. In C#, arrays are objects and must be instantiated. The following examples show how to create arrays: Single-dimensional arrays: int[] numbers = new int[5]; Multidimensional arrays: int[,] matrix = new int[5,4];

Array-of-arrays (jagged): byte[][] scores = new byte[5][]; for (int x = 0; x < scores.Length; x++) { scores[x] = new byte[4]; } You can also have larger arrays. For example, you can have a three-dimensional rectangular array: int[,,] buttons = new int[4,5,3];

Eg prgm using System; class DeclareArraysSample { public static void Main() { // Single-dimensional array int[] numbers = new int[5]; // Multidimensional array string[,] names = new string[5,4]; // Array-of-arrays (jagged array) byte[][] scores = new byte[5][];

// Create the jagged array for (int i = 0; i < scores.Length; i++) { scores[i] = new byte[i+3]; } // Print length of each row Console.WriteLine("Length of row {0} is {1}", i, scores[i].Length);

Output Length of row 0 is 3 Length of row 1 is 4 Length of row 2 is 5 Length of row 3 is 6 Length of row 4 is 7

Initializing Arrays C# provides simple and straightforward ways to initialize arrays at declaration time by enclosing the initial values in curly braces ({}). The following examples show different ways to initialize different kinds of arrays Single-Dimensional Array int[] numbers = new int[5] {1, 2, 3, 4, 5}; string[] names = new string[3] {"Matt", "Joanne", "Robert"};

You can omit the size of the array, like this: int[] numbers = new int[] {1, 2, 3, 4, 5}; string[] names = new string[] {"Matt", "Joanne", "Robert"}; You can also omit the new operator if an initializer is provided, like this: int[] numbers = {1, 2, 3, 4, 5}; string[] names = {"Matt", "Joanne", "Robert"};

Multidimensional Array-initialization int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} }; string[,] siblings = new string[2, 2] { {"Mike","Amy"}, {"Mary","Albert"} }; You can omit the size of the array, like this: int[,] numbers = new int[,] string[,] siblings = new string[,]

You can also omit the new operator if an initializer is provided, like this: int[,] numbers = { {1, 2}, {3, 4}, {5, 6} }; string[,] siblings = { {"Mike", "Amy"}, {"Mary“, "Albert"} };

Jagged Array (Array-of-Arrays) You can initialize jagged arrays int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} }; You can also omit the size of the first array, like this: int[][] numbers = new int[][] Or int[][] numbers ={ new int[] {2,3,4}, new int[] {5,6,7,8,9} };

Accessing Array Members Accessing array members is straightforward and similar to how you access array members in C/C++. For example, the following code creates an array called numbers and then assigns a 5 to the fifth element of the array: int[] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; numbers[4] = 5; The following code declares a multidimensional array and assigns 5 to the member located at [1, 1]: int[,] numbers = { {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10} }; numbers[1, 1] = 5;

The following is a declaration of a single-dimension jagged array that contains two elements. The first element is an array of two integers, and the second is an array of three integers: int[][] numbers = new int[][] { new int[] {1, 2}, new int[] {3, 4, 5} }; The following statements assign 58 to the first element of the first array and 667 to the second element of the second array: numbers[0][0] = 58; numbers[1][1] = 667;

Arrays are Objects 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. An example of this would be using the Length property to get the length of an array. : int[] numbers = {1, 2, 3, 4, 5}; int LengthOfNumbers = numbers.Length; The System.Array class provides many other useful methods/properties, such as methods for sorting, searching, and copying arrays.

Using foreach on Arrays C# also provides the foreach statement. This statement provides a simple, clean way to iterate through the elements of an array. For example, the following code creates an array called numbers and iterates through it with the foreach statement: int[] numbers = {4, 5, 6, 1, 2, 3, -2, -1, 0}; foreach (int i in numbers) { System.Console.WriteLine(i); }

With multidimensional arrays, you can use the same method to iterate through the elements, for example: int[,] numbers = new int[3, 2] {{9, 99}, {3, 33}, {5, 55}}; foreach(int i in numbers) { Console.Write("{0} ", i); } The output of this example is: 9 99 3 33 5 55 However, with multidimensional arrays, using a nested for loop gives you more control over the array elements.

Foreach on jagged arrays foreach (int[] ele in scores) { for (int j = 0; j < ele.Length; j++) Console.WriteLine(ele[j]); }

Jagged Arrays, eg,… // Declare the array of two elements: int[][]intarr = new int[2][]; // Initialize the elements: intarr[0] = new int[10] {1,5,10,15,20,25,30,35,40,45}; intarr[1] = new int[5] {1,3,6,9,12}; // Display the array elements: for (int i=0; i < 2; i++) { Console.Write("Element({0}): ", i); for (int j=0; j < intarr[i].Length; j++) Console.Write("{0} ", intarr[i][j]); Console.WriteLine(); }

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication3 { class Class1 static void Main(String[] args) int[][] b = new int[3][]; for (int i = 0; i < 3; i++) b[i] = new int[i + 1]; for (int j = 0; j < b[i].Length; j++) b[i][j] = int.Parse(Console.ReadLine()); } Console.WriteLine(); Console.Write(" "+b[i][j]); Console.ReadLine();