全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 合伙工程承包协议书
- 2026-2031中国光敏传感器行业市场发展分析及前景趋势与投资研究报告
- 2026-2031中国固态电容器市场投资前景预测研究报告
- 2025放射医学主治医师放射医学344主治医师考试课程题库
- 2025年自考专升本酒店管理试题及答案
- 国际市场拓展与海外销售渠道构建考核试卷及答案
- 2026-2031中国配电柜市场调研及发展趋势预测报告
- 2026年海峡两岸游戏食品合作委员会合作协议
- 2026年目标疾病管理合同
- 2026年美食创意服务合同
- 国家级教改课题申报书
- 排水沟工程施工合同协议书6篇
- 2025四川高考政治试题解读及2026备考策略指导课件
- 幕墙创优工程汇报材料
- 矿区生态修复后期管护方案
- 2025至2030中国船用阀和执行器行业产业运行态势及投资规划深度研究报告
- 厂房电力系统安全评估报告
- 农业银行2025秋招结构化面试经典题及参考答案四川地区
- 电力三种人安全培训课件
- XX县XX镇事业单位管理岗位职员等级晋升制度工作实施方案
- 2025年共青团入团考试测试试卷题库及答案
评论
0/150
提交评论