数组中 Java

什么是 Java 大批?
A Java 排列 是一个固定大小的结构,它将同一数据类型的元素存储在连续的内存空间中。每个元素都通过一个从零开始的索引访问,因此第一个元素位于 0 ... 0 最后一次是在 length - 1数组继承自 Object 并实施 Serializable 和 Cloneable并且可以保存基本类型或对象引用。
数组取代了长串编号变量。考虑六个独立的整数:
x0 = 0; x1 = 1; x2 = 2; x3 = 3; x4 = 4; x5 = 5;
这六个值存储在一个长度为 6 的数组中:
x[0] = 0; x[1] = 1; x[2] = 2; x[3] = 3; x[4] = 4; x[5] = 5;
A 循环 变量可以驱动括号中的索引,因此只需三行代码即可处理每个元素,而不是六行代码:
for (int count = 0; count < 5; count++) { System.out.println(x[count]); }
数组类型 Java
Java 数组有两种形式:
- 一维数组: 元素的扁平列表。
- 多维数组: 数组的数组,用于网格和矩阵。
数组变量:声明、构造和初始化
使用数组是一种 三个步骤声明,构造 new然后进行初始化。
1)声明数组
语法:
<elementType>[] <arrayName>;
or (旧式 C 风格,仍然有效):
<elementType> <arrayName>[];
计费示例:
int[] intArray; // canonical form: brackets on the type int intArray2[]; // legacy C-style form, discouraged in new code
2)构建数组
语法:
arrayName = new dataType[size];
计费示例:
intArray = new int[10]; // defines that intArray will store 10 integer values
声明和建设相结合:
int[] intArray = new int[10];
3)初始化数组
intArray[0] = 1; // assigns 1 to the first element (index 0) intArray[1] = 2; // assigns 2 to the second element (index 1)
一步完成声明和初始化 使用数组字面量:
int[] intArray = {1, 2, 3, 4}; // initialises an integer array of length 4 where the first element is 1, // the second element is 2, and so on.
第一个数组程序 Java
步骤1) 将以下代码复制到编辑器中:
class ArrayDemo { public static void main(String args[]) { int array[] = new int[7]; for (int count = 0; count < 7; count++) { array[count] = count + 1; } for (int count = 0; count < 7; count++) { System.out.println("array[" + count + "] = " + array[count]); } // System.out.println("Length of Array = " + array.length); // array[8] = 10; } }
步骤2) 保存、编译并运行代码。观察输出结果。
预期产量:
array[0] = 1 array[1] = 2 array[2] = 3 array[3] = 4 array[4] = 5 array[5] = 6 array[6] = 7
步骤3) If x 是一个数组引用, x.length 返回槽位数量。取消注释第一行并重新运行:
Length of Array = 7
步骤4) 与 C 不同, Java 每次数组访问都会进行边界检查。取消注释第二行并重新运行:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8 at ArrayDemo.main(ArrayDemo.java:11) Command exited with non-zero status 1
步骤5) JVM抛出异常 ArrayIndexOutOfBoundsException在 C 语言中,同样的访问操作会从相邻内存返回垃圾数据,而不是失败。
Java 数组:按引用传递
数组通过引用传递给方法。方法接收到的是引用的副本,因此方法内部对元素所做的更改对调用者是可见的。
例如:数组按引用传递。
步骤1) 将以下代码复制到编辑器中:
class ArrayDemo { public static void passByReference(String a[]) { a[0] = "Changed"; } public static void main(String args[]) { String[] b = {"Apple", "Mango", "Orange"}; System.out.println("Before Function Call " + b[0]); ArrayDemo.passByReference(b); System.out.println("After Function Call " + b[0]); } }
步骤2) 保存、编译并运行代码。观察输出结果。
预期产量:
Before Function Call Apple After Function Call Changed
多维数组 Java
多维数组是数组的数组。每增加一个维度,就添加一组方括号。要创建一个四行五列的二维整数网格:
int[][] twoD = new int[4][5];
您只需预先设置第一个(最左侧)维度的大小。其余维度可以单独分配,并且每一行都可以有自己的长度,从而使 Java 二维数组为真 锯齿状阵列.
计费示例:
public class Guru99 { public static void main(String[] args) { // Create a 2-dimensional array with 4 rows and 4 columns. int[][] twoD = new int[4][4]; // Assign three elements to it. twoD[0][0] = 1; twoD[1][1] = 2; twoD[3][2] = 3; System.out.print(twoD[0][0] + " "); } }
预期产量:
1
