lec14

lec14 - CSE 8A: Lecture 14 2D arrays Arrays and the Java...

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

View Full Document Right Arrow Icon
Page 1 of 23 CSE 8A UCSD LEC 14 CSE 8A: Lecture 14 2D arrays Arrays and the Java type system Arrays and Vectors (Reading: Savitch, Ch. 6)
Background image of page 1

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

View Full DocumentRight Arrow Icon
Page 2 of 23 CSE 8A UCSD LEC 14 Midterm Exam #2 Midterm Exam #2 will be Thu Feb 28, during lecture time. Place: Lecture room Closed-book, closed-notes, no calculators. Bring picture ID! Coverage: Chapters 1-6, Lectures 1-14, Assignments P1-P6 A practice midterm is available online (PDF format) Lecture notes are available online Midterm review: Lecture Tues Feb 26
Background image of page 2
Page 3 of 23 CSE 8A UCSD LEC 14 Multidimensional arrays in Java An array is a sequence of variables, all of the same type these reference type pointers or primitive values are the elements of the array these elements can be of any type (primitive types, classes. .. even arrays!), but they have to be of the same type So, it is possible in Java to have an array whose elements are themselves pointers to arrays this is called an array of arrays, or a multidimensional array It is possible to have arrays of arrays of arrays, and arrays of arrays of arrays of arrays, etc., etc. we will concentrate on 2-dimensional arrays: arrays whose elements are arrays of primitive type values or of non-array class type pointers
Background image of page 3

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

View Full DocumentRight Arrow Icon
Page 4 of 23 CSE 8A UCSD LEC 14 Declaring 2-dimensional arrays in Java An 2-dimensional array is declared with a declaration statement of the form <typename> <identifier>[][]; or <typename>[][] <identifier>; or <typename>[] <identifier>[]; This declares <identifier> to be the name of an array whose elements are arrays that contain elements of type <typename> . All of these are legal, but the second form is probably clearest and most readable. .. Examples: int arr[][]; // declares arr to be an array of arrays of ints Button[][] button; // button is an array of arrays of Buttons boolean[] vals[]; // vals is an array of arrays of booleans
Background image of page 4
Page 5 of 23 CSE 8A UCSD LEC 14 Creating 2-dimensional arrays in Java As with all objects in Java, a mere declaration statement never creates an array of arrays To create a 2-d array in Java, you use new , the type of elements in the array, and two bracketed integer expressions that specify the dimensions of the array: new <typename> [ <len1> ] [ <len 2> ] <len1> is the number of arrays; <len2> is the number of elements in each array The elements of the arrays are automatically initialized to default values: zero for numerical types; false for boolean; null for class types Examples: new int[25][4] // creates an array of 25 arrys of 4 ints each, // total 100 ints, all initially 0 new Button[10][10] // creates an array of 10 arrays of 10 Button // pointers each, total 100 pointers, all // null (this creates no Button objects!) new boolean[40][50] // creates an array of 40 arrays of 50 // booleans each, total 2000 booleans, // all initially false
Background image of page 5

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

View Full DocumentRight Arrow Icon
Page 6 of 23 CSE 8A UCSD LEC 14 Indexing 2-dimensional arrays You refer to an an element of a 2-d array by using the array name and two bracketed int indexing expressions
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 06/12/2008 for the course CSE 8 taught by Professor Marx during the Fall '08 term at UCSD.

Page1 / 23

lec14 - CSE 8A: Lecture 14 2D arrays Arrays and the Java...

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