SoFunction
Updated on 2025-03-06

Analysis of the difference between two-dimensional arrays in C# and Java

This article describes the difference between two-dimensional arrays in C# and Java, and is shared with you for your reference. The specific analysis is as follows:

Using two-dimensional arrays in Java can be used as follows:

Copy the codeThe code is as follows:
public class Array2D{
    public static void main(String[] args){
        int myInt[][]=new int[5][10];
//Tranquility, assign values ​​to each array in the array
        for(int i=0;i<;i++){
            for(int j=0;j<myInt[0].length;j++){
                myInt[i][j]=i*j;
            }
        }
        ("="++",myInt[0].length="+myInt[0].length);
//The lower and upper limits of each dimension of the output array
        for(int i=0;i<;i++){
            for(int j=0;j<myInt[0].length;j++){
                ("myInt["+i+"]["+j+"]="+myInt[i][j]);
            }
        }
    }
}

Regarding the above code, I personally think that this can be done in C#, but in fact it is wrong. In C# int[][] myInt declares an interlaced array and declares a two-dimensional array to declare int[,] myInt. If the above code is replaced with C#, it needs to be expressed as follows:

Copy the codeThe code is as follows:
class clsArrat2D
{
        /// <summary>
/// The main entry point of the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            int[,] myInt=new int[5,10];
//Tranquility, assign values ​​to each array in the array
            for(int i=(0);i<=(0);i++)
            {
                for(int j=(1);j<=(1);j++)
                {
                    myInt[i,j]=i*j;
                }
            }
//The lower limit and upper limit of each dimension of the output array
            for(int i=0;i<;i++)
            {
                ("{0} {1} {2}", i, (i), (i));
            }
//Travel, output the number of each element in the two-dimensional array
            for(int i=(0);i<=(0);i++)
            {
                for(int j=(1);j<=(1);j++)
                {
                    ("myInt[{0},{1}]={2}",i,j,myInt[i,j]);
                }
            }
            ();
        }
}

 
In general, I feel that C# is doing a bad job. It is obviously just imitating C++ and Java, but people declare two-dimensional arrays in this way. Microsoft is unconventional here. If you accidentally fall, you don’t know why. You are not used to it when you first use it.

I hope this article will be helpful to beginners' C# programming learning.