BUILDING JAVA PROGRAMS CHAPTER 7 Arrays days until the AP Computer Science test.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Arrays.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Lecture 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
1 Various Methods of Populating Arrays Randomly generated integers.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Saturday May 02 PST 4 PM. Saturday May 02 PST 10:00 PM.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
CS102--Object Oriented Programming Review 1: Chapter 1 – Chapter 7 Copyright © 2008 Xiaoyan Li.
BUILDING JAVA PROGRAMS CHAPTER 7 Array Algorithms.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Tallying and Traversing Arrays reading: 7.1 self-checks: #1-9 videos:
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Tallying and Traversing Arrays reading: 7.1 self-checks: #1-9 videos:
CS305j Introduction to Computing Arrays Part 2 1 Topic 20 Arrays part 2 "42 million of anything is a lot." -Doug Burger (commenting on the number of transistors.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Arrays Pepper. What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String.
Building java programs chapter 6
Java Programming: Program Design Including Data Structures 1 Vectors The class Vector can be used to implement a list Unlike an array, the size of a Vector.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
EE 422C Day 2 Java, Eclipse. Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/19/20151.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Building java programs, chapter 3 Parameters, Methods and Objects.
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.
BUILDING JAVA PROGRAMS CHAPTER 7: ARRAYS 1. 2 OUT-OF-BOUNDS INDEXES The indexes that are legal to access in an array are those in the range of 0 to the.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
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.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
CSE 143 Lecture 1 Arrays (review) slides created by Marty Stepp
Topic 21 arrays - part 1 Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from "Should.
CSCI 162 – Introduction to Programming II William Killian
Dr. Kyung Eun Park Summer 2017
Building Java Programs
Building Java Programs
Array traversals, text processing
CS106A, Stanford University
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
Building Java Programs Chapter 7
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Object Oriented Programming in java
Building Java Programs
Can we solve this problem?
python.reset() Also moving to a more reasonable room (CSE 403)
Lecture 9: Arrays Building Java Programs: A Back to Basics Approach
Lecture 10: Arrays AP Computer Science Principles
Suggested self-checks: Section 7.11 #1-11
Can we solve this problem?
Building Java Programs
Building Java Programs
Building Java Programs
File output; Arrays reading: 6.4 – 6.5, 7.1
Why are arrays useful? We can use arrays to store a large amount of data without declaring many variables. Example: Read in a file of 1000 numbers, then.
Can we solve this problem?
Building Java Programs
Presentation transcript:

BUILDING JAVA PROGRAMS CHAPTER 7 Arrays

days until the AP Computer Science test

At the end of this class, you will be able to Explain default values in arrays. Use methods in the Arrays class.

Array Initialization int arr = new int[10]; boolean bArr = new boolean[3]; System.out.println(arr[0]); System.out.println(bArr[0]); What is output? 0 false

Array Initialization Each element initially gets a default “zero equivalent” value. TypeDefault value int0 double0.0 booleanfalse String (or other object) null (means, "no object")

Array Initialization Commonly, we know the values we want to store in an array when it is declared. Example: String[] daysOfTheWeek = new String[7]; daysOfTheWeek[0] = “Sunday”; daysOfTheWeek[1] = “Monday”; daysOfTheWeek[2] = “Tuesday”; daysOfTheWeek[3] = “Wednesday”; daysOfTheWeek[4] = “Thursday”; daysOfTheWeek[5] = “Friday”; daysOfTheWeek[6] = “Saturday”; Lots of code to do something simple.

Quick Array Initialization Syntax: type[] name = {,, …, }; The compiler determines the size of the array by counting values. Example: String[] daysOfTheWeek = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”}; Syntax Yoda

Limitations of arrays You cannot compare arrays with == or equals : int[] a1 = {42, -7, 1, 15}; int[] a2 = {42, -7, 1, 15}; if (a1 == a2) {... } // false! if (a1.equals(a2)) {... } // false! An array does not know how to print itself: int[] a1 = {42, -7, 1, 15}; System.out.println(a1); //

The Arrays class Class Arrays in package java.util has useful static methods for manipulating arrays: Syntax: Arrays.methodName(parameters) Method nameDescription binarySearch(array, value) returns the index of the given value in a sorted array (or < 0 if not found) copyOf(array, length) returns a new copy of an array equals(array1, array2) returns true if the two arrays contain same elements in the same order fill(array, value) sets every element to the given value sort(array) arranges the elements into sorted order toString(array) returns a string representing the array, such as "[10, 30, -25, 17]"

Arrays.toString Arrays.toString accepts an array as a parameter and returns a String representation of its elements. int[] e = {0, 2, 4, 6, 8}; e[1] = e[3] + e[4]; System.out.println("e is " + Arrays.toString(e)); Output: e is [0, 14, 4, 6, 8] Must import java.util.*;

Homework for Chapter 7 HomeworkAssigned onDue on Read BJP 7.1 and write notes11/24/201411/26/2014 Practice It: SC 7.1, 7.2, 7.411/24/201411/26/2014 Practice It: Ex 7.111/24/201411/26/2014 Practice It: SC 7.3, 7.511/25/201411/26/2014 Practice It: Ex 7.211/25/201411/26/2014