1 / 17

More on Arrays

More on Arrays. Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading for this lecture: L&L 7.3 – 7.7. Arrays as Parameters to Methods. An entire array can be passed as a parameter to a method

howellc
Télécharger la présentation

More on Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More on Arrays • Passing arrays to or from methods • Arrays of objects • Command line arguments • Variable length parameter lists • Two dimensional arrays • Reading for this lecture: L&L 7.3 – 7.7

  2. Arrays as Parameters to Methods • An entire array can be passed as a parameter to a method • Like any other object, a reference to the array is passed, making the formal and actual parameters aliases of each other • Therefore, changing an array element within the method changes that array element in the original • An individual array element can be passed to a method as well, in which case the type of the formal parameter is the same as the element type

  3. Arrays as Return Values from Methods • An entire array can be returned by a method as its return value • Like any other object, a reference to the array is passed • If the array is declared inside the method as a local variable, then the returned reference is the only remaining reference to the array object • An individual array element can be returned by a method as well, in which case the type of the returned value is the same as the element type

  4. 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. - words - - - - Arrays of Objects • The words array when initially declared: • A reference to words.length is OK (= 5) • However, the following reference will throw a NullPointerException: System.out.println(words[0].length());

  6. “friendship” words “loyalty” “honor” - - 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”;

  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 as arguments to the main method of StateEval: > java StateEval pennsylvania texas arizona

  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: args[0]  “pennsylvania” args[1]  “texas” args[2]  “arizona” • Code in main can print the arguments: for (String arg : args) System.out.println(arg);

  11. Variable Length Parameter Lists • Suppose we want to create a method to processes a different amount of data from one invocation to the next • For example, let's define a method called average that returns the average of a variable number of integer parameters // one call to average three values mean1 = average (42, 69, 37); // another call to average seven values mean2 = average (35, 43, 93, 23, 40, 21, 75);

  12. Indicates a variable length parameter list element type array name Variable Length Parameter Lists • Using special syntax in a formal parameter list, we can define a method to accept any number of parameters of the same type • The parameters are automatically put into an array for easy processing in the method public double average (int ... list) { // see next slide }

  13. Variable Length Parameter Lists public double average (int ... list) { double result = 0.0; if (list.length != 0) { int sum = 0; for (int num : list) sum += num; result = (double)sum / list.length; } return result; }

  14. Variable Length Parameter Lists • A method can accept individual parameters and a varying number of parameters • The individual parameters must come first in the formal arguments • The varying number of parameters must come last in the formal arguments • A single method can accept only one set of varying parameters

  15. one dimension two dimensions Two-Dimensional Arrays • A one-dimensional array stores a list of elements • A two-dimensional array can be thought of as a table of elements, with rows and columns

  16. Two-Dimensional Arrays • A two-dimensional array is declared specifying the size of each dimension separately: int[][] scores = new int[12][50]; • An array element is referenced using two indexes: int value = scores[3][6] • Each dimension subdivides the previous one into the specified number of elements • Each dimension has its own length constant scores.length == 12 // 12 arrays of 50 scores[anyInteger0Through11].length == 50

  17. Two-Dimensional Arrays • A two-dimensional array is an array of arrays • The array stored in one row can be specified using one index: int [] scoreRow; scoreRow = scores[1]; // 50 int’s • scoreRow gets initialized from scores[1] scoreRow.length == 50 // for all n >= 0 && n < 50 scoreRow[n] == scores[1][n]

More Related