首页 > 编程语言 >什么是带有示例的 Java 中的交错数组?

什么是带有示例的 Java 中的交错数组?

时间:2024-09-25 11:56:21浏览次数:3  
标签:jaggedArray Java 示例 int students 数组 交错 row


Java 中的交错数组

交错数组,也称为数组数组,是一种数据结构,其中数组用于存储其他数组。交错数组的主要特征是主数组的每个元素可以具有不同的大小,从而允许在二维结构中使用可变的列长度。

为了理解 Jagged 数组的概念,让我们考虑一个例子。假设我们想要存储有关学生及其各自成绩的信息。我们可以创建一个交错数组来表示这个数据结构。这是它的外观:

什么是带有示例的 Java 中的交错数组?_数据结构

使用交错数组的好处是,当每个子数组中的元素数量不同时,它可以灵活地存储数据。这在列数可能不同的情况下特别有用,例如在处理不规则数据或稀疏矩阵时。

总之,交错数组提供了一种灵活的方法来表示和使用数据结构,其中每个维度的大小都可能不同,这使它们成为某些编程场景中的强大工具。

示例 1

在上面的代码中,我们首先声明一个 2D 交错数组 jagged Array 有三行。但是,我们此时不指定列长度。接下来,我们将不同大小的数组分配给交错数组的每一行。第一行有三个元素,第二行有两个元素,第三行有四个元素。

最后,我们使用嵌套循环来迭代并打印其元素。外部循环迭代行,内部循环迭代每行的列。jaggedarray

文件名:JaggedArrayExample.java

爪哇岛

1
public class JaggedArrayExample {
2


3
    public static void main(String[] args) {
4


5
        int[][] jaggedArray = new int[3][];
6


7
        
8


9
        // Assigning different-sized arrays to the jagged array
10


11
        jaggedArray[0] = new int[] { 1, 2, 3, 4 };
12


13
        jaggedArray[1] = new int[] { 5, 6, 7 };
14


15
        jaggedArray[2] = new int[] { 8, 9 };
16


17
        
18


19
        // Accessing and printing the elements of the jagged array
20


21
        for (int i = 0; i < jaggedArray.length; i++) {
22


23
            for (int j = 0; j < jaggedArray[i].length; j++) {
24


25
                 System.out.print(jaggedArray[i][j] + " ");
26


27
            }
28


29
            System.out.println();
30


31
        }
32


33
    }
34


35
}

输出

1 2 3 4 

5 6 7 

8 9

示例 2

在上面的代码中,我们声明了一个 2D 交错数组 jaggedArray,并用不同年级的学生名字对其进行初始化。第一行表示一年级学生的姓名。第二行表示二年级学生的姓名,依此类推。

然后,我们使用嵌套循环遍历交错数组并打印每个年级的学生姓名。外部循环遍历行 (grades),内部循环遍历每个年级的列 (students)。

文件名: JaggedArrayExample.java

爪哇岛

1
public class JaggedArrayExample {
2


3
     public static void main(String[] args) {
4


5
        // Declare and initialize a 2D jagged array to store names of students in different grades
6


7
        String[][] jaggedArray = {
8


9
            { "Ram", "Laxman" },                           // Grade 1 students
10


11
            { "Rahul", "Gauri", "Komal" },                // Grade 2 students
12


13
            { "Ajinkya", "Virat", "Tejaswi", "Sanju" }      // Grade 3 students
14


15
        };
16


17
        
18


19
        // Accessing and printing the elements of the jagged array
20


21
        for (int i = 0; i < jaggedArray.length; i++) {   // Iterate over the rows (grades)
22


23
            System.out.print("Grade " + (i + 1) + " students: ");
24


25
            for (int j = 0; j < jaggedArray[i].length; j++) {  // Iterate over the columns (students) of each grade
26


27
                 System.out.print(jaggedArray[i][j] + " ");    // Print the name of each student
28


29
            }
30


31
            System.out.println();   // Move to the next line after printing the names of students in a grade
32


33
        }
34


35
     }
36


37
}

输出

Grade 1 students: Ram Laxman 

Grade 2 students: Rahul Gauri Komal 

Grade 3 students: Ajinkya Virat Tejaswi Sanju

示例 3

在上面的代码中,我们有一个交错数组 jaggedArray,它在每行中存储不同的数字。第一行有 3 个元素,第二行有 2 个元素,第三行有 4 个元素,第四行有 1 个元素。然后

我们使用嵌套循环来迭代交错数组并计算每行的总和。外部循环迭代行,内部循环迭代每行的列。每行的总和是通过将该行中的所有元素相加来计算的。

文件名:JaggedArrayExample.java

爪哇岛

1
public class JaggedArrayExample {
2


3
     public static void main(String[] args) {
4


5
        int[][] jaggedArray = {
6


7
            { 1, 2, 3 },      // First row with three elements
8


9
            { 4, 5 },         // Second row with two elements
10


11
            { 6, 7, 8, 9 },   // Third row with four elements
12


13
            { 10 }            // Fourth row with one element
14


15
        };
16


17
        
18


19
        // Calculate the sum of each row and display the results
20


21
        for (int i = 0; i < jaggedArray.length; i++) {
22


23
            int rowSum = 0;
24


25
            for (int j = 0; j < jaggedArray[i].length; j++) {
26


27
                rowSum += jaggedArray[i][j];
28


29
            }
30


31
            System.out.println("Sum of row " + (i + 1) + ": " + rowSum);
32


33
        }
34


35
     }
36


37
}

输出

Sum of row 1: 6

Sum of row 2: 9

Sum of row 3: 30

Sum of row 4: 10

标签:jaggedArray,Java,示例,int,students,数组,交错,row
From: https://blog.51cto.com/u_16903194/12108114

相关文章