COMP 110 More arrays, 2D arrays, Program 4

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Apr 1, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Arrays.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Arrays Chapter 6 Chapter 6.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
©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.
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.
1 COMP 110 More Arrays Tabitha Peck M.S. April 2, 2008 MWF 3-3:50 pm Philips 367.
Building Java Programs Chapter 7.5
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
Java Unit 9: Arrays Declaring and Processing Arrays.
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.
Catie Welsh March 28,  Lab 7 due Friday, April 1st, 1pm 2.
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.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
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.
Week 9 - Wednesday.  What did we talk about last time?  2D arrays  Queen attacking pawn example  Started Game of Life.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.
COMP More About Arrays Yi Hong June 05, 2015.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Catie Welsh April 6,  Lab 8 due, Friday by 1pm 2.
Multidimensional Arrays Vectors of Vectors When One Is Not Enough.
COMP 110: Spring Announcements Lab 7 was due today Binary Expression Assignment due Friday.
COMP 110 More arrays, 2D arrays, Program 4 Luv Kohli November 10, 2008 MWF 2-2:50 pm Sitterson 014.
COMP 110 Arrays Luv Kohli November 5, 2008 MWF 2-2:50 pm Sitterson 014.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
Arrays Chapter 7.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
Chapter 6: Using Arrays.
Two-Dimensional Arrays
Michele Weigle - COMP 14 - Spr 04 Catie Welsh March 30, 2011
COMP Final Exam Review Yi Hong June 15, 2015.
Chapter 8 – Arrays and Array Lists
Two Dimensional Arrays
Two Dimensional Array Mr. Jacobs.
COMP 110 More about inheritance
CS 200 Arrays, Loops and Methods
Two-Dimensional Arrays
COMP 110 Some notes on inheritance, review
COMP 110 Information hiding and encapsulation
Multidimensional Arrays Vectors of Vectors
Michele Weigle - COMP 14 - Spr 04 Catie Welsh April 11, 2011
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Arrays 6-Dec-18.
Multidimensional Arrays
Chapter 7 Part 2 Edited by JJ Shepherd
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
Michele Weigle - COMP 14 - Spr 04
Multidimensional array
Multidimensional Arrays
Announcements Lab 6 was due today Lab 7 assigned this Friday
Multi-Dimensional Arrays
Dr Tripty Singh Arrays.
INC 161 , CPE 100 Computer Programming
Outline Declaring and Using Arrays Arrays of Objects
Multidimensional Arrays Section 6.4
C++ Array 1.
Arrays Imran Rashid CTO at ManiWeber Technologies.
Michele Weigle - COMP 14 - Spr 04
Presentation transcript:

COMP 110 More arrays, 2D arrays, Program 4 Michele Weigle - COMP 14 - Spr 04 COMP 110 More arrays, 2D arrays, Program 4 Catie Welsh April 4, 2011

Michele Weigle - COMP 14 - Spr 04 Announcements Lab 8 due, Friday 1pm Final program, Program 4, assigned today, due Wednesday, April 27th by 11:59pm

Michele Weigle - COMP 14 - Spr 04 Questions? Any other questions?

Michele Weigle - COMP 14 - Spr 04 Today in COMP 110 More about arrays 2D arrays Program 4

Arrays as instance variables public class Weather { private double[] temperature; private double[] pressure; public void initializeTemperature(int len) temperature = new double[len]; }

Arrays of objects When you create an array of objects like this: Student[] students = new Student[35]; Each of the elements of students is not yet an object You have to instantiate each individual one students[0] = new Student(); students[1] = new Student(); …or do this in a loop

Arrays of objects ? 1045 2584 2836 true GREEN 3 false BLUE 1 false Smiley[] smilies = new Smiley[3]; for (int i = 0; i < smilies.length; i++) { smilies[i] = new Smiley(); } ? 1045 2584 2836 true GREEN 3 false BLUE 1 false CYAN 4

Arrays of objects Student[] students = new Student[5]; for (int i = 0; i < students.length; i++) { students[i] = new Student(keyboard.nextInt()); students[i].printAge(); }

Arrays as parameters public void changeArray(int[] arr) { int len = arr.length; arr[len – 1] = 25; } 23 47 52 14 7 25

Arrays as return types public double[] buildArray(int len) { double[] retArray = new double[len]; for (int i = 0; i < retArray.length; i++) retArray[i] = i * 1.5; } return retArray;

Indexed variables as method arguments No different from using a regular variable public void printNum(int num) { System.out.println(num); } public void doStuff() int[] scores = { 15, 37, 95 }; for (int index = 0; index < scores.length; index++) printNum(scores[index]);

2D arrays Arrays having more than one index are often useful Tables Grids Bingo games 0: Open 1: High 2: Low 3: Close 0: Apple Inc. 99.24 99.85 95.72 98.24 1: Walt Disney Co. 21.55 24.20 21.41 23.36 2: Google Inc. 333.12 341.15 325.33 331.14 3: Microsoft Corp. 21.32 21.54 21.00 21.50

Declaring and creating 2D arrays int[][] table = new int[4][3]; or int[][] table; table = new int[4][3];

Declaring and creating 2D arrays int[][] table = new int[4][3]; gives you the ability to use table[0][0] table[0][1] table[0][2] table[1][0] table[1][1] table[1][2] table[2][0] table[2][1] table[2][2] table[3][0] table[3][1] table[3][2]

How do you use a 2D array? We used a loop to iterate over a 1D array int[] scores = { 13, 57, 93, 60, 102 }; for (int i = 0; i < scores.length; i++) { System.out.println(scores[i]); }

How do you use a 2D array? How about a 2D array? Use a nested loop int[][] table = new int[4][3]; Use a nested loop for (int row = 0; row < 4; row++) { for (int column = 0; column < 3; column++) table[row][column] = 37; }

Multidimensional arrays You can have more than two dimensions int[][][] table = new int[4][3][5]; Use more nested loops to access all elements

Multidimensional arrays as parameters public void print2DArray(int[][] arr) { for (int row = 0; row < arr.length; row++) for (int column = 0; column < arr[row].length; column++) System.out.print(arr[row][column] + " "); } System.out.println();

Multidimensional arrays as return types public int[][] giveMeAnArray() { int[][] table = new int[4][3]; // put values in the table return table; }

length for a 2D array int[][] table = new int[4][3]; table.length is the number of rows, or the integer in the first pair of brackets (4) table[i].length is the number of columns, or the integer in the second pair of brackets (3)

Why? Arrays of arrays scores is a one-dimensional array int[] scores = new int[5]; scores is a one-dimensional array base type is int int[][] table = new int[4][3]; table is also in fact a one-dimensional array base type is int[] We still refer to table as a two-dimensional array

Program 4: Memory This program is hard. But you can do it. START EARLY. If you start early, this will be you If you don’t start early, this will be you

Michele Weigle - COMP 14 - Spr 04 Wednesday More array details Go over Lab 7