Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Arrays, A1 COMP 401, Fall 2014 Lecture 4 8/28/2014.
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
START DEFINITIONS values (3) N1 = (8, 1,-9) i N1 average N3,2 sum N2 = 0 temp N1 Do not guess or assume any values! Follow the values of the variables.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Week 6: Arrays 1.  Loops are great  But, without a way to talk about a group of values, we can’t get the full potential out of a loop  Enter: the array.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Arrays part 3 Multidimensional arrays, odds & ends.
Arrays Chapter 6 Chapter 6.
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.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
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.
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
Java Unit 9: Arrays Declaring and Processing Arrays.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
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.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
1 Chapter 8 Multi-Dimensional Arrays. 2 1-Dimentional and 2-Dimentional Arrays In the previous chapter we used 1-dimensional arrays to model linear collections.
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.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
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.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
CS107 References and Arrays By Chris Pable Spring 2009.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 11/06/2012 and 11/07/2012 -Test 3 Study Guide.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Week 9 - Monday.  What did we talk about last time?  Method practice  Lab 8.
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
Arrays.
Week 9 - Wednesday.  What did we talk about last time?  2D arrays  Queen attacking pawn example  Started Game of Life.
Arrays.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
int [] scores = new int [10];
Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays days until the AP Computer Science test.
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.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
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.
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
Arrays in 60 seconds err.. minutes
Chapter 6 Arrays.
An Introduction to Java – Part I, language basics
OBJECT ORIENTED PROGRAMMING II LECTURE 13_2 GEORGE KOUTSOGIANNAKIS
Single-Dimensional Arrays chapter6
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Arrays Wellesley College CS230 Lecture 02 Thursday, February 1
Week 7 - Monday CS 121.
Presentation transcript:

Arrays CS177 (Week 06)

Announcements ● Project 2 due today ● Project 3 will be posted tomorrow

Questions?

Definitions ● Homogeneous – If you have an array A, then A[0] = 0 and A[1] = “string” is not valid ● Static – Size has to be specified when creating the array

Creating an array ● int[] age; – Creates a variable 'age' which can be used to reference an array of int type. No data can be stored in it ● age = new int[10]; – Now this variable can be used to store data. This array can have 10 int values. ● These two instructions are equivalent to ● int[] age = new int[10];

Accessing data in an array ● To store a value in an array at some position, do : age[position] = value; – So, if you want the first value in the array 'age' as '1', then do : age[0] = 1; ● Note args was an array of String type. ● Same as args, if you want to access a value in 'age', you write age[position] ● Remember that in java most of the time numbering starts with 0. i.e. the first value in 'age' is age[0] and not age[1]

cont... ● This is one way of initializing and assigning values to an array: ● String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; ● In this example, days is an array of size 7.

Length of array ● To get the length of String str, we used str.length() ● To get length of an array vector, we use vector.length ● Note, there are no parentheses after length

default value ● When you declare an array of type int, double, boolean or char, java assigns a default value to all the elements of the array. ● The default value is 0 for int and double, false for boolean and '\0' for char ● For others (like String), you have to explicitly assign some value

loops on arrays ● One of the most used combinations ● An example for summing all arguments provided by user (java Sum ) ● int sum = 0; ● for(int i = 0; i < args.length; i++) ● sum += Integer.parseInt(args[i]); ● Note that its a standard practice that you use array.length even if you know the length of array.

Swapping values ● Suppose you have 2 variables int a and int b. And you want to swap their values. Below is a sample code: ● int a = 5; ● int b = 6; ● int temp; ● temp = a; //temp = 5 ● a = b; //a = 6 ● b = temp; //b = 5

Swapping values ● Similarly, in case of arrays, if we want to swap element i and j, it will be : ● int temp; ● temp = vector[i]; ● vector[i] = vector[j]; ● vector[j] = temp;

2D arrays ● Suppose you want to store coordinates of a car at 10 different times. How do we do that? ● We use 2 dimensional arrays ● int[][] coordinates; ● coordinates = new int[10][2]; ● Or, equivalently, int[] coordinates = new int[10][2];

Sample 2D array ● Below is a code to take data from user to fill the coordinates array; ● for(int i = 0; i < coordinates.length; i++) { ● coordinates[i][0] = StdIn.readInt(); ● coordinates[i][1] = StdIn.readInt(); ● }

Another example from lecture int[][] table = new int[5][10]; int label = 1; for(int i = 0; i < 5; i++) for(int j = 0; j < 10; j++) { table[i][j] = label; label++; } ● What is the value of table[i][j]? (i * 10 + j + 1)

cont... ● Let us go though the loop sequentially: ● Initially, i = 0 and j = 0 and label = 1 then table[0][0] = 1 ● Then i = 0, j = 1; label = 2, so table[0][1] = 2 ● So on, then i = 0; j = 9; label = 10, so table[0][1] = 10 ● Then i = 1, j = 0, label = 11, so table[1][0] = 11; ● So on, then i = 1 = 1, j = 9, label = 20, so table[1][9] = 20

cont.. ● What happens when we swap the two for loops: ● for(int j = 0; j < 10; j++) ● for(int i = 0; i < 5; i++) { ● table[i][j] = label; ● label++; ● } ● table[i][j] = j * 5 + i = 1

Liar ● There is nothing called multidimensional arrays ● Its actually arrays of arrays (of arrays....) ● Think of int[] as some datatype say intA, then int[][] is actually intA[], i.e. Array of data type int[] ● So, actually, you can decide size of one dimension and then separately decide size of other dimension

Ragged arrays ● Example: ● int[][] pharma = new int[5][]; ● for(int i = 0; i < 5; i++) ● pharma[i] = new int[i+1];

cont.... ● Result:

Use of ragged arrays ● Suppose you want to store courses students are doing. ● Fixed number of students, but each student can be doing different number of courses ● Ragged arrays save space.

>2 dimensions? ● Example of 3D array: ● int[][][] rubiksCube = new int[3][3][3]; ● int count = 1; ● for(int i = 0; i < rubiksCube.length; i++) ● for(int j = 0; j < rubiksCube[i].length; j++) ● for(int k = 0; k < rubiksCube[i][j].length; k++) ● rubiksCube[i][j][k] = count++;

cont.. ● Notice rubriksCube[i][j].length and rubriksCube[i].length ● As we discussed earlier, multidimensional arrays are arrays of arrays, so rubriksCube[i] is an array and so is rubriksCube[i][j] ● Is rubriksCube[i][j][k] an array?

(dis)advantages of multidimensional arrays ● Sometimes, having higher dimensions are troublesome ● In general, with an addition of a dimension, you include another loop. 5 dimension means 5 loops. 5 loops means messy code ● Linus Torvalds (The father of awesome Linux) says: If you are using more than 3 loops, you are doing it wrong. ● But sometimes you do need more loops. ● How many elements are there int[5][10[100]?

Questions