Suggested self-checks: Section 7.11 #12-29

Slides:



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

Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
1 CSE 142 Lecture Notes File input using Scanner Suggested reading: , Suggested self-checks: Section 6.7 # 1-11, These lecture.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
1 More on Arrays Arrays of objects Command line arguments The ArrayList class Javadoc Review Lecture 8 notes and L&L 7.1 – 7.2 Reading for this lecture:
1 CSE 142 Lecture Notes Interactive Programs with Scanner Chapter 4 Suggested reading: Suggested self-checks: Section 4.10 #1-4 These lecture.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-1: Classes and Objects reading: self-checks: #1-9 exercises: #1-4.
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.
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.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-1: Classes and Objects reading: self-checks: #1-9 exercises: #1-4.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
EE 422C Day 2 Java, Eclipse. Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
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.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
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.
More on Arrays Review of Arrays of ints, doubles, chars
Building Java Programs
Building Java Programs
Dr. Kyung Eun Park Summer 2017
Building Java Programs
Building Java Programs
Building Java Programs
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
Array traversals, text processing
Building Java Programs
Defining New Types of Objects, part 3
Building Java Programs
CSE 143 Lecture 1 Review: Arrays and objects
Building Java Programs
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
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
CSE 143 Lecture 4 More ArrayIntList:
slides created by Marty Stepp
References and objects
Building Java Programs
CSE 142 Lecture Notes Defining New Types of Objects
Building Java Programs
Building Java Programs
What's next? Should you major in Computer Science?
Value and reference semantics; Arrays as parameters
CSE 142 Lecture Notes Global Constants, Parameters, Return Values
Suggested self-checks: Section 7.11 #1-11
Can we solve this problem?
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Building Java Programs
CSE 143 Lecture 2 ArrayIntList, binary search
Building Java Programs
Building Java Programs
File output; Arrays reading: 6.4 – 6.5, 7.1
Building Java Programs
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
Lecture 6: References and linked nodes reading: 16.1
CSE 143 Lecture 4 Implementing ArrayIntList reading:
Chapter 6 Arrays.
Building Java Programs
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Presentation transcript:

Suggested self-checks: Section 7.11 #12-29 CSE 142 Lecture Notes Arrays, continued Suggested reading: 7.6 - 7.9 Suggested self-checks: Section 7.11 #12-29 These lecture notes are copyright (C) Marty Stepp 2005. May not be rehosted, copied, sold, or modified without Marty Stepp's expressed written permission. All rights reserved.

String methods with arrays These String methods return arrays: String s = "long book"; Method name Description Example toCharArray() separates this String into an array of its characters s.toCharArray() returns {'l', 'o', 'n', 'g', ' ', 'b', 'o', 'o', 'k'} split(delimiter) separates this String into substrings by the given delimiter s.split(" ") returns {"long", "book"} s.split("o") returns {"l", "ng b", "", "k"}

Graphics methods w/ arrays These Graphics methods use arrays: int[] xPoints = {10, 30, 50, 70, 90}; int[] yPoints = {20, 50, 35, 90, 15}; g.setColor(Color.GREEN); g.drawPolyline(xPoints, yPoints, 5); Method name drawPolygon(int[] xPoints, int[] yPoints, int length) drawPolyline(int[] xPoints, int[] yPoints, int length) fillPolygon(int[] xPoints, int[] yPoints, int length)

Arrays of objects Recall: when you construct an array of primitive values like ints, the elements' values are all initialized to 0. What is the equivalent of 0 for objects? When you construct an array of objects (such as Strings), each element initially stores a special reference value called null. null means 'no object' Your program will crash if you try to call methods on a null reference. String[] words = new String[5]; index 1 2 3 4 value null

The dreaded 'null pointer' Null array elements often lead to program crashes: String[] words = new String[5]; System.out.println(words[0]); words[0] = words[0].toUpperCase(); // kaboom! Output: null Exception in thread "main" java.lang.NullPointerException at ExampleProgram.main(DrawPolyline.java:8) The array elements should be initialized somehow: for (int i = 0; i < words.length; i++) { words[i] = "this is string #" + (i + 1); } words[0] = words[0].toUpperCase(); // okay now

Command-line arguments command-line arguments: If you run your Java program from the Command Prompt, you can write parameters after the program's name. The parameters are passed into main as an array of Strings. public static void main(String[] args) { for (int i = 0; i < args.length; i++) { System.out.println("arg " + i + ": " + args[i]); } Usage: java ExampleProgram how are you? arg 0: how arg 1: are arg 2: you?

Output to files Java has an object named PrintStream (in the java.io package) that allows you to print output into a destination, which can be a file. System.out is also a PrintStream, so any methods you have used on System.out (such as print, println) will work on every PrintStream. Printing into an output file, general syntax: PrintStream <name> = new PrintStream( new FileOutputStream("<file name>")); ... Example: PrintStream output = new PrintStream( new FileOutputStream("output.txt")); output.println("Hello, file!");

Java's Arrays class The Arrays class in package java.util has several useful static methods for manipulating arrays: Method name Description binarySearch(array, value) returns the index of the given value in this array (-1 if not found) equals(array1, array2) whether the two given arrays contain exactly the same elements in the same order fill(array, value) sets every element in the array to have the given value sort(array) arranges the elements in the array into ascending order

Arrays class example Searching and sorting numbers in an array: int[] numbers = {23, 13, 480, -18, 75}; int index = Arrays.binarySearch(numbers, -18); System.out.println("index = " + index); Output: index = 3 Arrays.sort(numbers); index = Arrays.binarySearch(numbers, -18); System.out.println("index = " + 0); for (int i = 0; i < numbers.length; i++) { System.out.print(numbers[i] + " "); } -18 13 23 75 480