LISTS AND ARRAYS CHAPTER 22 1. Topics  C# Collections  List –Flexible collection of variable length  Array –Standard arrays  Multidimensional Arrays.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
Arrays.
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
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  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Chapter 9 Introduction to Arrays
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
Games and Simulations O-O Programming in Java The Walker School
Java Unit 9: Arrays Declaring and Processing Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Lec 19 Array Intro. Agenda Arrays—what are they Declaring Arrays Constructing Arrays Accessing Array cells.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
1 CSC 221: Computer Programming I Spring 2008 ArrayLists and arrays  example: letter frequencies  autoboxing/unboxing  ArrayLists vs. arrays  example:
Chapter 8 Arrays and Strings
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
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.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS.
Arrays BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Built-in Data Structures in Python An Introduction.
Arrays Art &Technology, 3rd Semester Aalborg University Programming David Meredith
5. Collections Arrays Other basic data structures.NET collections Class library example.
Research Topics in Computational Science. Agenda Survey Overview.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
1 9/22/05CS360 Windows Programming Arrays, Collections, Hash Tables, Strings.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
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];
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Data Types Chapter 6: Data Types Lectures # 11. Topics Introduction Primitive Data Types Character String Types Array Types Associative Arrays Record.
Array in C# Array in C# RIHS Arshad Khan
Chapter 6: Using Arrays.
CHAPTER 22 LISTS AND ARRAYS 1.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
ARRAYS 1 GCSE COMPUTER SCIENCE.
Object Oriented Programming in java
Grouped Data Arrays, and Array Lists.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Arrays in Java.
CHAPTER 21 LOOPS 1.
Arrays.
Presentation transcript:

LISTS AND ARRAYS CHAPTER 22 1

Topics  C# Collections  List –Flexible collection of variable length  Array –Standard arrays  Multidimensional Arrays  Jagged Lists & Arrays  foreach and Collections  When to Use List or Array  Other Collection Types 2

C# Collections  A collection in C# is a group of several things that are referenced by a single variable  Similar to a pride of lions, parliament of rooks, murder of crows 3 Image from

C# Collections  A collection in C# is a group of several things that are referenced by a single variable  Similar to a pride of lions, parliament of rooks, murder of crows  Two most important C# collections are: –List –Array  Both Lists and arrays do appear in the Inspector  List is the most flexible and easiest to use, so we'll start with List 4

List  Requires a new using line at the top of your script using System.Collections.Generic;  List is a generic collection –Generic collections can work for any data type List sList; // A List of strings List goList; // A List of GameObjects  A List must be defined before it can be used (because Lists default to null ) sList = new List ();  Elements are added to Lists using the Add() method sList.Add("Hello"); sList.Add("World"); 5

List  List elements are accessed via bracket access –Bracket access is zero indexed (i.e., starts at [0] ) print( sList[0] ); // Prints: "Hello" print( sList[1] ); // Prints: "World"  Lists have a Count of the number of elements print( sList.Count ); // Prints: "2"  Lists can be cleared of all elements sList.Clear(); // Empties sList 6

List  All these methods act on the List ["A","B","C","D"] print( sList[0] ); // Prints: "A" sList.Add("Apple"); // ["A","B","C","D","Apple"] sList.Clear(); // [] sList.IndexOf("B"); // 1 ("B" is the 1 st element) sList.IndexOf("Bob"); // -1 ("Bob" is not in the List) sList.Insert(2,"X"); // ["A","B","X","C","D"] sList.Insert(4,"X"); // ["A","B","C","D","X"] sList.Insert(5,"X"); // ERROR!!! Index out of range sList.Remove("C"); // ["A","B","D"] sList.RemoveAt(1); // ["A","C","D"]  Lists can be converted to arrays string[] sArray = sList.ToArray(); 7

Array  Array is a much simpler collection than List –Does not require any using statement at the top of a script  Not actually its own data type, but rather a collection of any data type string[] sArray; // An array of strings GameObject[] goArray; // An array of GameObjects  Arrays are created with a fixed length –Arrays cannot expand to add more elements like Lists can sArray = new string[4]; // An array of four strings sArray = new string[] {"A","B","C","D"}; –Either of these arrays will only ever have a Length of 4 8

