Lecture16 - More on Arrays Passing arrays to or from...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays The ArrayList class Reading for this lecture: L&L 7.3 – 7.7
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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
Background image of page 4
5 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());
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Arrays of Objects To create some String objects and store
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/25/2010 for the course CS 110 taught by Professor Wilson during the Spring '10 term at University of Massachusetts Boston.

Page1 / 21

Lecture16 - More on Arrays Passing arrays to or from...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online