lecture05-june15 - Lecture 5 Announcements Arun's office...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Lecture 5 Announcements • Arun’s office hours posted under Announcements on BB • Assignment 1 – Q&A due today – Designs due Wed. – Programs due Sunday before midnight • Read Chapter 1 for next week • Topics for today – Arrays 1D and 2D+ – Dynamic memory allocation Formatted String Output (A.5) • System.out.println (String.format (“format-string”, list-of-values)); • Works exactly like printf ( ) in C to build the string that is output to screen, e.g. System.out.println (String.format (“%10d %10.2f”,n, Math.sqrt(n))); • You can even create formatter objects to be used with the Formatter class (in java.util) Formatter fOut = new Formatter (System.out); fOut.format (“format-string”, list-of-values); Importing Library Classes Related classes are packaged together in the library A package is a named collection of related class definitions java.lang is automatically imported into your program – Classes in here include: String, Math, Object, etc. Syntax for importing classes import packageName . className ; Examples – import java.util.Random; – import java.awt.Rectangle; – import java.io. * // all of the classes in the java.io package Some JAVA Class Packages Package Name Description java.lang Contains core JAVA classes, such as numeric classes, strings, etc. Is implicitly imported into every program java.awt Classes for basic graphics java.applet Classes for supporting applets java.io Classes for input and output of streams and files java.util Many utility classes,such as date java.net Classes for supporting network communications java.awt.image Classes for managing bitmap images java.awt.peer Platform specific GUI implementation classes others e.g. java.sql, java.rml Arrays in Java Java has built-in arrays as well as more complicated classes to automate many array tasks (the Vector and ArrayList classes; later) arrays hold elements of the same type – primitive data types or class objects – space for array must be dynamically allocated with new operator. – Size is any integer expression - it does not have to be constant. – The length attribute of an array refers to the number of elements allocated to the array public static void arrayExamples(int n) { int[ ] intList = new int[n + 1]; for(int i = 0; i < intList.length; i++) { intList[i] = i * i * i; } intList[3] = intList[4] * intList[3]; } Array Details • Array variables are object variables • They hold the reference (memory address) of an array object • arrays have a public, final field called length • elements start with an index of 0, last index is length - 1 • trying to access a non existent element results in an ArrayIndexOutOfBoundsException (AIOBE)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Array Initialization • All values in the array are initialized to the default values for that type (0, 0.0, char 0, false, or null) • Arrays may also be initialized with an initializer list: • E.g. int[ ] intList = {2, 3, 5, 7, 11, 13};
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

lecture05-june15 - Lecture 5 Announcements Arun's office...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online