📜  C++和Java多维数组的区别

📅  最后修改于: 2021-09-13 02:31:10             🧑  作者: Mango

先决条件: C++ 中的多维数组, Java的多维数组

多维数组

多维数组是用于存储多个元素的数组的表格表示。这些维度可以是一维数组、二维数组等。多维数组在 C++ 和Java都可用,但它们的实现和一些属性是不同的。

在 C/C++ 中的实现

在 C++ 中,多维数组在内部创建为一个巨大的线性数组。 C++ 语法将此线性内存块抽象为 2 维或 3 维行为,使程序员更容易。

例子:

维度为 2 行 x 3 列 {{9, 45, 51}, {5, 25, 6}} 的二维数组实现如下(假设 Integer 需要 4 个字节):

因此,特定索引处的内部元素的内部公式如下:

假设基地址为3000 。然后arr[1][1] = 3000 + (1 * 3 * 4) + 1 * 4 = 3000 + 12 + 4 = 3016

由于这种实现,每行的列数必须相等,并且必须在声明时指定列大小才能正确访问元素。

下面是C++中多维数组的实现:

C++
// C++ program for multidimention array
// implementation
#include 
using namespace std;
  
// Driver Code
int main()
{
    // Create a 2d integer array,
    // dimensions: 3rows X 5cols
    int arr[3][5] = {
        { 23, 56, 34, 52, 63 },
        { 40, 20, 96, 43, 97 },
        { 75, 51, 10, 82, 43 }
    };
  
    // Traversing of 2D array
    cout << "Printing entire 2d array: "
         << endl;
  
    // Iterate over the rows
    for (int i = 0; i < 3; i++) {
  
        // Iterate over the cols
        for (int j = 0; j < 5; j++) {
  
            cout << "arr[" << i << "][" << j
                 << "]:" << arr[i][j]
                 << "      ";
        }
        cout << endl;
    }
  
    return 0;
}


Java
// Java program for multidimensional
// array implementation
import java.io.*;
  
class GFG {
  
    // Driver Code
    public static void main(String[] args)
    {
  
        // Create a 2D integer array
        // dimensions: 3rows X 5cols
        int[][] arr = {
            { 23, 56, 34, 52, 63 },
            { 40, 20, 96, 43, 97 },
            { 75, 51, 10, 82, 43 }
        };
  
        // Traversing the 2D array
        System.out.println("Printing entire 2d array: ");
  
        // Iterate over the rows
        for (int i = 0;
             i < arr.length; i++) {
  
            // Iterate over the cols
            for (int j = 0;
                 j < arr[i].length; j++) {
                System.out.print(
                    "arr[" + i + "][" + j
                    + "]:" + arr[i][j]
                    + "    ");
            }
            System.out.println();
        }
        System.out.println();
  
        // Reassigning arr[2] to another
        // array
  
        // This is not possible in 2D
        // arrays in C++, instead of
        // there is array of pointers
        arr[2] = new int[] { 82, 53, 64,
                             12, 45, 3 };
  
        // Traversing the array again
        System.out.println(
            "Printing entire 2d array "
            + "afer modification: ");
  
        // Iterate over the rows
        for (int i = 0;
             i < arr.length; i++) {
  
            // Iterate over the cols
            for (int j = 0;
                 j < arr[i].length; j++) {
  
                System.out.print(
                    "arr[" + i + "][" + j
                    + "]:" + arr[i][j]
                    + "    ");
            }
            System.out.println();
        }
    }
}


输出:
Printing entire 2d array: 
arr[0][0]:23      arr[0][1]:56      arr[0][2]:34      arr[0][3]:52      arr[0][4]:63      
arr[1][0]:40      arr[1][1]:20      arr[1][2]:96      arr[1][3]:43      arr[1][4]:97      
arr[2][0]:75      arr[2][1]:51      arr[2][2]:10      arr[2][3]:82      arr[2][4]:43

在Java的实现

在Java,多维数组被实现为一个数组数组,其中基本数组的每个索引都指向一个完全不同的数组。因此, arr[rowIndex]返回一个完整的一维数组,而arr[rowIndex][coLIndex]返回该一维数组中索引coLIndex处的元素。

例子:

下面是Java多维数组的实现:

Java

// Java program for multidimensional
// array implementation
import java.io.*;
  
class GFG {
  
    // Driver Code
    public static void main(String[] args)
    {
  
        // Create a 2D integer array
        // dimensions: 3rows X 5cols
        int[][] arr = {
            { 23, 56, 34, 52, 63 },
            { 40, 20, 96, 43, 97 },
            { 75, 51, 10, 82, 43 }
        };
  
        // Traversing the 2D array
        System.out.println("Printing entire 2d array: ");
  
        // Iterate over the rows
        for (int i = 0;
             i < arr.length; i++) {
  
            // Iterate over the cols
            for (int j = 0;
                 j < arr[i].length; j++) {
                System.out.print(
                    "arr[" + i + "][" + j
                    + "]:" + arr[i][j]
                    + "    ");
            }
            System.out.println();
        }
        System.out.println();
  
        // Reassigning arr[2] to another
        // array
  
        // This is not possible in 2D
        // arrays in C++, instead of
        // there is array of pointers
        arr[2] = new int[] { 82, 53, 64,
                             12, 45, 3 };
  
        // Traversing the array again
        System.out.println(
            "Printing entire 2d array "
            + "afer modification: ");
  
        // Iterate over the rows
        for (int i = 0;
             i < arr.length; i++) {
  
            // Iterate over the cols
            for (int j = 0;
                 j < arr[i].length; j++) {
  
                System.out.print(
                    "arr[" + i + "][" + j
                    + "]:" + arr[i][j]
                    + "    ");
            }
            System.out.println();
        }
    }
}
输出:
Printing entire 2d array: 
arr[0][0]:23    arr[0][1]:56    arr[0][2]:34    arr[0][3]:52    arr[0][4]:63    
arr[1][0]:40    arr[1][1]:20    arr[1][2]:96    arr[1][3]:43    arr[1][4]:97    
arr[2][0]:75    arr[2][1]:51    arr[2][2]:10    arr[2][3]:82    arr[2][4]:43    

Printing entire 2d array afer modification: 
arr[0][0]:23    arr[0][1]:56    arr[0][2]:34    arr[0][3]:52    arr[0][4]:63    
arr[1][0]:40    arr[1][1]:20    arr[1][2]:96    arr[1][3]:43    arr[1][4]:97    
arr[2][0]:82    arr[2][1]:53    arr[2][2]:64    arr[2][3]:12    arr[2][4]:45    arr[2][5]:3
想要从精选的视频和练习题中学习,请查看C++ 基础课程,从基础到高级 C++ 和C++ STL 课程,了解语言和 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程