Docsity

Multidimensional Arrays - Introduduction to Jave Programming - Lecture Slides, Slides of Java Programming

Partial preview of the text

Download Multidimensional Arrays - Introduduction to Jave Programming - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Multidimensional Arrays

Docsity.com

Announcements

  • Adding new jargon “instance of [class name]” (See: Partner.java from lab 6) p1 is an instance of Partner ... is the same as... p1 is (a variable) of type Partner Docsity.com

Multidimensional Arrays

So far we have dealt with simple (one dimensional) arrays We have represented this as all the data being stored in a line (See: LineWorld.java) Docsity.com

Multidimensional Arrays

If we think of a couple simple (one dimensional) arrays on top of each other... (See: GridWorld.java) One array for numbers 1- One array for numbers 71- Docsity.com

Multidimensional Arrays

The better way would be to make an array of length 3 to hold these 3 rows (See: BetterMultidimensionalArray.java) Docsity.com

Multidimensional Arrays

If you want a rectangular array, there is an easier way to create it: (See: BestMultidimensionalArray.java) Docsity.com

Multidimensional Arrays

foo.length = 3 (number of rows) foo[0].length = 5 (number of columns in row 0 Docsity.com

Multidimensional Arrays

If you don't know the length of the rows and columns, you should write your code like this: i goes over the rows (up and down) Number of rows j goes over the columns (left to right) Number of columns in row i Docsity.com

Ragged Arrays

Suppose we wanted to display the prime numbers listed in each 10 number interval Since there are an uneven amount of prime numbers in different ranges, this needs a ragged array (See: RaggedPrimes.java) Docsity.com

Array Facts

Whenever you write code, the outside loop should go over the rows and the inside loop should go over the columns. (See: InefficientArray.java) Docsity.com