Arrays Art &Technology, 3rd Semester Aalborg University Programming https://art.moodle.aau.dk/course/view.php?id=33 David Meredith

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
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.
Lesson Four: More of the Same
 9: Arrays  Why?  What is an Array?  Declaring and Creating an Array  Initializing and Array  Array Operations  Array Examples  Arrays of Objects.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Lecture 7: Arrays Yoni Fridman 7/9/01 7/9/01. OutlineOutline ä Back to last lecture – using the debugger ä What are arrays? ä Creating arrays ä Using.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
©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.
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.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and 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.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Chapter 9 Introduction to Arrays
 Pearson Education, Inc. All rights reserved Arrays.
More Arrays Length, constants, and arrays of arrays By Greg Butler.
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.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
Arrays After a while. Remember for loops? for(int i = 0; i < 10; i++){ System.out.println(i); } -But what if we are unsure of how many cycles we need?
Lec 19 Array Intro. Agenda Arrays—what are they Declaring Arrays Constructing Arrays Accessing Array cells.
Lists in Python.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Functions Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
 Pearson Education, Inc. All rights reserved Arrays.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Review of ArT3 Programming Course David Meredith Aalborg University
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
LISTS AND ARRAYS CHAPTER Topics  C# Collections  List –Flexible collection of variable length  Array –Standard arrays  Multidimensional Arrays.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
By Jonathan Villanueva. Bubble Sorting: the way to sort an array by switching two values that are right next to each other if the first number bigger.
CS 31 Discussion, Week 8 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:00-1:00pm.
Variables Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
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.
 MergeSort is a sorting algorithm which is more on the advanced end.  It is very fast, but unfortunately uses up a lot of memory due to the recursions.
1 BUILDING JAVA PROGRAMS CHAPTER 7.2 ARRAY TRAVERSAL ALGORITHMS.
Review Array – int[] diameters = new int[10]; – diameters[0], diameters[2], diameters[9] – diameters.length Indexing starts at 0 A way to have a collection.
Arrays.
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
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.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Review Objects – data fields – constructors – Methods Classes.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays. 2 Why do we care (about arrays)? What if you have a whole bunch of cars (or aliens or balls or ???) bouncing around the screen? How do we keep.
CS 115 Lecture 17 2-D Lists Taken from notes by Dr. Neil Moore.
Lecture 8: Collections, Comparisons and Conversions. Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
Objects Art &Technology, 3rd Semester Aalborg University Programming David Meredith
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
David Meredith Aalborg University
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
JavaScript: Functions.
Data Structures – 1D Lists
CS2011 Introduction to Programming I Arrays (I)
MSIS 655 Advanced Business Applications Programming
Suggested self-checks: Section 7.11 #1-11
Java SE 7 One and Multi Dimensional Arrays Module 6
Week 7 - Monday CS 121.
Presentation transcript:

Arrays Art &Technology, 3rd Semester Aalborg University Programming David Meredith

Reading Chapter 9 of Shiffman, Learning Processing

Overview What is an array? Declaring an array Initialization Array operations – using the ‘for’ loop Arrays of objects

Making lots of similar objects When we wanted two different cars, we wrote Car myRedCar = new Car(…); Car myBlueCar = new Car(…); What if we want 1000 different cars? Car car1 = new Car(…); Car car2 = new Car(…); … Car car1000 = new Car(); – This is extremely tedious and error-prone

Arrays Can use an array to store any number of objects of the same type, e.g., – 100 Car objects – 10 Fish objects in an aquarium simulation – 52 Card objects in a Whist game – 22 Player objects in a football game An array is a list of n boxes, all the same size, all designed to hold the same type or class of thing, numbered from 0 up to n – 1 – Counting from zero is actually quite useful – consider the % operator…

Declaring an array variable In Processing, you have to declare a variable before you use it, e.g., int x; float y; Car myCar; You also have to declare an array before you use it You declare an array by putting a pair of empty square brackets, [], immediately after the type of object that the array will hold, e.g., int[] myArrayOfInts; float[] myArrayOfFloats; This just declares the array, it does not construct it!

Constructing an array Once an array has been declared, you can actually make it When you make an array, you set its size and this stays fixed throughout the life of the array Create (construct) an array as follows: int[] arrayOfInts = new int[10]; (An array of 10 ints) float[] arrayOfFloats = new float[42]; (An array of 42 floats) Car[] arrayOfCars = new Car[27]; (An array of 27 Car objects)

Initializing an array Having declared and constructed an array, now we want to store some objects in it We can do this the hard way: int[] arrayOfInts = new int[3]; arrayOfInts[0] = 10; arrayOfInts[1] = 9; arrayOfInts[2] = 8; We refer to an element in an array using the format, arrayName[n], where – arrayName is the name of the array variable – n is the index of the element in the array that we want to access, modify or set

Initializing array values all at once Set all the values all at once: int[] intArray = {10,9,8,7,6,5,4,3,2,1}; float[] floatArray = {5.6, 3.141, 0.618, 2.3}; This method isn’t scalable – it isn’t practical for very large arrays as you still have to manually say what goes into each box

Initializing an array with a loop The fastest way to initialize a very large array is to use a loop – Condition is that we need to be able to calculate the value that goes into every box in the array Create an array with 1000 random numbers between 0 and 1000 using a while loop: int i = 0; int[] array = new int[1000]; while (i < 1000) { array[i] = random(0,1000); i++; } Same thing with a for loop: int[] array = new int[1000]; for(int i = 0; i < 1000; i++) array[i] = random(0,1000);

Finding the number of elements in an array: array.length If we think we might want to change the size of an array in some later incarnation of the program, and we iterate through an array several times in a program, then it’s better not to “hard code” the size of the array at each point it is used For example, if we hard code the array length, int[] array = new int[1000]; for(int i = 0; i < 1000; i++) array[i] = random(0,1000); Then if we change the size to 2000, we have to change 1000 to 2000 in two different places Instead, should simply find the length of the array: int[] array = new int[1000]; for(int i = 0; i < array.length; i++) array[i] = random(0,1000); Now we only have to change the size in one place (where the array is constructed)

array.length Note that array.length uses the “dot” syntax, where we give the name of a variable, then write a dot, then write either – the name of the instance variable inside the object whose value we want or – the method inside the object that we want to call For example: myRedCar.display() array.length

Using an array to store recent mouse positions

1000 cars!

Interactive Stripes

Array functions There exist a number of useful functions for manipulating arrays: – shorten(), concat(), subset(), append(), splice(), expand(), sort(), reverse() Look up these functions in the reference!!!