Download presentation
Presentation is loading. Please wait.
Published byCorey Woodhams Modified over 9 years ago
1
11 Chapter 8 ARRAYS Continued
2
22 AN ARRAY OF STRINGS We can create arrays of strings or other types of objects.
3
33 AN ARRAY OF STRINGS Example: We can create an array of String objects containing the strings "Texas", "Oklahoma", "Kansas", and "Nebraska" by writing: String[ ] states = {"Texas", "Oklahoma", "Kansas", "Nebraska"}; states[0] is the first string in the list, “Texas”. states[1] is the second string in the list, “Oklahoma”..
4
44 AN ARRAY OF STRINGS ***See the program DisplayStringArray.java on webct
5
55 AN ARRAY OF STRINGS ***See the program DemoStringArray.java on webct
6
66 AN ARRAY OF STRINGS In order to use a String object you need a reference to the String object, so an array of strings is really an array of references to String objects.
7
77 AN ARRAY OF STRINGS The declaration String[ ] studentNames creates a variable that can store a reference to an array of String references. String[ ] studentNames = new String[5]; address studentNames
8
88 AN ARRAY OF STRINGS The new operator is used to create an array of five references to String objects. The address of this array is assigned to studentNames. String[ ] studentNames = new String[5]; null [0] [1] [2] [3] address studentNames null [4]
9
99 AN ARRAY OF STRINGS So far we have not created any String objects. The key word null indicates that the String reference variables do not reference String objects. String[ ] studentNames = new String[5]; null [0] [1] [2] [3] address studentNames null [4] The key word null indicates that the reference variables do not reference an object.
10
10 AN ARRAY OF STRINGS We can create a String object and assign the address of the object to one of the String reference variables in the array as shown below: String[ ] studentNames = new String[5]; studentNames[0] = "Jane Doe"; address null [0] [1] [2] [3] address studentNames null [4] Jane Doe
11
11 AN ARRAY OF STRINGS We can also use the nextLine method of a Scanner object or the showInputDialog method of the JOptionPane class to read a string entered by the user and store the address of the String object returned by these methods in one of the reference variables in the array. String[ ] studentNames = new String[5]; studentNames[1] = JOptionPane.showInputDialog(”Enter the 2nd student’s name.”); address null [0] [1] [2] [3] address studentNames null [4]
12
12 AN ARRAY OF STRINGS In DemoStringArray.java the String objects are created when we call the nextLine method of a Scanner object. String[ ] studentNames = new String[5]; null [0] [1] [2] [3] address studentNames null [4]
13
13 AN ARRAY OF STRINGS The member methods of a String object can be used on array elements that reference a String object.
14
14 AN ARRAY OF STRINGS Given the following declaration: String[ ] states = {"Texas", "Oklahoma", "Kansas", "Nebraska"}; The statement below assigns the variable firstLetter the value of ‘T’. Remember states[0] is a reference to a String object and every String object has a member method named charAt. char firstLetter = states[0].charAt(0);
15
15 AN ARRAY OF STRINGS Given the following declaration: String[ ] states = {"Texas", "Oklahoma", "Kansas", "Nebraska"}; The variable named equalStrings is assigned the value false in the segment below. Every String object has a member method named equalsIgnoreCase. boolean equalStrings = states[1].equalsIgnoreCase(”Texas");
16
16 AN ARRAY OF STRINGS Given the following declaration: String[ ] states = {"Texas", "Oklahoma", "Kansas", "Nebraska"}; The statement below displays NEBRASKA on a line on the computer screen. Notice we are not storing the reference to the uppercase copy of the string that was created, we are simply displaying this string on the screen. System.out.println(states[3].toUpperCase( ));
17
17 PARALLEL ARRAYS In many applications there are several pieces of data of different data types that are related to each other. For example: –A employee's ID number, name, address, telephone number, pay rate –A student's ID number, name, major, and GPA –A book’s ISBN number, title, author, publisher, price, and quantity available
18
18 PARALLEL ARRAYS To maintain the relationship between these pieces of data and process them in parallel, we can set up a system of parallel arrays. In a system of parallel arrays, data for a particular entity (employee, student, book) is stored at the same relative position/offset/subscript in a number of separate arrays.
19
19 PARALLEL ARRAYS *** See Case Study 4 on the CD that came with the textbook for a discussion of parallel arrays
20
20 PARALLEL ARRAYS *** See the program ParallelArrays.java on webct.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.