Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.



Similar documents
In this Chapter you ll learn:

AP Computer Science Java Mr. Clausen Program 9A, 9B

Arrays in Java. Working with Arrays

CMSC 202H. ArrayList, Multidimensional Arrays

Introduction to Data Structures

Introduction to Java

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Two-Dimensional Arrays Summer 2010 Margaret Reid-Miller

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

A Brief Introduction to MySQL

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

CSE 2123 Collections: Sets and Iterators (Hash functions and Trees) Jeremy Morris

Basic Programming and PC Skills: Basic Programming and PC Skills:

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Programming with Data Structures

Contents. 9-1 Copyright (c) N. Afshartous

AP Computer Science Java Subset

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Object Oriented Software Design

WRITING DATA TO A BINARY FILE

5 Arrays and Pointers

Part I. Multiple Choice Questions (2 points each):

Computer Science III Advanced Placement G/T [AP Computer Science A] Syllabus

Example of a Java program

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

Java Interfaces. Recall: A List Interface. Another Java Interface Example. Interface Notes. Why an interface construct? Interfaces & Java Types

arrays C Programming Language - Arrays

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Answers to Review Questions Chapter 7

LINKED DATA STRUCTURES

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Building Java Programs

Object-Oriented Programming in Java

Java Crash Course Part I

The C Programming Language course syllabus associate level

VB.NET Programming Fundamentals

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

Building Java Programs

Efficient Data Structures for Decision Diagrams

Computer Programming I

Java Program Coding Standards Programming for Information Technology

CS 106 Introduction to Computer Science I

Building Java Programs

Chapter 1 Java Program Design and Development

Object Oriented Software Design

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Practical Session 4 Java Collections

Chapter 2 Introduction to Java programming

Moving from CS 61A Scheme to CS 61B Java

Java from a C perspective. Plan

Introduction to Object-Oriented Programming

CS 111 Classes I 1. Software Organization View to this point:

C# and Other Languages

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright /6/11 12:33 PM!

Basics of Java Programming Input and the Scanner class

#820 Computer Programming 1A

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Advanced Java Client API

JAVA ARRAY EXAMPLE PDF

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Creating a Simple, Multithreaded Chat System with Java

Two-Dimensional Arrays. Multi-dimensional Arrays. Two-Dimensional Array Indexing

Java Interview Questions and Answers

Introduction to Programming System Design. CSCI 455x (4 Units)

13 File Output and Input

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Introduction to Java. CS 3: Computer Programming in Java

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Sample CSE8A midterm Multiple Choice (circle one)

Java CPD (I) Frans Coenen Department of Computer Science

public static void main(string[] args) { System.out.println("hello, world"); } }

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Topic 11 Scanner object, conditional execution

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Java Programming Fundamentals

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Continuous Integration Part 2

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

J a v a Quiz (Unit 3, Test 0 Practice)

INTRODUCTION The collection of data that makes up a computerized database must be stored physically on some computer storage medium.

Computational Mathematics with Python

Introduction. Why (GIS) Programming? Streamline routine/repetitive procedures Implement new algorithms Customize user applications

One Dimension Array: Declaring a fixed-array, if array-name is the name of an array

Getting Started with the Internet Communications Engine

Lecture 5: Java Fundamentals III

Classes: Relationships Among Objects. Atul Prakash Background readings: Chapters 8-11 (Downey)

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Transcription:

Arrays Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html 1

Grid in Assignment 2 How do you represent the state of the game so that you can print it out after each step? Need a way to reprsent 1-dimensional or 2-dimensional grids Solution: arrays 2

Arrays Arrays are simply a fixed-length collection of objects, indexed by a number. To create an array of 10 integers, one can do: int[] x; // declares x to be an array type. x = new int[10]; // creates an int array of size 10. x[0], x[1],, x[8], x[9]: the ten elements 3

int[] nums; nums = new int[10]; nums[0] = 6; nums[1] = 10; Array usage nums nums[2] = nums[0] + nums[1]; nums[3] = nums[1]; 4

