物件導向程式設計
|
3.7 陣列 |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
授課教師:陳慶瀚 WWW : http://www.miat.ee.isu.edu.tw/java E-mail : pierre@isu.edu.tw |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Like
all other variables in Java, an array must have a specific type like Like
all other variables in Java an array must be declared. When you declare an array
variable you suffix the type with
This
says that You also have the option to append the brackets to the variable instead of the type.
The choice is primarily one of personal preference. You can even use both at the same time like this
配置陣列記憶體 Declaring
arrays merely says what kind of values the array will hold. It does not create
them. Java arrays are objects, and like any other object you use the
The
numbers in the brackets specify the length of the array; that is, how many slots
it has to hold values. With the lengths above
Individual components of an array are referenced by the array name and by an integer which represents their position in the array. The numbers you use to identify them are called subscripts or indexes into the array. Subscripts
are consecutive integers beginning with 0. Thus the array
This step is called initializing the array or, more precisely, initializing the components of the array. Sometimes the phrase "initializing the array" is used to mean when you initialize all the components of the array. For even medium sized arrays, it's unwieldy to specify each component individually. It is often helpful to use for loops to initialize the array. Here is a loop which fills an array with the squares of the numbers from 0 to 100.
Two things you should note about this code fragment:
課堂練習: 1. 使用一維陣列存放使用者由main(String args[])傳入的字串引數,陣列大小為args.length,每個字串引數分別放在args[0],args[1],......,最後再將陣列輸出至螢幕 。(範例程式) 2. 使用一維陣列存放10個使用者輸入的整數值,計算其平均值並輸出。(範例程式)
So far all these arrays have been one-dimensional. That is, a single number could locate any value in the array. However sometimes data is naturally represented by more than one number. For instance a position on the earth requires a latitude and a longitude. The most common kind of multidimensional array is the two-dimensional array. If you think of a one-dimensional array as a column of values, you can think of a two-dimensional array as a table of values like this
Here we have an array with five rows and four columns. It has twenty total elements. However we say it has dimension five by four, not dimension twenty. This array is not the same as a four by five array like this one:
We
need to use two numbers to identify a position in a two-dimensional array. These
are the element's row and column positions. For instance if the above array is
called Here's
how the elements in a four by five array called
Two dimensional arrays are declared, allocated and initialized much like one dimensional arrays. However you have to specify two dimensions rather than one, and you typically use two nested for loops to fill the array. This example fills a Two-Dimensional Array with the sum of the row and column indexes
Of course the algorithm you use to fill the array depends completely on the use to which the array is to be put. Program 9.3 calculates the identity matrix for a given dimension. The identity matrix of dimension N is a square matrix which contains ones along the diagonal and zeros in all other positions.
In two-dimensional arrays ArrayIndexOutOfBoundsExceptions occur whenever you exceed the maximum column index or row index. You can also declare, allocate, and initialize a a two-dimensional array at the same time by providing a list of the initial values inside nested brackets. For instance the three by three identity matrix could be set up like this:
The spacing and the line breaks used above are purely for the programmer. The compiler doesn't care. The following works equally well:
3. 使用9x9的二維陣列存放九九乘法表並輸出至螢幕 。 4. 設計一個3x3矩陣相加的程式,輸入兩個矩陣值,輸出矩陣相加結果。 5. 設計一個3x3矩陣相乘的程式,輸入兩個矩陣值,輸出矩陣相乘結果。 3.7.5 多維陣列的宣告、配置和初值化 You don't have to stop with two dimensional arrays. Java permits arrays of three, four or more dimensions. However chances are pretty good that if you need more than three dimensions in an array, you're probably using the wrong data structure. Even three dimensional arrays are exceptionally rare outside of scientific and engineering applications. The syntax for three dimensional arrays is a direct extension of that for two-dimensional arrays. The program below declares, allocates and initializes a three-dimensional array. The array is filled with the sum of its indexes.
You
need the additional nested 課堂練習:
6. 使用二維float陣列來建立一個10(rows)x3(columns)的資料表M,第一個column存放[0,10]之間的亂數值,第二個column存放 第一個column的平方值,第三個column存放 第一個column的開根號值。 提示: A. 使用Math.random( )產生介於0到1的亂數值,例 doublw r = Math.random( ); B. 開根號的函式為和Math.sqrt(double ) 7. 擴充練習6的程式,再產生一個3(rows)x3(columns)的二維float陣列T。第一個row的三個column分別存放M的三個column所有值當中的最大值,第二個row的三個column分別存放M的三個column所有值當中的最小值,第三個row的三個column分別存放M的三個column的平均值。 |
資 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
物件導向程式設計 義守大學電機系 陳慶瀚 2001.10.22 |