csc – java programming ii lecture 9 january 30, 2002

31
CSC – Java Programming II Lecture 9 January 30, 2002

Upload: jewel-mccarthy

Post on 25-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC – Java Programming II Lecture 9 January 30, 2002

CSC – Java Programming II

Lecture 9January 30, 2002

Page 2: CSC – Java Programming II Lecture 9 January 30, 2002

Creating and Initializing a Multidimensional Array

• The declaration of a multidimensional array will contain more than one set of square brackets:int[][] a;

• It’s legal to put some or all of the brackets after the array name:int[] a[];int a[][];

Page 3: CSC – Java Programming II Lecture 9 January 30, 2002

Creating and Initializing a Multidimensional Array

• The new keyword is used to allocate space for a multidimensional array:a = new int[5][10];

a will have 5 rows and 10 columns.• The declaration of a multidimensional array

can be combined with the allocation of space for the array:int[][] a = new int[5][10];

Page 4: CSC – Java Programming II Lecture 9 January 30, 2002

Creating and Initializing a Multidimensional Array

a = new int[5][10];

Page 5: CSC – Java Programming II Lecture 9 January 30, 2002

Creating and Initializing a Multidimensional Array

• Rules for assigning default values to array elements:– Numbers are set to zero.– boolean elements are set to false.

– Elements of a reference type are set to null.

Page 6: CSC – Java Programming II Lecture 9 January 30, 2002

Creating and Initializing a Multidimensional Array

• The initializer for a two-dimensional array looks like a series of one-dimensional array initializers:int[][] square = {{8, 3, 4}, {1, 5, 9}, {6, 7, 2}};

Page 7: CSC – Java Programming II Lecture 9 January 30, 2002

Processing the Elements in a Multidimensional Array

• Statements that sum the elements in an array a by visiting the elements row by row:int sum = 0;for (int row = 0; row < 3; row++) for (int col = 0; col < 3; col++) sum += a[row][col];

• Because the loops are nested, the row and col variables will take on all possible combinations of values.

Page 8: CSC – Java Programming II Lecture 9 January 30, 2002

Processing the Elements in a Multidimensional Array

• Order in which the elements of a will be visited:a[0][0]a[0][1]a[0][2]a[1][0]a[1][1]a[1][2]a[2][0]a[2][1]a[2][2]

• Nested for loops are ideal for processing multidimensional arrays.

Page 9: CSC – Java Programming II Lecture 9 January 30, 2002

How Multidimensional Arrays Are Stored

• In Java, a two-dimensional array is a one-dimensional array whose elements are one-dimensional arrays.

Page 10: CSC – Java Programming II Lecture 9 January 30, 2002

Using Two-DimensionalArrays to Store Images

• Two-dimensional arrays are often used to store images.

• Images are sometimes stored in gray scale, rather than in color.

• In a gray-scale image, each pixel is a shade of gray.

• Typically there are 256 shades, represented by integers between 0 (black) and 255 (white).

Page 11: CSC – Java Programming II Lecture 9 January 30, 2002

A sample gray-scale image

Page 12: CSC – Java Programming II Lecture 9 January 30, 2002

A 2-D Array of Gray Scale Values

Page 13: CSC – Java Programming II Lecture 9 January 30, 2002

Using Two-DimensionalArrays to Store Images

• Arrays often occupy large amounts of memory, especially when they have more than one dimension.

• If numRows and numColumns are both 1000, the grayscaleImage array will require 4,000,000 bytes of memory (1000 1000 4).

• Arrays this large may exceed the limits of a Java interpreter.

Page 14: CSC – Java Programming II Lecture 9 January 30, 2002

14.1 Files and Streams

• A file is a collection of related data that is given a name and placed on a storage medium.

• A file can be used to persist data over a long period of time and between executions.

• All files are the same in one important respect: they consist of bytes.

Page 15: CSC – Java Programming II Lecture 9 January 30, 2002

How Files Are Stored

• In some files, each byte represents a character. In other files, the bytes mean other things.

• Suppose that a file consists of four bytes:

• Some possible meanings:– The bytes represent the ASCII characters J, a, v, and a.– The bytes represent a single integer.– The first two bytes represent a short integer, and the second

two represent another short integer.– The bytes represent a single float value.

Page 16: CSC – Java Programming II Lecture 9 January 30, 2002

Text Files Versus Binary Files

• Files fall into two categories:– In a text file, the bytes represent characters in

some character set, such as ASCII or Unicode.– In a binary file, the bytes don’t necessarily

represent characters (although some of them may).

Page 17: CSC – Java Programming II Lecture 9 January 30, 2002

Text Files Versus Binary Files

