Java 数组的类型有哪些?
Java 中有两种数组,它们是 −
单维数组 − Java 中的单维数组是一个普通数组,其中数组包含的顺序元素(类型相同) −
int[] myArray = {10, 20, 30, 40}
示例
public class TestArray {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (int i = 0; i < myList.length; i++) {
System.out.println(myList[i] + " ");
}
// Summing all elements
double total = 0;
for (int i = 0; i < myList.length; i++) {
total += myList[i];
}
System.out.println("Total is " + total);
// Finding the largest element
double max = myList[0];
for (int i = 1; i < myList.length; i++) {
if (myList[i] > max) max = myList[i];
}
System.out.println("Max is " + max);
}
}
输出
1.9 2.9 3.4 3.5 Total is 11.7 Max is 3.5
多维数组 − Java 中的多维数组是数组的数组。二维数组是一维数组的数组,三维数组是二维数组的数组。
示例
public class Tester {
public static void main(String[] args) {
int[][] multidimensionalArray = { {1,2},{2,3}, {3,4} };
for(int i = 0 ; i < 3 ; i++){
//row
for(int j = 0 ; j < 2; j++){
System.out.print(multidimensionalArray[i][j] + " ");
}
System.out.println();
}
}
}
输出
1 2 2 3 3 4
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP