Presentation is loading. Please wait.

Presentation is loading. Please wait.

First Data Structure Definition A data structure is a data type whose components are smaller data structures and/or simple data types.

Similar presentations


Presentation on theme: "First Data Structure Definition A data structure is a data type whose components are smaller data structures and/or simple data types."— Presentation transcript:

1

2 First Data Structure Definition A data structure is a data type whose components are smaller data structures and/or simple data types.

3 Data Structure Starting Point Any data type that can store more than one value is a data structure.

4 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.

5 First Record Definition A record is a data structure with one, or more, elements, called fields, of the same or different data types.

6 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.

7 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.

8 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.

9 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.

10 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

11 Defining Static Arrays int list[ ]; // declares the array list identifier list = new int[10]; // allocates memory for 10 integers char names[ ]; // declares the names array identifier names = new char[25]; // allocates memory for 25 characters double grades[ ]; // declares the grades array identifier grades = new double[50]; // allocates memory for 50 doubles

12 Defining Static Arrays Preferred Method int list[ ] = new int[10]; char names[ ] = new char[25]; double grades[ ] = new double[50];

13 // 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(); } 0123456789 0000000000

14 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.

15 // 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(); } 0123456789 0000000000

16 // Java1203.java // This program demonstrates how to initialize array elements. // The 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(); } 012345678 112233445566778899

17 // 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(); } list1 01234 ????? list2 0123 char

18 // 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(); } 0123456789

19 // 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]); System.out.println(); } list1 01234 null list2 01234 AAABBBCCCDDDEEE

20 // 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(); } 012345678910111213141516171819 851680241928455284575802701889717142890206312584687803432775

21 // 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(); } 0123 JoeTomSueMeg

22 // 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(); } 0123456789 AABBCCDDEEFFGGHHIIJJ

23 // 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(); }

24 // 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

25 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(); }

26 // 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

27 class List3 { private int intArray[]; // stores array elements private int size; // number of elements in the array public List3(int s) { 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(); }

28 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

29 // 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] + " "); System.out.println(); } System.out.println(); } 012 0 000 1 000 2 000

30 // 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(); } 0123456 0 1234567 1 891011121314 2 15161718192021 3 22232425262728 4 29303132333435 5 36373839404142 6 43444546474849

31 // 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; k++; } System.out.println(); for(int r = 0; r < 7; r++) { for(int c = 0; c < 7; c++) System.out.print(twoDigits.format(matrix[r][c]) + " "); System.out.println(); } System.out.println() } 0123456 0 1234567 1 891011121314 2 15161718192021 3 22232425262728 4 29303132333435 5 36373839404142 6 43444546474849

32 // 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);. for(int r = 0; r < matrix.length; r++) { for(int c = 0; c < matrix[r].length; c++) System.out.print(threeDigits.format(matrix[r][c]) + " "); System.out.println(); } System.out.println(); } 0123 0 251080241828 1 055084375802 2 501389517942 3 390806012384 4 787303532175

33 // 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(); }

34 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.

35 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.

36 // 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]); }

37

38 // 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]); }

39 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

40 // Java1220.java // This program reviews the use of the class with the // 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); }

41 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

42 // Java1221.java // This program reviews the use of the class with the // 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); }


Download ppt "First Data Structure Definition A data structure is a data type whose components are smaller data structures and/or simple data types."

Similar presentations


Ads by Google