1、 声明数组
语法: 数据类型[ ] 数组名;
或者 数据类型 数组名[ ];
其中,数组名可以是任意合法的变量名,如:
int[] scores;double height[];String[] names;2、 分配空间
简单地说,就是指定数组中最多可存储多少个元素
语法: 数组名 = new 数据类型 [ 数组长度 ];
其中,数组长度就是数组中能存放元素的个数,如:
scores = new int [5];height = new double[5];names = new String[5];3、 声明数组的同时分配空间
int[] scores=new int[5]//习惯使用此法3、 声明、分配、赋值 java 中提供了一种直接创建数组的方式,它将声明数组、分配空间和赋值合并完成。如:
int[] scores={78,91,84,68};//等价于int[] ages=new int[]{15,18,22,25};应用举例:
public class HelloWorld { public static void main(String[] args) { // 定义一个长度为5的字符串数组,保存考试科目信息 String[] subjects = new String[5] ; // 分别为数组中的元素赋值 subjects[0] = "Oracle"; subjects[1] = "php"; subjects[2] = "linux"; subjects[3] = "Java"; subjects[4] = "HTML"; System.out.PRintln("数组中第4个科目为:" + subjects[3]); }}新闻热点
疑难解答