Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.

Slides:



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

CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
Lecture 05 - Arrays. Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
1 2-D Arrays Overview l Why do we need Multi-dimensional array l 2-D array declaration l Accessing elements of a 2-D array l Declaration using Initializer.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
©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.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Arrays. Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Declare 14 double variables? What about.
1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
Multiple-Subscripted Array
Building Java Programs Chapter 7.5
BUILDING JAVA PROGRAMS CHAPTER 7 Array Algorithms.
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 Rectangular & Jagged Plus: More 1D traversal.
Java Unit 9: Arrays Declaring and Processing Arrays.
One Dimensional Arrays. Declaring references to array objects How would you declare a variable somearray that is an array of ints? int[] somearray;
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.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04.
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.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Crypto Project Sample Encrypted Data: –Turing: CS Public\CryptoProjectData Crypto_Short_Keys_P_U.out Crypto_Long_Keys_O_R.out Bonus +10 points on.
Arrays and Strings Introducing Arrays Declaring Arrays Creating Arrays Initializing Arrays Array of Objects Copying Arrays Multidimensional Arrays Command-Line.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Chapter 6- Arrays. Overview n What are arrays? n Declaring/initializing arrays. n Using arrays. n Arrays details. n Using arrays with classes/ in classes.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 6.
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
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.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Lecture 7: Arrays Tami Meredith. Roadmap Variables Arrays Array indices & Using arrays Array size versus length Two dimensional arrays Sorting an array.
1. Define an array 1 Create reference arrays of objects in Java program 2 Initialize elements of arrays 3 Pass array to methods 4 Return array to methods.
Introduction to Computing Concepts Note Set 21. Arrays Declaring Initializing Accessing Using with Loops Sending to methods.
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.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Array and String.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
ARRAYS Multidimensional realities Image courtesy of
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 7.
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 10.
Two Dimensional Array Mr. Jacobs.
Hassan Khosravi / Geoffrey Tien
ECE Application Programming
CS150 Introduction to Computer Science 1
Multidimensional array
EECE.2160 ECE Application Programming
Arrays in Java.
C++ Array 1.
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Presentation transcript:

Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7

Note Set 7 Overview Arrays Declaring Accessing Using the enhanced for loop Passing Arrays to Methods

Arrays Fixed Length Homogenous type elements are somehow related usually An array is a reference type an array name is a reference variable even though the array holds primitive-typed data. Arrays are zero subscripted

Declarations double[] temperatures; // or double temperatures[]; temperatures = new double[20]; no doubles yet Now we have 20 doubles. int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Initializer List – Array just big enough is allocated and these values are copied into the array

Enhanced For public class ArrayOne { public static void main (String[] args) { int[] tests = {10, 20, 30, 40, 50}; for(int x: tests) { System.out.printf("Val = %d\n", x); } Val = 10 Val = 20 Val = 30 Val = 40 Val = 50 Val = 10 Val = 20 Val = 30 Val = 40 Val = 50 called the parameter

Enhanced For Example Point[] p = new Point[3]; p[0] = new Point(3, 4); p[1] = new Point(5, 6); p[2] = new Point(7, 8); for(Point x:p) x.setLocation(10, 10); for(Point z: p) System.out.println(z.toString()); 3, 4 5, 6 7, 8 p x

Enhanced For Example Point[] p = new Point[3]; p[0] = new Point(3, 4); p[1] = new Point(5, 6); p[2] = new Point(7, 8); for(Point x:p) x.setLocation(10, 10); for(Point z: p) System.out.println(z.toString()); 10, 10 5, 6 7, 8 p x

Enhanced For Example Point[] p = new Point[3]; p[0] = new Point(3, 4); p[1] = new Point(5, 6); p[2] = new Point(7, 8); for(Point x:p) x.setLocation(10, 10); for(Point z: p) System.out.println(z.toString()); 10, 10 7, 8 p x

Enhanced For Example Point[] p = new Point[3]; p[0] = new Point(3, 4); p[1] = new Point(5, 6); p[2] = new Point(7, 8); for(Point x:p) x.setLocation(10, 10); for(Point z: p) System.out.println(z.toString()); 10, 10 p x Can’t do anything fancy with the enhanced for… no skipping elements A copy of value/reference placed in parameter

Exercise Given: int [] x = {40, 99, 100, 88, 28, 32, 101, 89}; Write a snippet of code to print out all elements that are even.

Exercise Given: int [] x = {40, 98, 100, 88, 28, 32, 108, 89}; Write a snippet of code to print out all elements that end in the number 8.

Arrays and Methods Passing an array to a method public static void main (…) { int[] myArr = {1, 2, 3, 4, 5}; System.out.println(sumElements(myArr)); } public static int sumElements(int[] arr) { int sum = 0; for(int x : arr ) sum += x; return sum; } If we modified arr, would the changes be reflected in myArr also?

Return an array from a method You can return an array reference from a method public static void main (…) { int[] myArr = createArray(10, 50); for (int x : myArr) System.out.println(x); } //Creates array filled with ints between //the two args, assuming that a < b public static int createArray(int a, int b) { int[] arr = new int [b – a + 1]; for (int i = a; i <= b; i++) arr[i – a] = i; }

Write Some Code Write a static method that accepts an integer array and returns an array holding any integer between 0 and 10 that is not stored in the parameter array.

Two Dimensional Arrays Similar to a table of values with rows and columns Think of it as an array of 1-D arrays – each row is an array

Declaring a 2 D array int[][] x = new int [10][20]; for (int i = 0; i < x.length; i++) { for (int j = 0; j < x[i].length; j++) x[i][j] = 10; Number of Rows Number of columns in that row int[][] x = new int [2]; x[0] = new int[10]; //??? x[1] = new int[20]; //???

Printing elements from array public static void main (String [] args) { int[][] array1 = {{1, 2, 3}, {4, 5, 6}}; int[][] array2 = {{1, 2}, {3}, {4, 5, 6}}; outputArray (array1); outputArray (array2); } public static void outputArray(int[][]a) { for (int row = 0; row < a.lenght; row++) { for (int col = 0; col < a[row].length; col++) System.out.printf(“%d “, a[row][col]); System.out.println(); }