Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS-2852 Data Structures LECTURE 1B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

Similar presentations


Presentation on theme: "CS-2852 Data Structures LECTURE 1B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com."— Presentation transcript:

1 CS-2852 Data Structures LECTURE 1B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

2 CS-2852 Data Structures, Andrew J. Wozniewicz Agenda Arrays – One-Dimensional – Two-Dimensional – Multi-Dimensional Stream I/O – Text-file I/O – Binary-file I/O

3 CS-2852 Data Structures, Andrew J. Wozniewicz Array Definition Fixed number of values of a single type arranged consecutively and accessed by index.

4 CS-2852 Data Structures, Andrew J. Wozniewicz Array Declarations int[] anArrayOfInts; byte[] anArrayOfBytes; short[] anArrayOfShorts; long[] anArrayOfLongs; float[] anArrayOfFloats; double[] anArrayOfDoubles; boolean[] anArrayOfBooleans; char[] anArrayOfChars; String[] anArrayOfStrings;

5 CS-2852 Data Structures, Andrew J. Wozniewicz One-Dimensional Array

6 CS-2852 Data Structures, Andrew J. Wozniewicz Multi-Dimensional Arrays A multidimensional array is simply an array whose components are themselves arrays. The rows are allowed to vary in length. Arrays of Arrays of…

7 CS-2852 Data Structures, Andrew J. Wozniewicz Multi-Dim Array Declaration int[][] a2; int[][] a2 = new int[10][5]; String[][] Data; byte[][] screenPix; byte[][] raggedArray = new byte[5][];

8 CS-2852 Data Structures, Andrew J. Wozniewicz Multi-Dim Array Example class MultiDimArrayDemo { public static void main(String[] args) { String[][] names = {{"Mr. ", "Mrs. ", "Ms. "}, {"Smith", "Jones"}}; System.out.println(names[0][0] + names[1][0]); //Mr. Smith System.out.println(names[0][2] + names[1][1]); //Ms. Jones }

9 CS-2852 Data Structures, Andrew J. Wozniewicz Standard I/O Streams System.in System.out System.err Opened automatically and available without any further action.

10 CS-2852 Data Structures, Andrew J. Wozniewicz Standard I/O Streams Byte streams instead of character streams: – System.in PrintStream (is-a java.io.OutputStream, is-a java.io.FilterOutputStream): – System.out – System.err

11 CS-2852 Data Structures, Andrew J. Wozniewicz Using System.in import java.io.*; public class StreamIOTest1 { public static void main(String args[]) throws IOException { System.out.println("Hi!"); System.out.print("Enter your name: "); InputStreamReader cin = new InputStreamReader(System.in); BufferedReader input = new BufferedReader(cin); String name = input.readLine(); System.out.print("Hello "); System.out.println(name); }

12 CS-2852 Data Structures, Andrew J. Wozniewicz Using Console import java.io.*; public class ConsoleIOTest1 { public static void main(String[] args) { Console c = System.console(); if (c == null) { System.err.println("No console."); System.exit(1); } String login = c.readLine("Enter your user name: "); c.printf("Welcome, %1$s.\n", login); }

13 CS-2852 Data Structures, Andrew J. Wozniewicz Reading Text Files import java.io.*; public class TextFileReadDemo { public static void main(String[] args) throws IOException { String inFileName = args[0]; BufferedReader ins = new BufferedReader( new FileReader(inFileName) ); do { String line = ins.readLine(); if (line == null) break; System.out.println(line); } while (true); }

14 CS-2852 Data Structures, Andrew J. Wozniewicz Writing Text Files import java.io.*; public class TextFileWriteDemo { public static void main(String[] args) throws IOException { String outFileName = args[0]; String[] names = new String[] {"Andrew","Dorota","Martin", "Anna","Mimi","Kiki"}; PrintWriter outs = new PrintWriter(new FileWriter(outFileName)); for (int i = 0; i < names.length; i++) { outs.println(names[i]); } outs.close(); }

15 CS-2852 Data Structures, Andrew J. Wozniewicz Writing Binary Files – Part I import java.io.*; public class BinaryDataWriterOnlyTest1 { static final String dataFile = "invoicedata.dat"; static final double[] prices = { 2.99, 4.79, 5.99, 3.99, 5.99 }; static final int[] units = { 12, 8, 13, 10, 12 }; static final String[] descriptions = { "Milk", "Cereal", "Potato Chips", "Salsa", "Cheddar Cheese" };... }

16 CS-2852 Data Structures, Andrew J. Wozniewicz Writing Binary Files – Part II public static void main(String[] args) throws IOException { DataOutputStream out = null; try { out = new DataOutputStream( new BufferedOutputStream( new FileOutputStream(dataFile))); for (int i = 0; i < prices.length; i ++) { out.writeDouble(prices[i]); out.writeInt(units[i]); out.writeUTF(descriptions[i]); } } finally { out.close(); }

17 CS-2852 Data Structures, Andrew J. Wozniewicz Reading Binary Files – Part I import java.io.*; public class BinaryDataReadOnlyTest1 { static final String dataFile = "invoicedata.dat"; public static void main(String[] args) throws IOException { DataInputStream in = null; }

18 CS-2852 Data Structures, Andrew J. Wozniewicz Reading Binary Files – Part II double total = 0.0; try { in = new DataInputStream( new BufferedInputStream( new FileInputStream(dataFile))); double price; int unit; String desc; finally { in.close(); }

19 CS-2852 Data Structures, Andrew J. Wozniewicz Reading Binary Files – Part II try { while (true) { price = in.readDouble(); unit = in.readInt(); desc = in.readUTF(); System.out.format( "You ordered %d units of %s at $%.2f%n", unit, desc, price); total += unit * price; } } catch (EOFException e) { } System.out.format("For a TOTAL of: $%.2f%n", total);

20 CS-2852 Data Structures, Andrew J. Wozniewicz Summary Arrays – fixed number of values of a single type – one-, or multi-dimensional Stream I/O – Simple console I/O: System.in, System.out – Text-file I/O: BufferedReader, FileReader PrintWriter, FileWriter – Binary-file I/O DataInputStream, BufferedInputStream, FileInputStream DataOutputStream, BufferedOutputStream, FileOutputStream

21 Questions? Image copyright © 2010 andyjphoto.com


Download ppt "CS-2852 Data Structures LECTURE 1B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com."

Similar presentations


Ads by Google