TR-Java-lecture04-array.ppt

  1. 1、本文档共32页,可阅读全部内容。
  2. 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
TR-Java-lecture04-array

Java程序设计 Java Programming Spring, 2009 Contents Arrays(数组) Arrays (数组) An array is a collection of data values, all of which have the same type. Components: Primitive types or reference to objects Arrays are objects in Java(数组是一种特殊的对象) Declaring Arrays(声明数组) An array variable is declared with the type of the members. 一维数组的声明 方法1: 类型 数组名[]; String args[]; int a[]; double amount[]; char c[]; 方法2: 类型[] 数组名; String[] args; int[] a; double[] amount; char[] c; Declaring Arrays(声明数组) 注意 数组类型是数组中元素的数据类型; 数组名是一个标识符; When an array variable is declared, the array is NOT created. What we have is only a name of an array. Creating Arrays(创建数组) 为数组元素分配内存空间,并对数组元素进行初始化 To create the array, operator new is used. We must provide the number of members in the array, and the type of the members. These cannot be changed later. 格式: 数组名 = new 数据类型[数组长度] Example: or, combining the declaration and the array creation: Creating Arrays(创建数组) 默认赋初值 整型?初值为0 int[] i = new int[3]; 实型?初值为0.0 float[] f = new float[3]; 布尔型?初值为false boolean[] b = new boolean[3]; 字符型?初值为\u0000(null) char[] c = new char[3]; Accessing(访问) array members Array members are accessed by indices using the subscript operator []. The indices are integers starting from 0. In Java, for an array of length n, the values in the array are indexed from 0 up to n – 1. If an index is out of range, i.e., less than 0 or greater than length-1, a run-time error occurs. Initializing(初始化) array members 为数组元素指定初始值 方式1 : Array members may also be initialized when the array is created(在创建数组的同时对数组元素初始化): int[] intArray; intArray = new int[]{ 3, 5, 4 }; 方式2: 声明和创建数组后对数组初始化 Example 1: Array members can be initialized individually using the indices and the subscript operator. int[] intArray = new int[3]; intArray[0] = 3; intArray[1] = 5; intArray[2] = 4; Initializing(初始化) Example 2: When an array is created, the number of positions available in the array can be accessed using a field called length w

文档评论(0)

wt60063 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档