




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、6.1 基本概念6.2 声明数组和给数组分配内存6.3 数组应用举例6.4 把数组传递给方法6.5 多维数组6.1 基本概念6.1 基本概念 数组是由同一类型的相关数据构成的有序数据集合,根据数组名和下标来唯一地确定数组中的元素。 数组是静态实体,一旦被创建,其大小保持不变,但是数组引用可以重新赋值为不同大小的数组。 在Java中规定,数组的第一个元素的下标为。 在Java中,每一个数组知道自己的长度,由一个称为length的变量存储。如:数组arr的长度表示为arr.length。6.1 基本概念6.2 声明数组和给数组分配内存在Java中,所有的对象(包括数组)必须由new操作符动态分配内
2、存。对数组而言,编程人员所指定的数组元素的类型和元素的数目是new操作符的一部分。例如,声明一个长度为12的整型数组c,并为其分配内存,可以使用下面的语句:int c = new int12;也可以将声明数组和分配内存分开进行:int c; / declares the array c = new int12; / allocates the array给数组分配内存之后,基本数据类型数值的数组元素将会被赋默认值为,布尔型(boolean)数组的默认值为假(false),对于非基本类型的数组引用,则赋予值为空(null)。可以通过一条声明语句为多个数组分配内存空间,如:String b=new
3、 String50,x=new String27;数组的类型和方括号可以在声明的开始处结合起来使用,表示声明中的所有标识符都是数组。double arr1,arr2; arr1=new double20; arr2=new double40;6.2 声明数组和给数组分配内存6.3 数组应用举例例6.1 分配数组内存并初始化数组元素。/ 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 数组应用举例例6.2 用初值表对数组进行初始化。/ 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 用初值表对数组进行初始化。例6.3 计算并存储数组元素的值。/ 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 计算并存储数组元素的值。例6.4 使用柱状图显示数组数据。/ 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 使用柱状图显示数组数据。例6.5 使用数组元素作为计数器统计掷骰子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 使用数组元素作为计数器统计掷骰子6000次的情况6.4 把数组传递给方法把数组传递给一个方法,应使用不加方括号的数组名。例6.6 把数组和单个元素传递给方法。/ 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 把数组传递给方法 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 数组排序。/ 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 数组排序。 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 多维数组例6.8 采用applet统计学生成绩情况(每个学生有4门课程成绩)。/ 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 多维数组 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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年中国沙棘果粉市场调查研究报告
- 个人承揽合同范例写
- 制造公司用工合同范本
- 2025年中国摩托车工具箱市场调查研究报告
- 2025年中国废边纱弹王片市场调查研究报告
- 2025年中国宽动态彩色转黑摄像机市场调查研究报告
- 2025年中国台式塑料袋封口机市场调查研究报告
- 传输分包合同范例
- 仓储玉米收购合同范例
- yiaoliao供销合同范本
- 大学生心理健康 第3章-教学教案-自我意识
- 名著《骆驼祥子》中考真题及典型模拟题训练(原卷版)
- 女性健康知识讲座超美的课件
- 2025年兴安职业技术学院单招职业技能测试题库汇编
- 2025年黑龙江职业学院单招职业技能测试题库审定版
- 2025年湖南汽车工程职业学院单招职业技能测试题库参考答案
- 拆除工程方案
- 2025年合肥职业技术学院单招职业适应性测试题库及参考答案
- 中职高教版(2023)语文职业模块-第一单元1.2宁夏闽宁镇:昔日干沙滩今日金沙滩【课件】
- 2025年春季1530安全教育记录主题
- 2024年国家公务员考试行测真题附解析答案
评论
0/150
提交评论