Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSC – Java Programming II Lecture 9 January 30, 2002."— Presentation transcript:

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

2 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[][];

3 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];

4 Creating and Initializing a Multidimensional Array a = new int[5][10];

5 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.

6 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}};

7 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.

8 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.

9 How Multidimensional Arrays Are Stored In Java, a two-dimensional array is a one- dimensional array whose elements are one- dimensional arrays.

10 Using Two-Dimensional Arrays 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).

11 A sample gray-scale image

12 A 2-D Array of Gray Scale Values

13 Using Two-Dimensional Arrays 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.

14 14.1Files 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.

15 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.

16 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).

17 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.

18 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.

19 Streams Classes whose names end with Stream are subclasses of InputStream and OutputStream : System.out and System.err are instances of the PrintStream class.

20 Streams Other classes have names that end with Reader or Writer. These classes are subclasses of Reader and Writer :

21 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.

22 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.

23 Stream Layering A diagram showing the layering of the three classes:

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

25 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:

26 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");

27 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.

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

29 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.

30 Writing Data Types A partial list of DataOutputStream methods: Description Action void 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.

31


Download ppt "CSC – Java Programming II Lecture 9 January 30, 2002."

Similar presentations


Ads by Google