sys 4.4 tar 数组 size 大小 sta 理解 ref
Java 数组
数组是用来存储固定大小的同类型的元素
声明
数组必须先声明 才能使用
datatype[] arrayRefVar;// 首选 [] 放在最后是为了 C/C++ 的快速理解 Java
char[] ch ; 或者 char ch[];
char ch = new char[arraySize];
1. 创建一个 char 数组 含有 arraySize 元素
2. 把新建 char 数组的引用 赋值给变量 ch
数组的使用
}输出结果:1.1 2.2 4.4 3.3 Total = 11.0 Max = 4.4
public class TestArr {
public static void main(String[] args) {
double[] arr = {
1.1,
2.2,
4.4,
3.3
};
for (double x: arr) {
System.out.println(x);
} //打印所有数组元素
double total = 0;
for (int i = 0; i < arr.length; ++i) {
total = total + arr[i];
}
System.out.println("Total = " + total);
//计算并打印所有元素的和
double max = arr[0];
for (int i = 1; i < arr.length; ++i) {
if (arr[i] > max) max = arr[i];
}
System.out.println("Max = " + max);
} //查找并打印最大元素值
数组作为函数的参数:
输出结果:
class A{
static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
}
public class TestA{
public static void main(String args[]){
int[] arr = {1, 2, 3, 4, 5, 6};
A.printArray(arr);
}
}
1 2 3 4 5 6
整理 Java 基础知识 -- 数组 1
来源: http://www.bubuko.com/infodetail-2459310.html