全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 【正版授权】 ISO/IEC 29168-2:2025 EN Information technology - Open systems interconnection - Part 2: Procedures for the object identifier resolution system operational agency
- 【正版授权】 IEC SRD 63301-2:2025 EN Smart city use case collection and analysis - Water systems in smart cities - Part 2: Use case analysis
- 【正版授权】 ISO/IEC 18047-6:2025 EN Information technology - Radio frequency identification device conformance test methods - Part 6: Test methods for air interface communications at 86
- TCECS 1648-2024 无机纤维复合保温板外墙外保温工程技术规程
- TCECS 1456-2023 冷库地面工程技术规程
- GBT 50838-2015 城市综合管廊工程技术标准(2024年版)
- 浙江事业单位衢州市开化县定向培养粮油储检人员招考易考易错模拟试题(共500题)试卷后附参考答案
- 河北沧州市审计局事业单位招考易考易错模拟试题(共500题)试卷后附参考答案
- 养猪场工人合同范本
- 占地建设协议书模板
- 电气主管年终总结
- 2016京东集团年会策划方案
- 餐饮服务联合体招投标协议模板
- 2024全国二卷语文高考试题
- 住宅小区电梯采购与安装 投标方案(技术方案)
- 《园艺病虫害防治》课件
- HG-T20678-2023《化工设备衬里钢壳设计标准》
- 教学能力大赛-教学实施报告《大学英语2c》
- 三农政策解读
- 企业咨询报告范文模板
- 《聪明的牧羊人》导读
评论
0/150
提交评论