Array  Array elements are both accessed and assigned via bracket access sArray[1] = "Bob"; // Assigns "Bob" to the 1 st element print( sArray[1] ); // Prints: "Bob"  It's possible to skip elements in an array –The skipped elements are the default value for that type string[] sArray = new string[4]; // 4-element string array sArray[0] = "A"; // ["A",null,null,null] sArray[2] = "C"; // ["A",null,"C",null]  Arrays have Length instead of Count print( sArray.Length ); // Prints: 4 –This is similar to strings (which are collections of chars) 9

Array  All these methods act on the array ["A","B","C","D"] print( sArray[0] ); // Prints: "A" sArray[2] = "Cow"; // ["A","B","Cow","D"] sArray[10] = "Test"; // ERROR!!! Index out of range sList.Remove("C"); // ["A","B","D"] sList.RemoveAt(1); // ["A","C","D"]  Static methods of the System.Array class print( System.Array.IndexOf(sArray,"B") ); // 1 System.Array.Resize( ref sArray, 6); // Sets Length to 6  Arrays can be converted to Lists List sList = new List ( sArray ); 10

Multidimensional Arrays  Arrays can have more than one dimension string[,] s2D = new string[4,4]; // Makes a 4x4 array s2D[0,0] = "A"; s2D[0,3] = "B"; s2D[1,2] = "C"; s2D[3,1] = "D"; –This would make the 2-dimensional array | A | | | B | | | | C | | | | | | | | D | | | | –Length is still the total length of the array print( s2D.Length ); // Prints: 16 11

Multidimensional Arrays string str = ""; for ( int i=0; i<4; i++ ) { for ( int j=0; j<4; j++ ) { if (s2D[i,j] != null) { str += "|" + s2D[i,j]; } else { str += "|_"; } } str += "|"+"\n"; } print( str ); –This prints: |A|_|_|B| |_|_|C|_| |_|_|_|_| |D|_|_|_| 12

Jagged Lists & Arrays  Both Lists and arrays can be composed of other Lists or arrays string[][] jArray = new string[3][]; // Makes a 3x? array jArray[0] = new string[4]; jArray[0][0] = "A"; jArray[0][3] = "B"; jArray[1] = new string[] {"C","D","E"}; jArray[2] = new string[] {"F","G"}; –This would make the jagged array | A | | | B | | C | D | E | | F | G | –Length is now accurate for each part of the jagged array print( jArray.Length ); // Prints: 4 print( jArray[1].Length ); // Prints: 3 13

foreach and Collections  Lists and arrays can be iterated over using foreach string[] sArray = new string[] {"A","B","C","D"}; string str = ""; foreach (string s in sArray) { str += s; } print( s ); // Prints: "ABCD" List sList = new List ( sArray ); string str2 = ""; foreach (string s in sList) { str2 += s; } print( s2 ); // Prints: "ABCD"  Why can string s be declared twice? –Because string s is local to each foreach loop 14

When to Use List or Array  Each have pros and cons: –List has flexible length, whereas array length is more difficult to change. –Array is very slightly faster. –Array allows multidimensional indices. –Array allows empty elements in the middle of the collection.  Because they are simpler to implement and take less forethought (due to their flexible length), the author tends to use Lists much more often than arrays. –This is especially true when prototyping games, since prototyping requires a lot of flexibility. 15

Other Collection Types  ArrayList –ArrayList is like a List except without a set type –Extremely flexible, but I find them less useful and slower  Dictionary –A key / value pair The key could be a string, like a username The value would be the user data –Used in some of the later tutorials in the book –Requires using System.Collections.Generic; –Very useful –But don't appear properly in the Unity Inspector 16

Chapter 22 – Summary  The collections we use in this book can hold any number of objects (of a single type)  List is the most easily usable collection type –It requires using System.Collections.Generic;  Arrays are less flexible but also very useful –Only arrays can be multidimensional  Both Lists and arrays can be jagged  I generally recommend Lists over arrays  Dictionaries will also be used in this book  Next Chapter: Functions! 17