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.

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

Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
For use of IST410 Students only Arrays-1 Arrays. For use of IST410 Students only Arrays-2 Objectives l Declaring arrays l Instantiating arrays l Using.
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 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
CS102--Object Oriented Programming Lecture 5: – Arrays – Sorting: Selection Sort Copyright © 2008 Xiaoyan Li.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
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:
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
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:
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-1: Classes and Objects reading: self-checks: #1-9 exercises: #1-4.
Java Unit 9: Arrays Declaring and Processing Arrays.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
7. Arrays. Topics Declaring and Using Arrays Some Array Algorithms Arrays of Objects Variable Length Parameter Lists Two-Dimensional Arrays The ArrayList.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
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.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
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.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
EE 422C Day 2 Java, Eclipse. Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Simple Arrays Arrays of primitives and Strings Sections 7.1, 7.2.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
1 Introduction  Algorithms  Data structures  Abstract data types  Programming with lists and sets © 2008 David A Watt, University of Glasgow Algorithms.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Chapter 4 Generic Vector Class. Agenda A systemic problem with Vector of Object – Several approaches at a solution – Generic structures Converting classes.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
Java Part I By Wen Fei, HAO. Program Structure public class ClassName { public static void main(String[] args) { program statements } user defined methods.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
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.
Arrays Chapter 7. 2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays days until the AP Computer Science test.
Topic 23 arrays - part 3 (tallying, text processing) Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 4 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis 1.
1 Building Java Programs Chapter 7: Arrays. 2 Chapter outline array basics declaring and initializing an array getting and setting values of elements.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
CSE 143 Lecture 1 Arrays (review) slides created by Marty Stepp
The Methods and What You Need to Know for the AP Exam
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
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
Defining New Types of Objects, part 3
An Introduction to Java – Part I, language basics
Object Oriented Programming in java
Building Java Programs
Value and reference semantics; Arrays as parameters
Suggested self-checks: Section 7.11 #1-11
Suggested self-checks: Section 7.11 #12-29
Introduction to java Part I By Shenglan Zhang.
Presentation transcript:

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 in the Pentium IV processor) Based on slides for Building Java Programs by Reges/Stepp, found at

CS305j Introduction to Computing Arrays Part 2 2 Concept of an array rotation  Imagine we want to 'rotate' the elements of an array; that is, to shift them left by one index. The element that used to be at index 0 will move to the last slot in the array. For example, {3, 8, 9, 7, 5} becomes {8, 9, 7, 5, 3}. Before: [0] [1] [2] [3] [4] list | > | 3 | 8 | 9 | 7 | 5 | After: [0] [1] [2] [3] [4] list | > | 8 | 9 | 7 | 5 | 3 |

CS305j Introduction to Computing Arrays Part 2 3 Shifting elements left  A left shift of the elements of an array: [0] [1] [2] [3] [4] list | > | 3 | 8 | 9 | 7 | 5 | / / / / V V V V list | > | 8 | 9 | 7 | 5 | 5 |  Let's write the code to do the left shift. –Can we generalize it so that it will work on an array of any size? –Can we write a right-shift as well?

CS305j Introduction to Computing Arrays Part 2 4 Shifting practice problem  Write a method insertInOrder that accepts a sorted array a of integers and an integer value n as parameters, and inserts n into a while maintaining sorted order. In other words, assume that the element values in a occur in sorted ascending order, and insert the new value n into the array at the appropriate index, shifting to make room if necessary. The last element in the array will be lost after the insertion. –Example: calling insertInOrder on array {1, 3, 7, 10, 12, 15, 22, 47, 74} and value = 11 produces {1, 3, 7, 10, 11, 12, 15, 22, 47}.

CS305j Introduction to Computing Arrays Part 2 5 String methods with arrays  These String methods return arrays: String s = "long book"; Method nameDescriptionExample 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"}

CS305j Introduction to Computing Arrays Part 2 6 String practice problems  Write a method named areAnagrams that accepts two Strings as its parameters and returns whether those two Strings contain the same letters (possibly in different orders). –areAnagrams("bear", "bare") returns true –areAnagrams("sale", "sail") returns false  Write a method that accepts an Array of Strings and counts the number of times a given letter is present in all the Strings

CS305j Introduction to Computing Arrays Part 2 7 Graphics methods with 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); xPoints and yPoints are "parallel" arrays parallel arrays: two or more separate arrays, usually of the same length, whose elements with equal indices are associated with each other in some way Method name drawPolygon(int[] xPoints, int[] yPoints, int length) drawPolyline(int[] xPoints, int[] yPoints, int length) fillPolygon(int[] xPoints, int[] yPoints, int length)

CS305j Introduction to Computing Arrays Part 2 8 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]; index01234 valuenull

CS305j Introduction to Computing Arrays Part 2 9 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

CS305j Introduction to Computing Arrays Part 2 10 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: C:\hw6> java ExampleProgram how are you? Or BlueJ call to main arg 0: how arg 1: are arg 2: you?

CS305j Introduction to Computing Arrays Part 2 11 Java's Arrays class  The Arrays class in package java.util has several useful static methods for manipulating arrays: Method nameDescription 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 toString(array)returns a String representing the array

CS305j Introduction to Computing Arrays Part 2 12 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  Sorting and searching: Arrays.sort(numbers);// now {-18, 13, 23, 75, 480} index = Arrays.binarySearch(numbers, -18); System.out.println("index = " + index); System.out.println(Arrays.toString(numbers)); –Output: index = 0 [-18, 13, 23, 75, 480]