1.数组的定义方式:
package Day_04;
/*
* 数组定义
*
* 数据类型[] 数组名 = new 数据类型[长度];
* 数组名[下标] = 数值
*
* 数据类型[] 数组名 = new 数据类型[]{元素1,元素2,元素3,...,元素n};
* 数据类型[] 数组名 = {元素1,元素2,元素3,...,元素n};
* 数组越界异常:java.lang.ArrayIndexOutOfBoundsException
*/
public class shuzu {
public static void main(String[] args) {
//方式1
int[] scores = new int[3];
scores[0] = 60;
scores[1] = 70;
scores[2] = 80;
/*当添加scores[3] = 90; 时
即会出现数组越界异常:java.lang.ArrayIndexOutOfBoundsException*/
//方式2
/* int[] scores = new int[]{60,70,80};*/
//方式3
/* int[] scores = {60,70,80};*/
for(int i = 0;i<scores.length;i++){
System.out.println(scores[i]);
}
}
}
2.数组的升序: