Title: Introduction to Programming with Java, for Beginners
1Introduction to Programmingwith Java, for
Beginners
2What if our Frog (from lab) could say 10
different things?
- public class Frog
- private boolean formerPrince
- private String phrase1
- private String phrase2
- private String phrase3
- private String phrase4
- private String phrase5
- private String phrase6
- private String phrase7
- private String phrase8
- private String phrase9
- private String phrase10
- . . .
3What a Person could adopt lots of Pets ?
- public class Person
- private String name
- private Pet pet1
- private Pet pet2
- private Pet pet3
- private Pet pet4
- private Pet pet5
- private Pet pet5
- private Pet pet6
- private Pet pet6
- private Pet pet8
- private Pet pet9
- private Pet pet10
- private Pet pet11
- private Pet pet12
- private Pet pet13
- private Pet pet14
- private Pet pet15
- . . .
4What if we wanted to store 500 Itoons?
public class IToonList private IToon
toon1 private IToon toon2 private
IToon toon3 private IToon toon4
private IToon toon5 private IToon toon6
private Ttoon toon7 private IToon
toon8 private IToon toon9 private
Ttoon toon10 private IToon toon11
private IToon toon12 private IToon
toon13 . . . private IToon toon500
5What if we want to store lots of things
- But we dont want to declare a separate variable
for each one? - Thats what arrays are good for
6What is an Array?
Its an easy way to declare lots of variables
that all have the same type.
7Array Elements and Indices
- The data array has 5 elements. The value of
- data0 is 6
- data1 is 10
- data2 is 12
- data3 is 0
- data4 is 0
- The number within square brackets is called an
index - The valid indices are 0 thru (array length - 1)
- NOTE Whenever you see square brackets in a
Java program, it means youre dealing with an
array.
8An Array is an Object
9Using Array Elements in Expressions
- An element of an array of ints can be used
virtually anywhere an expression of type int is
valid. - Likewise for arrays of other types
- int data new int 6, 10, 12, 0, 0
- int x data0
- data3 data2
- data4 data3 data2 2
- System.out.println(data0 is data0)
- // note order of operations below
- data4 Math.pow(2, data4)
10Accessing an Arrays Length
- ArrayName.length gives size of the array
- int data
- data new int5 // data.length is 5
- data0 6
- data1 10
- data2 12
- int result 0
- for (int i 0 i lt data.length i)
- result result datai
-
11Complete the sum(..) method
public class ArrayTool / Takes an
array of ints as an argument. returns the
sum of all the integers in the array. /
public static int sum ( ?? )
Welcome to DrJava gt int data new int 6,
10, 12, 0, 0 gt ArrayTool.sum(data) 28
12Array Out of Bounds Exceptions
public class ArrayTool public static int
sum(int data) int sum 0 for (int i
0 i lt data.length i) sum sum
datai // sum datai return
sum public static int sum2(int
data) int sum 0 for (int i 0 i lt
data.length i) // error! sum sum
datai // sum datai return
sum
gt ArrayTool tool new ArrayTool() gt int data
new int 6, 10, 12, 0, 0 gt
tool.sum(data) 28 gt tool.sum2(data) ArrayIndexOutO
fBoundsException
13Examples of Arrays of Other Primitive Types
14Declaring Initializing Arrays of Primitives
int info1 2000, 100, 40, 60 int info2
new int 2000, 100, 40, 60 char
choices1 'p', 's', 'q' char choices2
new char 'p', 's', 'q' double temps1
75.6, 99.4, 86.7 double temps2 new
double 75.6, 99.4, 86.7
15Complete this method
public class ArrayTool / Returns true if
all integers in the data array are positive,
false otherwise. / public static boolean
allPositive(int data)
16One Solution
public class ArrayTool / Returns true if
all integers in the data array are positive,
false otherwise. / public static boolean
allPositive(int data) for (int i 0 i lt
data.length i) if (datai lt 0)
return false return true
17Another Solution
public class ArrayTool / Returns true if
all integers in the data array are positive,
false otherwise. / public static boolean
allPositive(int data) boolean result
true for (int i 0 i lt data.length i)
if (datai lt 0) result
false break //break is java
keyword return result
18break statement
- For example on the previous slide
- Terminates the for loop if the value found is
negative - Used only with loop structures
- Rule Terminates the innermost for, while
192D Arrays
- Array can have 2, 3, or more dimensions
- When declaring a variable for such an array, use
a pair of square brackets for each dimension - For 2D arrays, the elements are indexed
rowcolumn - Remember RC rowcolumn
- char board
- board new char33 // 3 rows, 3 columns
- board00 O // row 0, column 0
- board11 X // row 1, column 2
- board01 X // row 0, column 1
20Nested Loops
- A nested loop occurs when one loop is inside
another
21Nested Loops
- Doubly nested one loop inside another
- Triply nested a loop within a loop within a
loop - Java does not place a limit on nesting levels
- Deeply nested loops can be hard to maintain/debug
- Fix create a helper method to handle inner
loops and call that method in an outer loop
22Processing a 2D Array
- Use a doubly-nested for-loop to process a 2D
array - In this example we know the number of rows (3)
and columns (5) - In general, its better not to use magic
numbers (here the 3 and 5) in the loop. Its
better to write code that handles the general
case (see next slides)
char data new char35 for (int row
0 row lt 3 row) for (int col 0 col lt 5
col) System.out.print(datarowcol
\t) System.out.println()
23Processing a 2D Array (cont.)
- Most 2D arrays have a uniform number of rows and
colums - An n x m array (an n by m array) has n rows
and m columns
// prints an array with dimensions nWeeks x
nDays public static void print(double
temps,int nWeeks, int nDays) for (int row
0 row lt nWeeks row) for (int col 0
col lt nDays col) System.out.print(tem
psrowcol \t)
System.out.println()
24Processing a 2D Array (cont.)
- This illustrates a general purpose way to print
a 2D array - It works even for ragged arrays, whose row
lengths vary.
public static void printArray(int data)
if (data null data.length 0)
return for (int row 0 row lt data.length
row) for (int col 0 col lt
datarow.length col)
System.out.print(datarowcol \t)
System.out.println()
25Ragged Array
- gt int one 1,2,3
- gt int two 1,2,3,4,5,6
- gt int three 1,2
- gt
- gt int data one, two, three
- gt data0.length
- 3
- gt data1.length
- 6
- gt data2.length
- 2
- gt data0 three
- gt data1 two
- gt data2 one
- gt data0.length
- 2
- gt data1.length
- 6
- gt data2.length
- Row lengths vary.
- Motivation save space