Download presentation
Presentation is loading. Please wait.
Published byEmmeline Merritt Modified over 7 years ago
1
More on Arrays Review of Arrays of ints, doubles, chars
Arrays of objects Command line arguments The ArrayList class Javadoc Review Lecture 8 notes and L&L 7.1 – 7.2 Reading for this lecture: L&L 7.3 – 7.7, App I
2
Review of Arrays We can declare an array of int variables with the capability to use an index variable to select one variable int[ ] nums = new int [5]; The above declares 5 variables of type int The valid array index values are 0-4 Note: Values have not been assigned to those 5 variables in the array yet. To set a value, can use, for example nums[0] = 3; Similarly, an array of 10 double variables: double[ ] nums1 = new double[10]; 2
3
Review: Array Initializer Lists
An array can be defined and initialized so that each element contains a specific value: int[] primes = {2,3,5,7,11, 13}; char [] vowels = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’}; Java uses the initializer list to determine how long the array must be and allocates that many elements An initializer list can be used only when the array is first declared, as above Afterward, each individual element of the array can be accessed with an index, for example: int firstPrime = primes[0]; // 2 boolean result = vowels[3] == ‘o’ // true 3
4
String[] words = new String[5];
Arrays of Objects The elements of an array can be object references The following declaration reserves space to store 5 references to String objects String[] words = new String[5]; It does NOT create the String objects themselves Initially an array of objects holds null references Each object stored in an element of an array must be instantiated separately
5
System.out.println(words[0].length());
Arrays of Objects The words array when initially declared: words - A reference to words.length is OK (= 5) However, the following reference will throw a NullPointerException: System.out.println(words[0].length());
6
Arrays of Objects To create some String objects and store them in elements of the array: words[0] = new String(“friendship”); words[1] = “loyalty”; words[2] = “honor”; words “friendship” “loyalty” “honor” - -
7
Arrays of Objects String objects can be created using literals
The following declaration creates an array object called verbs with a length of 4 and fills it with references to four String objects created using string literals String[] verbs = {"play", "work", "eat", "sleep"};
8
Arrays of Objects To use one of the methods of an object element of an array: verbs[2].equals(“eat”); // true To pass one of the object elements of an array as a parameter to a method: “eat”.equals(verbs[2]); // true To return an element of an array: public String methodName(String [] verbs) { return verbs[2]; // “eat” }
9
Command-Line Arguments
Your program’s main method is defined as: public static void main(String [] args) The signature of the main method indicates that it takes an array of String objects as a parameter These values come from command-line arguments that are provided when the interpreter is invoked In Dr Java interactions pane, this invocation of the JVM passes three String objects (or tokens) as arguments to the main method of StateEval: > java StateEval pennsylvania texas arizona Command Line “Tokens”
10
Command Line Arguments
These strings are stored at indexes 0-2 in the array args for the main method The array args will contain: Code in main can print the arguments: for (String arg : args) System.out.println(arg); args “pennsylvania” “texas” “arizona”
11
The ArrayList Class The ArrayList class is in java.util package
Instantiating an empty ArrayList ArrayList<String> myList = new ArrayList<String>( ); Like an array: ArrayList can store a list of object references You can access each one using a numeric index Unlike an array: ArrayList object grows and shrinks as needed You don’t use [ ] syntax with an ArrayList object Cannot store primitive types (Use Wrapper classes)
12
The ArrayList Class The ArrayList class is available in the java.util package Instantiating an empty ArrayList: ArrayList<String> myList = new ArrayList<String>( ); An ArrayList stores references to the class inside the < > which allows it to store objects of that class only This is a part of Java’s generics capability which you will study further in CS210 12
13
The ArrayList Class Strings are inserted with a method invocation
boolean b = myList.add(string); // to end myList.add(index, string); // at index When an element is inserted at a specific index, the other elements are "moved aside" to make room If index > myList.size(), the method throws an IndexOutOfBounds exception Elements are removed with a method invocation String s = myList.remove(index); When an element is removed, the list "collapses" to close the gap and maintain contiguous indexes
14
ArrayList Efficiency The ArrayList class is implemented using an underlying array The array is manipulated so that indexes remain contiguous as elements are added or removed If elements are added to and removed from the end of the list, this processing is fairly efficient But as elements are inserted and removed from the front or middle of the list, the remaining elements are shifted
15
Javadoc Javadoc is a JDK tool that creates HTML user documentation for your classes and their methods In this case, user means a programmer who will be writing Java code using your classes You can access Javadoc via the JDK CLI: javadoc MyClass.java This creates MyClass.html You can access Javadoc via Dr Java menu: Tools > Javadoc >Javadoc All Documents Tools > Javadoc >Preview Javadoc for Current Document
16
Javadoc The Javadoc tool scans your source file for specialized multi-line style comments: /** * <p>HTML formatted text here</p> */ Your Javadoc text is written in HTML so that it can appear within a standardized web page format We’ll just use plain text: it works fine too * Plain text here
17
Block Tags for Classes At the class level, you should include these block tags with data (each on a separate line): /** * @author Your Name * @version Version Number or Date */ You should include text describing the use of this class and perhaps give examples
18
Block Tags for Methods At the method level, you should include these block tags, especially for public methods: /** * @param text for 1st parameter * @param text for 2nd parameter * @return text for return value */ If there are no parameters or return type, you can omit these Javadoc block tags
19
Example of Javadoc /** * Represents a box Robert Cohen 1.0 */ public class Box { private int id; // id for Box private int height; // height of Box * Constructs a Box id the id of the Box height the height of the Box public Box(int id, int height) { this.id = id; this.height = height; } /** * Returns the height of the box (a getter) the height of the box */ public int getHeight() { return height; } A string representation of the Box public String toString() { return "Box " + id + ": " + height;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.