Example int nums[]; nums = new int[10]; for (int i = 0; i < 10; i++) { // Initialize the array with squares nums[i] = i*i; } for (int i = 0; i < 10; i++) { // print out the array System.out.printf("index = %d, array content = %d\n", i, nums[i]); } index = 0, cell content = 0 index = 1, cell content = 1 index = 2, cell content = 4 index = 3, cell content = 9 index = 4, cell content = 16 index = 5, cell content = 25 index = 6, cell content = 36 index = 7, cell content = 49 index = 8, cell content = 64 index = 9, cell content = 81 printf: Formatted output %d: substituted by the corresponding integer argument 5

Two-dimensional arrays We want to create a 10x20 array of 1's. Java (like C) only can create onedimensional arrays. Solution: Create an array of arrays. 6

// create an array or arrays int[][] nums; // create 10 arrays. nums[0]...nums[9] will // be of type int[]. nums = new int[10][]; // make each element a 20-element array for (int i = 0; i < 10; i++) { nums[i] = new int[20]; } // fill the array with ones. for (int i = 0; i < 10; i++) { for (int j = 0; j < 20; j++) { nums[i][j] = 1; } } 7

Visualization of Arrays Courtesy: jgrasp Editor 8

Visualization Courtesy: jgrasp Editor 9

Assigning Arrays - Aliasing occurs // Assigning array references. int[] a; int [] b; a = new int[8]; b = a; // reference copy. a and b refer to the same array a[4] = 3; System.out.println(b[4]); // Will print 3. a b 0 0 0 0 1 0 0 0 10

Bound Checking Java does safety checks on arrays bounds. Exception occurs if array index is negative or >= size of the array Different from C++, where array bounds are not checked one can write past an array, trashing a program Different from Python lists: negative indices not allowed 11

Arrays versus Lists Arrays look like lists of values and can sometimes be used interchangeably, but some differences Arrays (as in Java) fixed-length Fast for random access. Any element can be accessed fast Not designed for fast insert/ deletes in the middle of the array. Would require shifting elements to permit indexing Lists (e.g., Python lists) variable-length Fast for sequential access. Random access could be slow Designed for fast inserts/deletes, typically Java has lists as well: ArrayList, LinkedList, Vector. More on that later. 12

Stop here The following slides will make more sense after we discuss objects 13

Object References Dog d; d is a reference to a dog. It is not the actual dog. d = null; null is a special object to help initialize references. Like 0. d = new Dog("Fido", "woof"); d now refers to a dog object d 14

Dog d1, d2; Assigning Object Creates two object references d1 = new Dog("Fido", "woof"); d1 References d2 = d1 Only copies the reference, not the object d1 d2 15

Aliasing of references d1 d2 d1.setbark("huff"); d1 d2 name: "Fido" bark: "woof" name: "Fido" bark: "huff" What are d1.getbark() and d2.getbark()? 16

In Java... Most of the variables are references to objects. Array variables are always references to array objects. Exceptions: primitive types, such as int, boolean, byte, short, long, float, double 17

Example: Primitive Types vs. object types public static void main(string[] args) { int i, j; // Not references. Basic types. i = 0, j = 0 i = 2; // i = 2, j = 0 j = i; // i = 2, j = 2. i = 3; // i = 3, j = 2 System.out.println(j); // will print 2, not 3. Dog d1, d2; // References. d1 = new Dog("Fido", "woof"); d2 = d1; d1.setbark("huff"); d2.bark(); // will print "huff", not "woof" d1 d2 name: "Fido" bark: "huff" } Java convention: Types starting with small cap (e.g., int) are primitive. Others should start with a capital letter (e.g., String, Dog) and are object types. 18

Arrays of Objects // Arrays of objects Dog[] dogarray;// Create a reference to an array dogarray = new Dog[3]; // Create 3 references to dogs // Create the dogs dogarray[0] = new Dog("Fido", "woof"); dogarray[1] = new Dog("Daisy", "huff"); dogarray[2] = new Dog("Ginger", "woof"); 19

20