Chapter 13 Slides - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 13 Slides

Description:

Title: Slide 1 Author: John Schram Last modified by: h0000838 Created Date: 7/4/2003 3:08:29 AM Document presentation format: On-screen Show Company – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 15
Provided by: JohnSc166
Category:

less

Transcript and Presenter's Notes

Title: Chapter 13 Slides


1
Exposure Java
Chapter 13 Slides
Subscripted Variables
PowerPoint Presentation created by Mr. John L.
M. Schram From Materials created by Mr. Leon
Schram
2
Improved Array Definition
An array is a data structure with a fixed number
of elements of the same type. Every element of
the array can be accessed directly.
3
Array Example
16 Ingrid 17 Darlene 18 Gene 19 Sean 20 Stephanie
11 Holly 12 Blake 13 Michelle 14 Remy 15 Haley
06 Diana 07 Jessica 08 David 09 Anthony 10 Alec
01 Isolde 02 John 03 Greg 04 Maria 05 Heidi
4
Declaring a 1D Array
int list // declares the
array list identifier list new int10 //
allocates memory for 10 integers char names
// declares the names array identifier names
new char25 // allocates memory for 25
characters double grades // declares the
grades array identifier grades new
double50 // allocates memory for 50 doubles
5
// Java1301.java // This program demonstrates how
to declare an array of integers. // Note how each
element of the array defaults to zero. // Java
allows the display of uninitialized objects
because object // elements get assigned values
from the default constructor. public class
Java1301 public static void main(String
args) int list
// declares the array object identifier
list new int10 // allocates
memory for 10 array elements for
(int k 0 k lt 10 k)
System.out.println("list" k " "
listk) System.out.println()

Java1301.java Output List0 0 List1
0 List2 0 List3 0 List4 0 List5
0 List6 0 List7 0 List8 0 List9 0
6
Array Index Note
Java arrays indicate individual elements with an
index inside two brackets, following the array
identifier, like listk. The array index is
always an integer and starts at 0. In an array
of N elements, the largest index is N-1.
7
// Java1302.java // This program is almost
identical to Java1301. The difference // is
that the array declaration and array definition
to allocate // memory is done in one program
statement. public class Java1302
public static void main(String args)
int list new int10
// combined array declaration and definition
for (int k 0 k lt 10 k)
System.out.println("list" k
" " listk)
System.out.println()
Java1302.java Output List0 0 List1
0 List2 0 List3 0 List4 0 List5
0 List6 0 List7 0 List8 0 List9 0
8
// Java1303.java // This program demonstrates how
to initialize array elements. // The ltnewgt
operator is not necessary in this case. public
class Java1303 public static void
main(String args) int
list 11,22,33,44,55,66,77,88,99
for (int k 0 k lt 9 k)
System.out.println("list" k " "
listk) System.out.println()

Java1303.java Output List0 11 List1
22 List2 33 List3 44 List4 55 List5
66 List6 77 List7 88 List8 99
9
// Java1304.java // This program demonstrates how
to declare an array of characters. // Are the
array elements initialized to any kind of
value? public class Java1304 public
static void main(String args)
int k char list1 new
char5 for (k 0 k lt 5 k)
System.out.println("list1" k
" " list1k)
System.out.println() char list2
'c','h','a','r' for (k 0 k
lt 4 k) System.out.println(
"list2" k " " list2k)
System.out.println()
Java1304.java Output List10 List11
List12 List13 List14 List20
c List21 h List22 a List23 r
10
// Java1305.java // The purpose of this program
is to investigate the type of character that //
is used to initialize a character array. // Is
it no-character-at-all or a blank space? public
class Java1305 public static void
main(String args) char
List1 new char10
System.out.print("Start") for
(int K 0 K lt 10 K)
System.out.print(List1K)
System.out.println("End")
System.out.println()
Java1305.java Output Start
End
11
// Java1306.java // This program demonstrates how
String objects are initialized, // both without
and with specified array values. public class
Java1306 public static void main(String
args) String list1
new String5 for (int k 0
k lt 5 k)
System.out.println("list" k " "
list1k) System.out.println()
String list2 "AAA","BBB","CCC","DD
D","EEE" for (int k 0 k lt
5 k) System.out.println("l
ist2" k " " list2k)
System.out.println()
Java1306.java Output List0 null List1
null List2 null List3 null List4
null List20 AAA List21 BBB List22
CCC List23 DDD List24 EEE
12
// Java1307.java // This program fills an integer
array with a random set of numbers. import
java.util.Random public class Java1307
public static void main(String args)
int list new int20
Random random new Random(12345)
for (int k 0 k lt 20 k)
listk random.nextInt(900) 100
for (int k 0 k lt 20 k)
System.out.println("list" k " "
listk) System.out.println()

Java1307.java Output List0 851 List1
680 List2 241 List3 928 List4
458 List5 284 List6 575 List7
802 List8 701 List9 889 List10
717 List11 142 List12 890 List13
206 List14 312 List15 584 List16
687 List17 803 List18 432 List19 775
13
// Java1308.java // This program introduces the
length field to determine the // number of
elements in the array. Remove the comments from
line 16 // to observe what happens when the
length field is altered. public class Java1308
public static void main(String args)
String names
"Joe","Tom","Sue","Meg" int n
names.length // data field access
not a method call
System.out.println("There are " n " array
elements.") for(int k 0 k lt n
k) System.out.println("name
s" k " " namesk) //
names.length 10
System.out.println()
Java1308.java Output There are 4 array
elements. Names0 Joe Names1 Tom Names2
Sue Names3 Meg
14
// Java1309.java // This program demonstrates how
to create an array of random strings. import
java.util.Random public class Java1309
public static void main(String args)
Random random new Random(12345)
int rndInt String
names "AA","BB","CC","DD","EE","FF","GG","HH"
,"II","JJ" for(int k 1 k lt 15
k) rndInt
random.nextInt(names.length)
System.out.println("names" rndInt " "
namesrndInt)
System.out.println()
Java1309.java Output Names9 JJ Names7
HH Names9 JJ Names8 II Names8
II Names1 BB Names6 GG Names0
AA Names1 BB Names0 AA Names7
HH Names8 II Names0 AA Names3 DD
Write a Comment
User Comments (0)
About PowerShow.com