




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、6.1 基本概念6.2 聲明數(shù)組和給數(shù)組分配內(nèi)存6.3 數(shù)組應(yīng)用舉例6.4 把數(shù)組傳遞給方法6.5 多維數(shù)組6.1 基本概念6.1 基本概念 數(shù)組是由同一類(lèi)型的相關(guān)數(shù)據(jù)構(gòu)成的有序數(shù)據(jù)集合,根據(jù)數(shù)組名和下標(biāo)來(lái)唯一地確定數(shù)組中的元素。 數(shù)組是靜態(tài)實(shí)體,一旦被創(chuàng)建,其大小保持不變,但是數(shù)組引用可以重新賦值為不同大小的數(shù)組。 在Java中規(guī)定,數(shù)組的第一個(gè)元素的下標(biāo)為。 在Java中,每一個(gè)數(shù)組知道自己的長(zhǎng)度,由一個(gè)稱(chēng)為length的變量存儲(chǔ)。如:數(shù)組arr的長(zhǎng)度表示為arr.length。6.1 基本概念6.2 聲明數(shù)組和給數(shù)組分配內(nèi)存在Java中,所有的對(duì)象(包括數(shù)組)必須由new操作符動(dòng)態(tài)分配內(nèi)
2、存。對(duì)數(shù)組而言,編程人員所指定的數(shù)組元素的類(lèi)型和元素的數(shù)目是new操作符的一部分。例如,聲明一個(gè)長(zhǎng)度為12的整型數(shù)組c,并為其分配內(nèi)存,可以使用下面的語(yǔ)句:int c = new int12;也可以將聲明數(shù)組和分配內(nèi)存分開(kāi)進(jìn)行:int c; / declares the array c = new int12; / allocates the array給數(shù)組分配內(nèi)存之后,基本數(shù)據(jù)類(lèi)型數(shù)值的數(shù)組元素將會(huì)被賦默認(rèn)值為,布爾型(boolean)數(shù)組的默認(rèn)值為假(false),對(duì)于非基本類(lèi)型的數(shù)組引用,則賦予值為空(null)。可以通過(guò)一條聲明語(yǔ)句為多個(gè)數(shù)組分配內(nèi)存空間,如:String b=new
3、 String50,x=new String27;數(shù)組的類(lèi)型和方括號(hào)可以在聲明的開(kāi)始處結(jié)合起來(lái)使用,表示聲明中的所有標(biāo)識(shí)符都是數(shù)組。double arr1,arr2; arr1=new double20; arr2=new double40;6.2 聲明數(shù)組和給數(shù)組分配內(nèi)存6.3 數(shù)組應(yīng)用舉例例6.1 分配數(shù)組內(nèi)存并初始化數(shù)組元素。/ Fig. 7.3: InitArray.javaimport javax.swing.*;public class InitArray public static void main( String args ) int array; / declare ref
4、erence to an array array = new int 10 ; / dynamically allocate array String output = SubscripttValuen; for ( int counter = 0; counter array.length; counter+ ) output += counter + t + array counter + n; JTextArea outputArea = new JTextArea(); outputArea.setText( output ); JOptionPane.showMessageDialo
5、g( null, outputArea, Initializing an Array of int Values, JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); 6.3 數(shù)組應(yīng)用舉例例6.2 用初值表對(duì)數(shù)組進(jìn)行初始化。/ Fig. 7.4: InitArray.javaimport javax.swing.*;public class InitArray public static void main( String args ) int array = 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 ;
6、 String output = SubscripttValuen; for ( int counter = 0; counter array.length; counter+ ) output += counter + t + array counter + n; JTextArea outputArea = new JTextArea(); outputArea.setText( output ); JOptionPane.showMessageDialog( null, outputArea, Initializing an Array with a Declaration, JOpti
7、onPane.INFORMATION_MESSAGE ); System.exit( 0 ); 例6.2 用初值表對(duì)數(shù)組進(jìn)行初始化。例6.3 計(jì)算并存儲(chǔ)數(shù)組元素的值。/ Fig. 7.5: InitArray.javaimport javax.swing.*;public class InitArray public static void main( String args ) final int ARRAY_SIZE = 10; int array; / reference to int array array = new int ARRAY_SIZE ; / allocate array
8、 for ( int counter = 0; counter array.length; counter+ ) array counter = 2 + 2 * counter; String output = SubscripttValuen; for ( int counter = 0; counter array.length; counter+ ) output += counter + t + array counter + n; JTextArea outputArea = new JTextArea(); outputArea.setText( output ); JOption
9、Pane.showMessageDialog( null, outputArea, Initializing to Even Numbers from 2 to 20, JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); 例6.3 計(jì)算并存儲(chǔ)數(shù)組元素的值。例6.4 使用柱狀圖顯示數(shù)組數(shù)據(jù)。/ Fig. 7.7: Histogram.javaimport javax.swing.*;public class Histogram public static void main( String args ) int array = 19, 3,
10、15, 7, 11, 9, 13, 5, 17, 1 ; String output = ElementtValuetHistogram; for ( int counter = 0; counter array.length; counter+ ) output += n + counter + t + array counter + t; for ( int stars = 0; stars array counter ; stars+ ) output += *; JTextArea outputArea = new JTextArea(); outputArea.setText( ou
11、tput ); JOptionPane.showMessageDialog( null, outputArea, Histogram Printing Program, JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); 例6.4 使用柱狀圖顯示數(shù)組數(shù)據(jù)。例6.5 使用數(shù)組元素作為計(jì)數(shù)器統(tǒng)計(jì)擲骰子6000次的情況。/ Fig. 7.8: RollDie.javaimport javax.swing.*;public class RollDie public static void main( String args ) int face,
12、frequency = new int 7 ; for ( int roll = 1; roll = 6000; roll+ ) face = 1 + ( int ) ( Math.random() * 6 ); +frequency face ; String output = FacetFrequency; for ( face = 1; face frequency.length; face+ ) output += n + face + t + frequency face ; JTextArea outputArea = new JTextArea(); outputArea.set
13、Text( output ); JOptionPane.showMessageDialog( null, outputArea, Rolling a Die 6000 Times, JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); 例6.5 使用數(shù)組元素作為計(jì)數(shù)器統(tǒng)計(jì)擲骰子6000次的情況6.4 把數(shù)組傳遞給方法把數(shù)組傳遞給一個(gè)方法,應(yīng)使用不加方括號(hào)的數(shù)組名。例6.6 把數(shù)組和單個(gè)元素傳遞給方法。/ Fig. 7.10: PassArray.javaimport java.awt.Container;import javax.swing.
14、*;public class PassArray extends JApplet public void init() JTextArea outputArea = new JTextArea(); Container container = getContentPane(); container.add( outputArea ); int array = 1, 2, 3, 4, 5 ; String output = Effects of passing entire array by reference:n + The values of the original array are:n
15、; for ( int counter = 0; counter array.length; counter+ ) output += + array counter ; modifyArray( array ); output += nnThe values of the modified array are:n; for ( int counter = 0; counter array.length; counter+ ) output += + array counter ; output += nnEffects of passing array + element by value:
16、n + array3 before modifyElement: + array 3 ; modifyElement( array 3 ); output += narray3 after modifyElement: + array 3 ; outputArea.setText( output ); / end method init6.4 把數(shù)組傳遞給方法 public void modifyArray( int array2 ) for ( int counter = 0; counter array2.length; counter+ ) array2 counter *= 2; pu
17、blic void modifyElement( int element ) element *= 2; / end class PassArray public void modifyArray( in例6.7 數(shù)組排序。/ Fig. 7.11: BubbleSort.javaimport java.awt.*;import javax.swing.*;public class BubbleSort extends JApplet public void init() JTextArea outputArea = new JTextArea(); Container container =
18、getContentPane(); container.add( outputArea ); int array = 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 ; String output = Data items in original ordern; for ( int counter = 0; counter array.length; counter+ ) output += + array counter ; bubbleSort( array ); / sort array output += nnData items in ascending ord
19、ern; for ( int counter = 0; counter array.length; counter+ ) output += + array counter ; outputArea.setText( output ); 例6.7 數(shù)組排序。 public void bubbleSort( int array2 ) for ( int pass = 1; pass array2.length; pass+ ) for ( int element = 0; element array2 element + 1 ) swap( array2, element, element +
20、1 ); / end loop to control comparisons / end loop to control passes / end method bubbleSort public void swap( int array3, int first, int second ) int hold; / temporary holding area for swap hold = array3 first ; array3 first = array3 second ; array3 second = hold; / end class BubbleSort public void
21、bubbleSort( int6.5 多維數(shù)組例6.8 采用applet統(tǒng)計(jì)學(xué)生成績(jī)情況(每個(gè)學(xué)生有4門(mén)課程成績(jī))。/ Fig. 7.16: DoubleArray.javaimport java.awt.*;import javax.swing.*;public class DoubleArray extends JApplet int grades = 77, 68, 86, 73 , 96, 87, 89, 81 , 70, 90, 86, 81 ; int students, exams; String output; JTextArea outputArea; public void
22、 init() students = grades.length; / number of students exams = grades 0 .length; / number of exams outputArea = new JTextArea(); Container container = getContentPane(); container.add( outputArea ); output = The array is:n; buildString(); output += nnLowest grade: + minimum() + nHighest grade: + maxi
23、mum() + n; for ( int counter = 0; counter students; counter+ ) output += nAverage for student + counter + is + average( grades counter ); outputArea.setFont( new Font( Courier, Font.PLAIN, 12 ) ); outputArea.setText( output ); 6.5 多維數(shù)組 public int minimum() int lowGrade = grades 0 0 ; for ( int row = 0; row students; row+ ) for ( int column = 0; column exams; column+ ) if ( grades row column lowGrade ) lowGrade = grad
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 機(jī)構(gòu)公眾號(hào)面試題及答案
- 古代語(yǔ)文考試題及答案
- 甘肅英文面試題及答案
- 合水縣考試題型及答案
- 花店學(xué)員面試題及答案
- 急性溶血性貧血的臨床護(hù)理
- 保育小班個(gè)人工作方案
- 小學(xué)數(shù)學(xué)《認(rèn)識(shí)時(shí)間》教學(xué)設(shè)計(jì)
- 材料居間合同協(xié)議協(xié)議書(shū)
- 共同承擔(dān)公司債務(wù)協(xié)議書(shū)
- 涉密人員涉密資格審查表
- GB/T 2346-2003流體傳動(dòng)系統(tǒng)及元件公稱(chēng)壓力系列
- GB 5009.74-2014食品安全國(guó)家標(biāo)準(zhǔn)食品添加劑中重金屬限量試驗(yàn)
- FZ/T 10007-2018棉及化纖純紡、混紡本色紗線檢驗(yàn)規(guī)則
- 《薪酬管理的國(guó)內(nèi)外文獻(xiàn)綜述》1100字
- 設(shè)備調(diào)撥單表格
- 工廠電氣安全培訓(xùn)課件
- DB63T1743-2019青海省建筑工程資料管理規(guī)程
- 文稿成果pcb承認(rèn)書(shū)
- (精華完整版)國(guó)家開(kāi)放大學(xué)電大本科《農(nóng)業(yè)生態(tài)學(xué)》網(wǎng)絡(luò)課形考網(wǎng)考作業(yè)及答案
- 運(yùn)動(dòng)控制系統(tǒng)思考題參考答案阮毅
評(píng)論
0/150
提交評(píng)論