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.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 Array and Collections.
Advertisements

Arrays.
Two-Dimensional Arrays Chapter What is a two-dimensional array? A two-dimensional array has “rows” and “columns,” and can be thought of as a series.
Basic Algorithms on Arrays. Learning Objectives Arrays are useful for storing data in a linear structure We learn how to process data stored in an array.
CSC 142 K 1 CSC 142 Multidimensional Arrays [Reading: chapter 10]
C# Types Tom Roeder CS fa. Administration CMS is up let me know if you can’t see the course Assignments are posted may not be able to do some.
©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.
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.
JAVA 1.5 New Features Dr V. The syntax of the enhanced for loop (for each) for (type variableName : arrayName) { statements } arrayName could be any.
Chapter 7: Working with Arrays
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
ArrayLists.  The Java API includes a class named ArrayList. The introduction to the ArrayList class in the Java API documentation is shown below.  java.lang.Object.
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
Java Unit 9: Arrays Declaring and Processing Arrays.
One Dimensional Arrays. Declaring references to array objects How would you declare a variable somearray that is an array of ints? int[] somearray;
2 dimensional arrays Steven Wood ©2005. Arrays dimensions Java allows arrays with many subscripts 2-D examples Chess board Excel spreadsheet.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Arrays and ArrayLists.
Crypto Project Sample Encrypted Data: –Turing: CS Public\CryptoProjectData Crypto_Short_Keys_P_U.out Crypto_Long_Keys_O_R.out Bonus +10 points on.
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 Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
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.
1 nd Semester Module7 Arrays Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Two –Dimensional Arrays Mrs. C. Furman September 18, 2008.
Processing Sequences of Elements Technical Trainer Telerik Corporation Doncho Minkov.
Enhanced For Loops (For Each Loops) Less Code and Less Overhead for Writing For Loops.
5. Collections Arrays Other basic data structures.NET collections Class library example.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Array.  ARRAYS ALLOW US TO STORE ELEMENTS OF SINGLE DATA TYPE CONTAGUISLY IN MEMORY  EACH ELEMENT IS ASSOCIATED WITH AN INDEX OR LOCATION  WE CAN ACCESS.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Lec 13 Oct 21, 02. Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’,
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
IT259 Foundation of Programming Using Java Unit 9 Seminar : (Chapter 8 ) Instructor : Vladimir Gubanov, PhD
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.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
COMP More About Arrays Yi Hong June 05, 2015.
Multidimensional Arrays Computer and Programming.
Arrays and Indexers Programming in C# Arrays and Indexers CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Multidimensional Arrays Vectors of Vectors When One Is Not Enough.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline 1 Introduction 2 Arrays 3Declaring Arrays 4Processing Array Contents 5 Multiple-Subscripted.
Arrays and Collections
Basic arrays, binary search, selection sort by amelia bateman
Java Arrays and ArrayLists COMP T1 #5
Chapter 6: Using Arrays.
Two-Dimensional Arrays
Chapter 7: Working with Arrays
Two Dimensional Array Mr. Jacobs.
C# Programming Arrays in C# Declaring Arrays of Different Types Initializing Array Accessing Array Elements Creating User Interfaces Using Windows Standards.
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Repeating Instructions And Advance collection
Arrays in Java.
Presentation transcript:

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 can be specified at run time. int n = ReadInt(); int[] a = new int[n];  An array is a reference type. It is defined on the heap  As in C++ and Java, the first element has index 0.  To iterate through an array (use the Length property) for(int i=0; i<a.Length; i++) { /* work with a[i] */ }

C# E3 Rectangular arrays  Arrays with 2 or more dimensions  array of strings of size 10 by 20 (10 rows by 20 columns) string[,] a = new string[10,20];  Useful properties and methods Length : total number of elements Rank : total number of dimensions GetLength(int dimension) : number of elements in the specified dimension  To iterate through a rectangular array for(int i=0; i<a.GetLength(0); i++) for(int j=0; j<a.GetLength(1); j++) { /*Work with a[i,j]*/ }

C# E4 Jagged arrays  In a 2D rectangular array all rows have the same number of columns.  If this is not desirable, use a jagged array // an array to store the daily rainfall // during March, April and May double[][] a = new double[3][]; a[0] = new double[31]; // March a[1] = new double[30]; // April a[2] = new double[31]; // May // To iterate for(int i=0; i<a.Length; i++) for(int j=0; j<a[i].Length; j++) { /* Work with a[i][j] */ }  Think of a as an array of arrays.

C# E5 Array initialization  The array initializer must be on the line of declaration int[] a = new int[]{1,2,3}; //shorthand int[] a = {1,2,3}; //rectangular 3x2 array int[,] b = new int[3,2]{{1,2},{3,4},{5,6}}; //shorthand int[,] b = {{1,2},{3,4},{5,6}}; // jagged array int[][] c = new int[2][]{new int[]{1,2,3},new int[]{4,5}}; //shorthand (must use new for the inner //arrays) int[][] c = {new int[]{1,2,3},new int[]{4,5}};

C# E6 foreach loop (1)  A convenient construct to iterate through an array string[] a = new string[10]; /* code to initialize a */... // Iterate through a foreach(string s in a) { /* work with s */ }  How would you iterate through a rectangular or a jagged array with this construct?

C# E7 foreach loop (2)  Rectangular array (same as for 1D array) string[,] a = new string[10,20]; /* code to initialize a */... // Iterate through a foreach(string s in a) { /* work with s */ }  Jagged array string[][] a = new string[2][]; a[0] = new string[5]; a[1] = new string[3]; /* code to initialize a */... // Iterate through a (=array of arrays) foreach(string[] arr in a) foreach(string s in arr) { /* work with s */ }

C# E8 Array class  A class that contains many useful methods to work on arrays  A sampling (see the documentation for more details) // Reverse the order of the elements // of a 1D array a Array.Reverse(a); // Find an object o in a 1D array a // o could be an int, a string... // Note: the array must be sorted int index = Array.BinarySearch(a,o); // Sort a 1D array // Note: the elements of the array // must be comparable. Array.Sort(a);

C# E9 ArrayList class (1)  To get an array whose size can change dynamically, use an ArrayList (in System.Collections)  An ArrayList is a dynamic array of objects.  object is the ultimate base class of all objects in C# (more on this soon). All struct and class instances are of type object.  Thus, you can put anything you want in an ArrayList.

C# E10 ArrayList class (2)  Simple example ArrayList a = new ArrayList(); a.Add(4); // a is {4} a.Add("Hello"); // a is {4,"Hello"} Console.Write(a.Count); // 2 is printed a.Remove(a[0]); // a is {"Hello"} a.Add(Color.Red); //a is {"Hello",Color.Red} string s = (string)a[0]; // s is "Hello" a.Remove(Color.Red); // a is {"Hello"} // could also do a.Remove(a[1]);  See the class web site for a more complete example

C# E11 Iterating through an ArrayList ArrayList a = new ArrayList(); // Initialize a... // To iterate, can use a for loop for(int i=0; i<a.Count; i++){ /* work with a[i] */ } // or a foreach loop foreach(object o in a){ /* work with o */ } // Can also use an IEnumerator IEnumerator ietr = a.GetEnumerator(); while(ietr.MoveNext()) { object o = ietr.Current; /* work with o */ }