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.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
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.
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Programming with Collections Collections in Java Using Arrays Week 9.
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 Eight: Arrays 1.Terms and what they mean 2.Types of arrays -One Dimensional arrays -Two Dimensional arrays -ragged arrays -Parallel arrays 3. Methods.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
Chapter 7 & 8- Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 Pearson Education, Inc. All rights reserved Arrays.
Java Unit 9: Arrays Declaring and Processing Arrays.
Lists in Python.
The University of Texas – Pan American
Program structure Four different storage-class specifications: –Automatic (auto): local to a function, normally declared variables are automatic. Does.
chap8 Chapter 8 Arrays (Hanly) chap8 2 Data Structure Simple data types use a simple memory to store a variable. Data Structure: a.
ArrayList, Multidimensional Arrays
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Processing Sequences of Elements Svetlin Nakov Telerik Corporation
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1.00/ Lecture 8 Arrays and Vectors. Arrays-1 Arrays are a simple data structure Arrays store a set of values of the same type – Built-in types.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
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.
1 9/22/05CS360 Windows Programming Arrays, Collections, Hash Tables, Strings.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
List Interface and Linked List Mrs. Furman March 25, 2010.
Processing Sequences of Elements Technical Trainer Telerik Corporation Doncho Minkov.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
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 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Module 1: Array ITEI222 - Advance Programming Language.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
1 Unit-2 Arrays, Strings and Collections. 2 Arrays - Introduction An array is a group of contiguous or related data items that share a common name. Used.
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.
CS 116: Object Oriented Programming II. Topics Vectors Multidimensional Arrays ArrayList.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
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.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Lecture 10 Collections Richard Gesick.
Arrays Chapter 7.
Containers and Lists CIS 40 – Introduction to Programming in Python
Java Arrays. Array Object An array :a container object holds a fixed number of values of a single type. The length of an array is established when the.
Can store many of the same kind of data together
Object Oriented Programming in java
MSIS 655 Advanced Business Applications Programming
Data Structures (CS212D) Week # 2: Arrays.
Arrays Chapter 7.
Arrays in Java.
Data Structures & Algorithms
Arrays.
Presentation transcript:

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 are has System.Array type. So that they could use all functions of System.Array class.

Arrays As a simple example, think of days within a week. This a one dimensional array whose first element is monday and last element is sunday. Another example is, the days within a month forms 2D (two dimensional) array. At horizontal there are the days of week and at horizontal dimention there are weeks. We may think days of a year as a 3D array. 1st dimension is days of week, 2nd dimension is week and 3rd dimension is months.

Arrays One dimensional array is defined with a variable name, type and its length given between square brackets. Exaple, int[] days = new int[ 7 ]; statement defines a one dimensional array named days with seven elements. Since this is a integer array, elements of it will take initial value of zero.

Use of array After defining array, we access its elements with array name and index number within square brackets, e.g. name_of_array[index] In C# language first element in an array is at index zero (0). Forexample, we can access at least 0 and at most 6th index in days array The numbers within square brackets are called index numbers.

Index Numbers day[0]day[1]day[2]day[3]day[4]day[5]day[6] int[] day = new int[7]; day[5] = 1; if( day[5] == 4 ) break; day[5] = day[6] - 1; day[5] = 1; if( day[5] == 4 ) break; day[5] = day[6] - 1; Examples:

Example Variable i will take values from 0 to 6 within for loop

Initialization of arrays As in the case of variables, we could also set initial values to the arrays in the definition phase. static void Main(string[] args) { int[] day = { 0,2,4,6,8,10,11 }; } static void Main(string[] args) { int[] day = { 0,2,4,6,8,10,11 }; }

Initialization of arrays static void Main(string[] args) { int[] day = { 0,2,4,6,8,10,11 }; } [] Compiler counts the integer values and decides the value in the [] brackets is 7 and compiles the program as int[] gun = new int[7] [] Compiler counts the integer values and decides the value in the [] brackets is 7 and compiles the program as int[] gun = new int[7]

Initialization of arrays If we are not assigning initial values to an array, then elements of array will take initial values depending on the type of the array. For example numerical types will be zero and string and other reference types will be “null”. Warning: Remember that a variable of type string having value of null null doesn’t mean that it is an empty string. null means that there is no memory allocated for that variable and its value can not be used in expressions yet.

Example Define an array of double type that has 10 elements, then assign values between 1.0 and Finally swap elements at 0 and 9 with each other. static void Main(string[] args) { double[] arr = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}; 9.0, 10.0}; double tmp; tmp = arr[9]; arr[9] = arr[0]; arr[0] = tmp; }

Exercise Write a program that generates 100 random numbers and puts them to an array. Program should write smallest and biggest numbers in the array to the output. Also program should calculate the mean value of the array and print it to the output.

Exercise - Solution The program below finds maximum and minimum values. Add calculation of mean value feature to the program.

Array.Length() Used in finding the length of the array. Called right after the “.” mark after the array name. int[] myArray = new int[5]; int len = myArray.Length(); Console.WriteLine(len); Output : 5

foreach Loop This is a loop that iterates for each element in a loop. It is forward only. It look like for loop

foreach foreach and for loops can be converted to each other.

Multidimensional Arrays These are arrays with more than one dimension. As an example we can use multidimensional arrays for modeling a chess board.

Standard multidimensional arrays Creation of multidimensional arrays are similar to one dimensional ones. The difference is there is a comma between the square brackets. int [,] numbers; string [,,] lines; As an example, a string type two by three multidimensional array can be created with string[,] array = new string[2,3];

Standard multidimensional arrays array All dimension lengths are equal in standard arrays. Therefore they look like matrices.

Standard multidimensional arrays Lets create a 2D multidimensional array and assign values to its elements in a loop.

Array operations Searching and sorting

Array Operations Some of the most used array functions in programming; IndexOf() LastIndexOf() Sort() Reverse() They are defined within Array class.

IndexOf() - LastIndexOf() IndexOf() : Starts searching from first element and returns the index number of element found. LastIndexOf() : Starts searching from last element and returns the index number of element found. If the element that we are looking for is not in the list, that these function return -1.

Array.IndexOf(), Array.LastIndexOf() Result : 1 Result : 4

Array.Sort() Sorts the elements of the array by ascending order. Result : Ali Mehmet Pınar Zeynep

Array.Reverse() Reverses the order of the elements of the array. Result : Not : Array.Sort and Array.Reverse does not have to be used together.

Problem If we don’t know the size of the array in advance? What if array expands dynamically?

Collections Used in situations of which the length of the array is not known in advance. Or the length of the array grows or shrinks dynamically. They are one dimensional.

ArrayList It is one of the basic collection types. Resides in System.Collections namespace. It has initial length of 16 and for each expansion, its length is multiplied by 2. Stores data as type of object.

ArrayList Creating a new ArrayList: ArrayList list = new ArrayList(); Example

ArrayList Add Capacity Clear Count IndexOf Insert RemoveAt Reverse Sort TrimToSize Important functions and properties

Exercise Define an ArrayList to hold the names of staff. Add the names; Ahmet, Mehmet, Pınar, Yeşim, Utku, Sinan, Fatma, Ayşe to the list. Print the list and its size to output. Delete the elements which are stating with letter ‘A’ from the list Sort the list with reverse order. Print the list and its size to output.

Exercise Solution