Transcript Document

First Data Structure Definition
A data structure is a data type whose
components are smaller data structures
and/or simple data types.
Data Structure Starting Point
Any data type that can store more than
one value is a data structure.
First Array Definition
An array is a data structure with one, or
more, elements of the same type.
A one-dimensional
array is frequently
also called a vector.
A two-dimensional
array is frequently
also called a matrix.
First Record Definition
A record is a data structure with one,
or more, elements, called fields, of the
same or different data types.
File Definition
A file is an internal data structure - with an
unspecified number of elements of the same
type - assigned to an external file name.
The file data structure
allows transfer of data
between internal and
external storage.
Stack Definition
A stack is a data structure
with elements of the same
type.
Data elements of the stack
data structure can only be
accessed (stored or
retrieved) at one end of the
stack in a LIFO (Last In,
First Out) manner.
Improved Data Structure Definition
A data structure is a data type whose
components are smaller data structures
and/or simple data types.
The storing and retrieval of the data elements
is performed by accessing methods that
characterize the data structure.
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.
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
Defining Static Arrays
int list[ ];
list = new int[10];
// declares the array list identifier
// allocates memory for 10 integers
char names[ ];
names = new char[25];
// declares the names array identifier
// allocates memory for 25 characters
double grades[ ];
// declares the grades array identifier
grades = new double[50]; // allocates memory for 50 doubles
Defining Static Arrays
Preferred Method
int list[ ] = new int[10];
char names[ ] = new char[25];
double grades[ ] = new double[50];
// Java1201.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 Java1201
{
public static void main(String args[])
{
int list[];
// declares the array object identifier
list = new int[10]; // allocates memory for 10 array elements
for (int k = 0; k < 10; k++)
System.out.println("list[" + k + "] = " + list[k]);
System.out.println();
0
1
2
3
4
5
6
7
8
9
}
0 0 0 0 0 0 0 0 0 0
}
Array Index Note
Java arrays indicate individual elements with
an index inside two brackets, following the
array identifier, like list[k]
The array index is always an integer and
starts at 0.
In an array of N elements, the largest index is
N-1.
// Java1202.java
// This program is almost identical to Java1201. The difference
// is that the array declaration and array definition to allocate
// memory is done in one program statement.
public class Java1202
{
public static void main(String args[])
{
int list[] = new int[10];
// combined array declaration and definition
for (int k = 0; k < 10; k++)
System.out.println("list[" + k + "] = " + list[k]);
System.out.println();
}
}
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
0
0
// Java1203.java
// This program demonstrates how to initialize array elements.
// The <new> operator is not necessary in this case.
public class Java1203
{
public static void main(String args[])
{
int list[] = {11,22,33,44,55,66,77,88,99};
for (int k = 0; k < 9; k++)
System.out.println("list[" + k + "] = " + list[k]);
System.out.println();
}
}
0
1
2
3
4
5
6
7
8
11 22 33 44 55 66 77 88 99
// Java1204.java
// This program demonstrates how to declare an array of characters.
// Are the array elements initialized to any kind of value?
public class Java1204
{
public static void main(String args[])
{
int k;
char list1[] = new char[5];
for (k = 0; k < 5; k++)
System.out.println("list1[" + k + "] = " + list1[k]);
System.out.println();
char list2[] = {'c','h','a','r'};
for (k = 0; k < 4; k++)
System.out.println("list2[" + k + "] = " + list2[k]);
System.out.println();
0 1 2 3 4
0 1 2 3
list1
list2
}
? ? ? ? ?
c h a r
}
// Java1205.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 Java1205
{
public static void main(String args[])
{
char List1[] = new char[10];
System.out.print("Start");
for (int K = 0; K < 10; K++)
System.out.print(List1[K]);
System.out.println("End");
System.out.println();
} 1
0
2
3
4
5
6
7
8
9
}<sp> <sp> <sp> <sp> <sp> <sp> <sp> <sp> <sp> <sp>
// Java1206.java
// This program demonstrates how String objects are initialized,
// both without and with specified array values.
public class Java1206
{
public static void main(String args[])
{
String list1[] = new String[5];
for (int k = 0; k < 5; k++)
System.out.println("list[" + k + "] = " + list1[k]);
System.out.println();
String list2[] = {"AAA","BBB","CCC","DDD","EEE"};
for (int k = 0; k < 5; k++)
System.out.println("list2[" + k + "] = " + list2[k]);
0
1
2
3
4
0 System.out.println();
1
2
3
4
list1
} null null null null null list2 AAA BBB CCC DDD EEE
}
// Java1207.java
// This program fills an integer array with a random set of numbers.
import java.util.Random;
public class Java1207
{
public static void main(String args[])
{
int list[] = new int[20];
Random random = new Random(12345);
for (int k = 0; k < 20; k++)
list[k] = random.nextInt(900) + 100;
for (int k = 0; k < 20; k++)
System.out.println("list[" + k + "] = " + list[k]);
System.out.println();
0
1}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
} 680 241 928 455 284 575 802 701 889 717 142 890 206 312 584 687 803
851
18
19
432 775
// Java1208.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 Java1208
{
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 < n; k++)
System.out.println("names[" + k + "] = " + names[k]);
// names.length = 10; // Line 16
System.out.println();
}
}
0
1
2
3
Joe
Tom
Sue
Meg
// Java1209.java
// This program demonstrates how to create an array of random strings.
import java.util.Random;
public class Java1209
{
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 < 15; k++)
{
rndInt = random.nextInt(names.length);
System.out.println("names[" + rndInt + "] = " + names[rndInt]);
}
System.out.println();
0
}AA
}
1
2
3
4
5
6
7
8
9
BB
CC
DD
EE
FF
GG
HH
II
JJ
// Java1210.java
// This program is the first stage in the List class. At this stage the
// List class only initializes an array and displays its default values.
public class Java1210
{
public static void main(String args[])
{
List1 listA = new List1(10);
listA.display();
List1 listB = new List1(15);
listB.display();
}
}
class List1
{
private int intArray[]; // stores array elements
private int size;
// number of elements in the array
public List1(int s)
{
size = s;
intArray = new int[size];
}
public void display()
{
for (int k = 0; k < size; k++)
System.out.print(intArray[k] + " ");
System.out.println();
}
}
// Java1211.java
// This is the second stage of the List class.
// A method for assigning random integers has been added.
import java.util.Random;
public class Java1211
{
public static void main(String args[])
{
List2 listA = new List2(10);
listA.Display();
listA.Assign();
listA.Display();
System.out.println();
}
}
class List2
// shown on next slide
class List2
{
private int intArray[]; // stores array elements
private int size;
// number of elements in the array
public List2(int s)
{
System.out.println("\nCONSTRUCTING NEW LIST OBJECT WITH DEFAULT VALUES");
size = s;
intArray = new int[size];
}
public void Assign()
{
System.out.println("\nASSIGNING RANDOM VALUES TO LIST OBJECT");
Random random = new Random(12345);
for (int k = 0; k < size; k++)
intArray[k] = random.nextInt(9000) + 1000;
}
public void Display()
{
System.out.println("\nDISPLAYING ARRAY ELEMENTS");
for (int k = 0; k < size; k++)
System.out.print(intArray[k] + " ");
System.out.println();
}
}
// Java1212.java
// A second "overloaded" constructor has been added to the third stage of the
// List class. The second constructor specifies the initial array values.
import java.util.Random;
public class Java1212
{
public static void main(String args[])
{
List3 listA = new List3(10);
listA.Display();
List3 listB = new List3(10,999);
listB.Display();
listB.Assign();
listB.Display();
System.out.println();
}
}
class List3
// continued on next slide
class List3
{
private int intArray[];
private int size;
public List3(int s)
{
// stores array elements
// number of elements in the array
System.out.println("\nCONSTRUCTING NEW LIST OBJECT WITH DEFAULT VALUES");
size = s;
intArray = new int[size];
}
public List3(int s, int n)
{
System.out.println("\nCONSTRUCTING NEW LIST OBJECT WITH SPECIFIED VALUES");
size = s;
intArray = new int[size];
for (int k = 0; k < size; k++)
intArray[k] = n;
}
public void Assign()
{
System.out.println("\nASSIGNING RANDOM VALUES TO LIST OBJECT");
Random random = new Random(12345);
for (int k = 0; k < size; k++)
intArray[k] = random.nextInt(9000) + 1000;
}
public void Display()
{
System.out.println("\nDISPLAYING ARRAY ELEMENTS");
for (int k = 0; k < size; k++)
System.out.print(intArray[k] + " ");
System.out.println();
}
}
2-D Array Example
[04][01]
Ingrid
[04][02]
Darlene
[04][03]
Gene
[04][04]
Sean
[04][05]
Stephanie
[03][01]
Holly
[03][02]
Blake
[03][03]
Michelle
[03][04]
Remy
[03][05]
Haley
[02][01]
Diana
[02][02]
Jessica
[02][03]
David
[02][04]
Anthony
[02][05]
Alec
[01][01]
Isolde
[01][02]
John
[01][03]
Greg
[01][04]
Maria
[01][05]
Heidi
// Java1213.java
// This program demonstrates how to declare a 2-dimensional array.
public class Java1213
{
public static void main(String args[])
{
int matrix[][];
matrix = new int[3][3];
for(int r = 0; r < 3; r++)
{
for(int c = 0; c < 3; c++)
System.out.print(matrix[r][c] + " ");
0 1 2
System.out.println();
0 0 0 0
}
System.out.println();
1 0 0 0
}
2 0 0 0
}
// Java1214.java
// This program assigns a set of consecutive integers to a two-dimensional
// array and displays the values in a matrix format.
public class Java1214
{
public static void main(String args[])
{
int k = 1;
int matrix[][];
matrix = new int[7][7];
for(int r = 0; r < 7; r++)
for(int c = 0; c < 7; c++)
{
matrix[r][c] = k;
k++;
}
System.out.println();
for(int r = 0; r < 7; r++)
{
for(int c = 0; c < 7; c++)
System.out.print(matrix[r][c] + " ");
System.out.println();
}
System.out.println();
}
}
0
1
2
3
4
5
6
0
1
2
3
4
5
6
7
1
8
9
10 11 12 13 14
2
15 16 17 18 19 20 21
3
22 23 24 25 26 27 28
4
29 30 31 32 33 34 35
5
36 37 38 39 40 41 42
6
43 44 45 46 47 48 49
// Java1215.java
// This program assigns a set of consecutive integers to a two-dimensional array
// and displays the values in a two-digit format using the DecimalFormat class.
import java.text.DecimalFormat;
public class Java1215
{
public static void main(String args[])
{
DecimalFormat twoDigits = new DecimalFormat("00");
int k = 1;
int matrix[][];
matrix = new int[7][7];
for(int r = 0; r < 7; r++)
for(int c = 0; c < 7; c++)
{
matrix[r][c] = k;
0
1
k++;
}
0 1
2
System.out.println();
1 8
9
for(int r = 0; r < 7; r++)
{
2 15 16
for(int c = 0; c < 7; c++)
System.out.print(twoDigits.format(matrix[r][c])
" ");23
3 +22
System.out.println();
4 29 30
}
System.out.println()
5 36 37
}
}
6 43 44
2
3
4
5
6
3
4
5
6
7
10 11 12 13 14
17 18 19 20 21
24 25 26 27 28
31 32 33 34 35
38 39 40 41 42
45 46 47 48 49
// Java1216.java
// This program assigns random values to a matrix and displays all the
// random integers in a three-digit format with leading zeroes.
import java.text.DecimalFormat;
import java.util.Random;
public class Java1216
{
public static void main(String args[])
{
DecimalFormat threeDigits = new DecimalFormat("000");
Random random = new Random(12345);
int matrix[][];
matrix = new int[5][4];
for(int r = 0; r < matrix.length; r++)
for(int c = 0; c < matrix[r].length; c++)
matrix[r][c] = random.nextInt(1000);
0
1
.
for(int r = 0; r < matrix.length; r++)
{
0 251 080
for(int c = 0; c < matrix[r].length; c++)
1 055
System.out.print(threeDigits.format(matrix[r][c])
+ " ");
084
System.out.println();
2 501 389
}
System.out.println();
3 390 806
}
4 787 303
}
2
3
241 828
375 802
517 942
012 384
532 175
// Java1217.java
// This program assigns random doubles to a matrix and displays all the numbers in a three-digit
// format with leading zeroes, and three digits after the decimal point.
import java.text.DecimalFormat;
public class Java1217
{
public static void main(String args[])
{
DecimalFormat df = new DecimalFormat("0000.000");
double matrix[][];
matrix = new double[5][5];
double count = Math.PI;
for(int r = 0; r < 5; r++)
for(int c = 0; c < 5; c++)
{
matrix[r][c] = count;
count += Math.PI;
}
for(int r = 0; r < 5; r++)
{
for(int c = 0; c < 5; c++)
System.out.print(df.format(matrix[r][c]) + " ");
System.out.println();
}
System.out.println();
}
}
DecimalFormat Class
with format Method
Objects of DecimalFormat can control numerical output
by adding leading zeroes, adding trailing zeroes, rounding
numbers after the decimal point, adding commas in large
numbers and adding a $ sign.
DecimalFormat x = new DecimalFormat("format specification");
System.out.println(x.format(Number));
If "format specification" = "$00,000.00" then the output will
display a $ sign followed by five digits with a comma
inserted, and a decimal point followed by two digits.
AP Exam Alert
One-dimensional arrays will be tested
for the "A-level" and "AB-level" APCS
examination.
Two-dimensional arrays will only be
tested for the "AB-level“ APCS
examination.
// Java1218.java
// This program demonstrates how an argument can be entered
// from the command line using the args[] array.
// This program expects a single argument.
public class Java1218
{
public static void main(String args[])
{
System.out.println("Java1218\n");
System.out.print("Entered at command line: ");
System.out.println(args[0]);
}
}
// Java1219.java
// This program demonstrates how multiple arguments can be
// entered from the command line using the args[] array.
// This program expects three separate single arguments.
public class Java1219
{
public static void main(String args[])
{
System.out.println("Java1219\n");
System.out.println("Entered at command line:\n");
System.out.println("args[0]: " + args[0]);
System.out.println("args[1]: " + args[1]);
System.out.println("args[2]: " + args[2]);
}
}
Working with args
The Java main method always includes (String args[ ])
The args array stores String values entered at the prompt.
There are many, many ways to do interactive input in Java.
While the Scanner class will let you enter int and double
values, the vast majority of Java input methods will only let
you enter String values.
It is possible to convert entered string values to numerical
values by using Integer.parseInt & Double.parseDouble
// Java1220.java
// This program reviews the use of the <Integer> class with the
// <parseInt> method to convert strings entered at the command
// line prompt to integers.
public class Java1220
{
public static void main(String args[])
{
System.out.println("Java1220\n");
int nr1 = Integer.parseInt(args[0]);
int nr2 = Integer.parseInt(args[1]);
int sum = nr1 + nr2;
System.out.println(nr1 + " + " + nr2 + " = " + sum);
}
}
Working with args continued
main method arguments can be entered in JCreator via a small
window that pops up to request the command line information.
This window will only pop up in JCreator, if the following setting
steps are used:
•
•
•
•
•
•
•
•
•
Click Configure
Click Options
Click JDK Tools
Select Run Application in the Select Tool Type window
Click on default
Click Edit
Click the Parameters tab
Check Prompt for main method arguments
Click OK twice
// Java1221.java
// This program reviews the use of the <Double> class with the
// <parseDouble> method to convert strings entered at the command
// line prompt to real numbers.
public class Java1221
{
public static void main(String args[])
{
System.out.println("Java1221\n");
double nr1 = Double.parseDouble(args[0]);
double nr2 = Double.parseDouble(args[1]);
double sum = nr1 + nr2;
System.out.println(nr1 + " + " + nr2 + " = " + sum);
}
}