Материал готовится,

пожалуйста, возвращайтесь позднее

пожалуйста, возвращайтесь позднее

Suppose we are trying to keep track of the colors in a bitmap, or elevation data on a map. While we could store this in an array, we think about this two-dimensionally, like a matrix. Java allows us to write code that matches how we think about the data using multi-dimensional arrays. In java, square brackets are used to indicate you are declaring an array variable. What precedes the brackets are the kind of thing stored in the array. Since that can be any type, we can also have it be an array. So, declaring double bracket bracket bracket bracket means that we have an array of arrays of doubles.As with normal arrays, we don’t actually have an array until we call a constructor. Here, we are making an array with 3 rows and 7 columns. Although we can write this with a single constructor, what is actually happening is that we are first making an array of size three which will hold pointers to the rows, and then we are filling that array by calling a constructor to make an array of seven doubles three times, once for each row.This slide shows a picture what the allocation creates. Percentages is an array with 3 elements. Each of those elements refers to an array of 7 doubles. Java automatically initializes each of the doubles to zero. Since Java arrays begin at zero, the elements of percentages are numbered from 0 to 2, and each row of percentages is indexed from 0 to 6. The method fillArray shows two nested for loops that fill a two-dimensional array of integers with random numbers from 0 to max minus 1. The outer loop counts from zero to array dot length minus one, which is the number of rows in this two-dimensional array. Inside that loop we have another loop so that we process every element of every row. The inner loop goes from zero to array bracket I dot length, which is the length of the ith row. Although generally Java programmers create two dimensional arrays where every row is the same length, the language does not require this. Adding this line of code at the beginning of the method would replace the first row with a row of length 27. Next we’ll show how to find the maximum value of a two-dimensional array that has at least one element. The code first sets max to be the first element, and then loops through the array. If it ever finds something larger than max, it replaces the value of max. Again, we use two nested loops to look at each item of a two dimensional array. If we made a three dimensional array, we’d use three nested loops, and so on. You can have as many dimensions as you want by simply adding more brackets. The size of the array does grow exponentially with the number of dimensions, so don’t get too carried away.In review, Java allows you to create arrays of arrays, which corresponds to multi-dimensional arrays. When processing all of the elements of the array, you use one nested loop for each dimension of the array. And, Java arrays always are indexed beginning at zero, going from zero to length minus 1. You can download a sample program with the methods fillArray and findMax from java.martincarlisle.com.

Загрузка...

Выбрать следующее задание

Ты добавил

Выбрать следующее задание

Ты добавил