Arrays in 60 seconds err.. minutes

Slides:



Advertisements
Similar presentations
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.
Advertisements

Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.
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.
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.
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.
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
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 Chapter 7.
Chapter 8 Arrays and the ArrayList Class Introduction to Arrays.
1 Chapter 7 Multidimensional Arrays. 2 Motivations You can use a two-dimensional array to represent a matrix or a table.
Processing Sequences of Elements
CSC 211 Java I for loops and arrays.
Arrays Chapter 7.
Chapter 8 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Introduction to programming in java
A simple way to organize data
Motivations Thus far, you have used one-dimensional arrays to model linear collections of elements. You can use a two-dimensional array to represent a.
Week 9 - Monday CS 121.
Chapter 6 Arrays.
Chapter 8 Multidimensional Arrays
CSC 142 Computer Science II
DAYS OF THE WEEK.
CS 177 Week 15 Recitation Slides
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
Building Java Programs Chapter 7
Chapter 6 Arrays Solution Opening Problem
An Introduction to Java – Part I, language basics
Chapter 8 Multidimensional Arrays
Chapter 8 Multi-Dimensional Arrays
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
CS-161 Computer Programming Lecture 14: Arrays I
Chapter 7 Multidimensional Arrays
Review of Arrays and Pointers
INC 161 , CPE 100 Computer Programming
Chapter 8 Multidimensional Arrays
Introduction To Programming Information Technology , 1’st Semester
Chapter 8 Multidimensional Arrays
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
python.reset() Also moving to a more reasonable room (CSE 403)
Multidimensional Arrays
CS2011 Introduction to Programming I Arrays (I)
Chapter 8 Multidimensional Arrays
Data Structures (CS212D) Week # 2: Arrays.
Arrays Chapter 7.
CISC124 Labs start this week in JEFF 155.
OBJECT ORIENTED PROGRAMMING II LECTURE 13_2 GEORGE KOUTSOGIANNAKIS
Single-Dimensional Arrays chapter6
Chapter 8 Multidimensional Arrays
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Chapter 7 Multidimensional Arrays
CSS161: Fundamentals of Computing
Arrays.
Chapter 7 Multidimensional Arrays
Consider Write a program that prompts a user to enter the number of students and then, their names and grades. The program will then outputs the average.
Contact
Arrays Wellesley College CS230 Lecture 02 Thursday, February 1
Chapter 7 Multidimensional Arrays
2011年 5月 2011年 6月 2011年 7月 2011年 8月 Sunday Monday Tuesday Wednesday
Chapter 8 Multidimensional Arrays
Arrays.
Week 7 - Monday CS 121.
Presentation transcript:

Arrays in 60 seconds err.. minutes CS177 (Week 06)

Announcements Project 1 will be graded by Monday Project 2 due in coming Wednesday

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 2 3 4 5 12 3) 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 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