全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Arrays (subject of Lab 7)An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. Youve seen an example of arrays already, in the main method of the Hello World! application. An array of ten elementsEach item in an array is called an element, and each element is accessed by its numerical index. As shown in the above illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8. The following program, ArrayDemo, creates an array of integers, puts some values in it, and prints each value to standard output. class ArrayDemo public static void main(String args) int anArray; / declares an array of integers anArray = new int10; /allocates memory for 10 integers anArray0 = 100; / initialize first element anArray1 = 200; / initialize second element anArray2 = 300; / etc. anArray3 = 400; anArray4 = 500; anArray5 = 600; anArray6 = 700; anArray7 = 800; anArray8 = 900; anArray9 = 1000; System.out.println(Element at index 0: + anArray0); System.out.println(Element at index 1: + anArray1); System.out.println(Element at index 2: + anArray2); System.out.println(Element at index 3: + anArray3); System.out.println(Element at index 4: + anArray4); System.out.println(Element at index 5: + anArray5); System.out.println(Element at index 6: + anArray6); System.out.println(Element at index 7: + anArray7); System.out.println(Element at index 8: + anArray8); System.out.println(Element at index 9: + anArray9); The output from this program is: Element at index 0: 100Element at index 1: 200Element at index 2: 300Element at index 3: 400Element at index 4: 500Element at index 5: 600Element at index 6: 700Element at index 7: 800Element at index 8: 900Element at index 9: 1000In a real-world programming situation, youd probably use one of the supported looping constructs (for, while, do, etc.) to iterate through each element of the array, rather than write each line individually as shown above. However, this example clearly illustrates the array syntax. Declaring a Variable to Refer to an ArrayThe above program declares anArray with the following line of code: int anArray; / declares an array of integersLike declarations for variables of other types, an array declaration has two components: the arrays type and the arrays name. An arrays type is written as type, where type is the data type of the contained elements; the square brackets are special symbols indicating that this variable holds an array. The size of the array is not part of its type (which is why the brackets are empty). An arrays name can be anything you want, provided that it follows the rules and usual Java naming conventions. As with variables of other types, the declaration does not actually create an array it simply tells the compiler that this variable will hold an array of the specified type. Similarly, you can declare arrays of other types: byte anArrayOfBytes;short anArrayOfShorts;long anArrayOfLongs;float anArrayOfFloats;double anArrayOfDoubles;boolean anArrayOfBooleans;char anArrayOfChars;String anArrayOfStrings;You can also place the square brackets after the arrays name: float anArrayOfFloats; / this form is discouragedHowever, convention discourages this form; the brackets identify the array type and should appear with the type designation. Creating, Initializing, and Accessing an ArrayOne way to create an array is with the new operator. The next statement in the ArrayDemo program allocates an array with enough memory for ten integer elements and assigns the array to the anArray variable. anArray = new int10; / create an array of integersIf this statement were missing, the compiler would print an error like the following, and compilation would fail: ArrayDemo.java:4: Variable anArray may not have been initialized.The next few lines assign values to each element of the array: anArray0 = 100; / initialize first elementanArray1 = 200; / initialize second elementanArray2 = 300; / etc.Each array element is accessed by its numerical index: System.out.println(Element 1 at index 0: + anArray0);System.out.println(Element 2 at index 1: + anArray1);System.out.println(Element 3 at index 2: + anArray2);Alternatively, you can use the shortcut syntax to create and initialize an array: int anArray = 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000;Here the length of the array is determined by the number of values provided between and . You can also declare an array of arrays (also known as a multidimensional array) by using two or more sets of square brackets, such as String names. Each element, therefore, must be accessed by a corresponding number of index values. In the Java programming language, a multidimensional array is simply an array whose components are themselves arrays. This is unlike arrays in C or Fortran. A consequence of this is that the rows are allowed to vary in length, as shown in the following MultiDimArrayDemo program: class MultiDimArrayDemo public static void main(String args) String names = Mr. , Mrs. , Ms. , Smith, Jones; System.out.println(names00 + names10); /Mr. Smith Syste
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 长春师范大学《结构生物学》2025-2026学年期末试卷
- 福建江夏学院《中学生心理教育》2025-2026学年期末试卷
- 眩晕患者健康指导-1
- 安全生产培训加盟讲解
- 行业职业发展规划模板
- IPQC 巡检与拦截特训
- 导猎员岗前生产标准化考核试卷含答案
- 呼吸治疗师班组建设竞赛考核试卷含答案
- 力学计量员创新方法强化考核试卷含答案
- 纺丝凝固浴液配制工安全文化考核试卷含答案
- GB 35574-2017热电联产单位产品能源消耗限额
- CB/T 3480-1992钢通舱管件
- 2023年高中物理竞赛讲义
- rm928x产品规格书-中文版rm9284d
- 附件1:岗位分等级聘用考核量化表(竞岗竞聘积分表)
- 《内部控制与风险管理(第二版)》第九章财务风险管理
- 综合布线施工标准作业指导书
- 巡察工作流程图1
- 职工食堂危险源识别评价表完整
- 名特优经济林基地建设技术规程LY/T1557-2000
- DB14∕T 2270-2021 煤炭洗选行业污染物排放标准
评论
0/150
提交评论