基本概念6.-声明数组和给数组分配内存6.-数组汇总ppt课件



《基本概念6.-声明数组和给数组分配内存6.-数组汇总ppt课件》由会员分享,可在线阅读,更多相关《基本概念6.-声明数组和给数组分配内存6.-数组汇总ppt课件(15页珍藏版)》请在装配图网上搜索。
1、单击以编辑母版标题样式,单击以编辑母版文本样式,第 6 章 数 组,6.1 基本概念,6.2 声明数组和给数组分配内存,6.3 数组应用举例,6.4 把数组传递给方法,6.5 多维数组,6.1 基本概念,6.1 基本概念,数组是由同一类型的相关数据构成的有序数据集合,根据数组名和下标来唯一地确定数组中的元素。,数组是静态实体,一旦被创建,其大小保持不变,但是数组引用可以重新赋值为不同大小的数组。,在Java中规定,数组的第一个元素的下标为。,在Java中,每一个数组知道自己的长度,由一个称为length的变量存储。如:数组arr的长度表示为arr.length。,6.1 基本概念,6.2 声明
2、数组和给数组分配内存,在Java中,所有的对象(包括数组)必须由new操作符动态分配内存。对数组而言,编程人员所指定的数组元素的类型和元素的数目是new操作符的一部分。,例如,声明一个长度为12的整型数组c,并为其分配内存,可以使用下面的语句:,int c=new int12;,也可以将声明数组和分配内存分开进行:,int c;/declares the array,c=new int12;/allocates the array,给数组分配内存之后,基本数据类型数值的数组元素将会被赋默认值为,布尔型(boolean)数组的默认值为假(false),对于非基本类型的数组引用,则赋予值为空(nu
3、ll)。,可以通过一条声明语句为多个数组分配内存空间,如:,String b=new String50,x=new String27;,数组的类型和方括号可以在声明的开始处结合起来使用,表示声明中的所有标识符都是数组。,double arr1,arr2;,arr1=new double20;,arr2=new double40;,6.2 声明数组和给数组分配内存,6.3 数组应用举例,例6.1 分配数组内存并初始化数组元素。,/Fig.7.3:InitArray.java,import javax.swing.*;,public class InitArray,public static vo
4、id main(String args),int array;/declare reference 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);,JOptionPan
5、e.showMessageDialog(null,outputArea,Initializing an Array of int Values,JOptionPane.INFORMATION_MESSAGE);,System.exit(0);,6.3 数组应用举例,例6.2 用初值表对数组进行初始化。,/Fig.7.4:InitArray.java,import javax.swing.*;,public class InitArray,public static void main(String args),int array=32,27,64,18,95,14,90,70,60,37;,S
6、tring 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,JOptionPane.INFORMATION_MESSAGE
7、);,System.exit(0);,例6.2 用初值表对数组进行初始化。,例6.3 计算并存储数组元素的值。,/Fig.7.5:InitArray.java,import 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,for(int counter=0;counter array.length
8、;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);,JOptionPane.showMessageDialog(null,outputArea,Initializing to Even Numbers fro
9、m 2 to 20,JOptionPane.INFORMATION_MESSAGE);,System.exit(0);,例6.3 计算并存储数组元素的值。,例6.4 使用柱状图显示数组数据。,/Fig.7.7:Histogram.java,import javax.swing.*;,public class Histogram,public static void main(String args),int array=19,3,15,7,11,9,13,5,17,1;,String output=ElementtValuetHistogram;,for(int counter=0;count
10、er 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(output);,JOptionPane.showMessageDialog(null,outputArea,Histogram Printing Program,JOptionPane.INFORMATION_MESSAGE);,System.e
11、xit(0);,例6.4 使用柱状图显示数组数据。,例6.5 使用数组元素作为计数器统计掷骰子6000次的情况。,/Fig.7.8:RollDie.java,import javax.swing.*;,public class RollDie,public static void main(String args),int face,frequency=new int 7;,for(int roll=1;roll=6000;roll+),face=1+(int)(Math.random()*6);,+frequency face;,String output=FacetFrequency;,f
12、or(face=1;face frequency.length;face+),output+=n+face+t+frequency face;,JTextArea outputArea=new JTextArea();,outputArea.setText(output);,JOptionPane.showMessageDialog(null,outputArea,Rolling a Die 6000 Times,JOptionPane.INFORMATION_MESSAGE);,System.exit(0);,例6.5 使用数组元素作为计数器统计掷骰子6000次的情况,6.4 把数组传递给方
13、法,把数组传递给一个方法,应使用不加方括号的数组名。,例6.6 把数组和单个元素传递给方法。,/Fig.7.10:PassArray.java,import java.awt.Container;,import javax.swing.*;,public class PassArray extends JApplet,public void init(),JTextArea outputArea=new JTextArea();,Container container=getContentPane();,container.add(outputArea);,int array=1,2,3,4,
14、5;,String output=Effects of passing entire array by reference:n+The values of the original array are:n;,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+),outp
15、ut+=+array counter;,output+=nnEffects of passing array +element by value:n+,array3 before modifyElement:+array 3;,modifyElement(array 3 );,output+=narray3 after modifyElement:+array 3;,outputArea.setText(output);,/end method init,6.4 把数组传递给方法,public void modifyArray(int array2),for(int counter=0;cou
16、nter array2.length;counter+),array2 counter *=2;,public void modifyElement(int element),element*=2;,/end class PassArray,public void modifyArray(in,例6.7 数组排序。,/Fig.7.11:BubbleSort.java,import java.awt.*;,import javax.swing.*;,public class BubbleSort extends JApplet,public void init(),JTextArea outputArea=new JTextArea();,Container container=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 arra
- 温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。