• Text files have two characteristics that binary files don’t possess.– First, text files are divided into lines.– Second, text files may contain a special “end-

of-file” marker.

• In a binary file, there’s no “end-of-line” or “end-of-file” marker; all bytes are treated equally.

Page 18: CSC – Java Programming II Lecture 9 January 30, 2002

Streams

• Programs that work with files will need to use the java.io package.

• The names of many java.io classes include the word “stream.”

• A stream is an abstraction that represents any “file-like” source of input or destination for output.

• A stream object may be capable of reading from anything that resembles a file or writing to anything that resembles a file.

Page 19: CSC – Java Programming II Lecture 9 January 30, 2002

Streams

• Classes whose names end with Stream are subclasses of InputStream and OutputStream:

• System.out and System.err are instances of the PrintStream class.

Page 20: CSC – Java Programming II Lecture 9 January 30, 2002

Streams

• Other classes have names that end with Reader or Writer.

• These classes are subclasses of Reader and Writer:

Page 21: CSC – Java Programming II Lecture 9 January 30, 2002

Streams

• The reader and writer classes are designed to help with a common problem: Java stores characters in Unicode, whereas most software assumes that characters are stored in ASCII form.

• An instance of a Reader class solves the problem by automatically converting bytes to Unicode characters during input.

• Similarly, an instance of a Writer class will convert Unicode characters to single bytes during output.

Page 22: CSC – Java Programming II Lecture 9 January 30, 2002

Streams

• The reader and writer classes are designed to help with a common problem: Java stores characters in Unicode, whereas most software assumes that characters are stored in ASCII form.

• An instance of a Reader class solves the problem by automatically converting bytes to Unicode characters during input.

• Similarly, an instance of a Writer class will convert Unicode characters to single bytes during output.

Page 23: CSC – Java Programming II Lecture 9 January 30, 2002

Stream Layering

• A diagram showing the layering of the three classes:

Page 24: CSC – Java Programming II Lecture 9 January 30, 2002

Working with Files

• Working with a file involves three steps:– Open the file– Perform operations on the file– Close the file

Page 25: CSC – Java Programming II Lecture 9 January 30, 2002

File Pointers

• For each open file, Java maintains a file pointer, which keeps track of which byte will be the next to be read or written.

• If a file is opened for reading, the file pointer will be at the beginning:

Page 26: CSC – Java Programming II Lecture 9 January 30, 2002

Creating File Objects

• There are several ways to create a File object.• One File constructor expects a string containing

the file’s name:File f = new File("Lottery.java");

Java will try to locate the file in the current directory.

• To work with a file in a different directory, path information will need to be included in the string:File f = new File("c:\\programs\\lottery\\Lottery.java");

Page 27: CSC – Java Programming II Lecture 9 January 30, 2002

Using Multiple catch Blocks

• More than one catch block may be capable of handling a given exception:try { …} catch (FileNotFoundException e) { …} catch (IOException e) { …}

• If a FileNotFoundException occurs, both catch blocks can potentially handle the exception, but only the first will be allowed to.

Page 28: CSC – Java Programming II Lecture 9 January 30, 2002

finally Blocks

• After the last in a series of catch blocks, a finally block may be present:try blockcatch (exception-type identifier) block…catch (exception-type identifier) blockfinally block

Page 29: CSC – Java Programming II Lecture 9 January 30, 2002

Writing Data Types

• A DataOutputStream object can’t be created directly from a file name.

• Instead, a FileOutputStream object is first created, and then that object is used to create a DataOutputStream object:FileOutputStream fileOut = new FileOutputStream(fileName);DataOutputStream out = new DataOutputStream(fileOut);

• The DataOutputStream constructor will accept any output stream object as its argument.

Page 30: CSC – Java Programming II Lecture 9 January 30, 2002

Writing Data Types• A partial list of DataOutputStream methods: Description Actionvoid write(int b) Writes the value of b as a single byte.void write(byte[] b, Writes len bytes of the array b, start- int off, int len) ing at the position indicated by off.void writeBoolean( Writes the value of v as a byte contain- boolean v) ing either 1 (true) or 0 (false).void writeByte(int v) Writes the value of v as a single byte.void writeChar(int v) Writes the value of v as 2 bytes.void writeDouble(double v) Writes the value of v as 8 bytes.void writeFloat(float v) Writes the value of v as 4 bytes.

void writeInt(int v) Writes the value of v as 4 bytes.void writeLong(long v) Writes the value of v as 8 bytes.void writeShort(int v) Writes the value of v as 2 bytes.

Page 31: CSC – Java Programming II Lecture 9 January 30, 2002