C#和Java中二维数组的区别

本文对比了Java和C#中二维数组的声明与使用方式,指出C#中使用int[,]而非int[][]来声明标准的二维数组,并提供了代码示例。

本人是从Java转向C#的,由于工作需要,感觉C#和Java的很多类似性,所以在使用C#的时候总喜欢套用Java的,因为他们太像了,然而今天却碰了一鼻子灰,原因就在使用二维数组上。

在Java中使用二维数组可以如下代码:

public class Array2D{
    
public static void main(String[] args){
        
int myInt[][]=new int[5][10];
        
//遍历,给数组中的每一个数组赋值
        for(int i=0;i<myInt.length;i++){
            
for(int j=0;j<myInt[0].length;j++){
                myInt[i][j]
=i*j;
            }

        }

        System.out.println (
"myInt.length="+myInt.length+",myInt[0].length="+myInt[0].length);
        
//输出数组每一维的下限和上限
        for(int i=0;i<myInt.length;i++){
            
for(int j=0;j<myInt[0].length;j++){
                System.out.println (
"myInt["+i+"]["+j+"]="+myInt[i][j]);
            }

        }

    }

}

 我满以为在C#中也可以这么做,事实上错了,在C#中int[][] myInt是声明一个交错数组,声明二维数组是这么声明int[,] myInt,上面的代码如果换成C#的,需要如下表示:

    class clsArrat2D
    
{
        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main(string[] args)
        
{
            
int[,] myInt=new int[5,10];
            
//遍历,给数组中的每一个数组赋值
            for(int i=myInt.GetLowerBound(0);i<=myInt.GetUpperBound(0);i++)
            
{
                
for(int j=myInt.GetLowerBound(1);j<=myInt.GetUpperBound(1);j++)
                
{
                    myInt[i,j]
=i*j;
                }

            }

            
//输出数组每一维的下限和上限
            for(int i=0;i<myInt.Rank;i++)
            
{
                Console.WriteLine(
"{0} {1} {2}", i, myInt.GetLowerBound(i), myInt.GetUpperBound(i));
            }

            
//遍历,输出二维数组中每一个元素的个数
            for(int i=myInt.GetLowerBound(0);i<=myInt.GetUpperBound(0);i++)
            
{
                
for(int j=myInt.GetLowerBound(1);j<=myInt.GetUpperBound(1);j++)
                
{
                    Console.WriteLine(
"myInt[{0},{1}]={2}",i,j,myInt[i,j]);
                }

            }

            Console.ReadLine();
        }

    }

 

总的感觉C#这点做得很烂,明明就是模仿C++和Java,而声明二维数组人家都是这么声明的,微软在这里却标新立异,一不小心栽跟头都不知道为什么,初用起来也很不习惯。

### C# 中二数组的序列化 在 C# 中,可以通过多种方式来序列化二维数组。最常用的方式之一是使用 `System.Runtime.Serialization.Formatters.Binary` 命名空间中的 `BinaryFormatter` 类来进行二进制序列化[^2]。 #### 使用 BinaryFormatter 进行序列化反序列化 下面是一个完整的例子,展示了如何将一个二整数数组序列化到文件并从中读取: ```csharp using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; public class Program { public static void Main() { // 创建一个二维数组 int[,] array = new int[3, 4]; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 4; ++j) { array[i, j] = i * 4 + j; } } string filePath = "array.dat"; // 序列化过程 using (FileStream fs = File.Create(filePath)) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(fs, array); } Console.WriteLine("Array has been serialized."); // 反序列化过程 int[,] deserializedArray; using (FileStream fs = File.OpenRead(filePath)) { BinaryFormatter formatter = new BinaryFormatter(); deserializedArray = (int[,])formatter.Deserialize(fs); } Console.WriteLine("Deserialized array:"); PrintMatrix(deserializedArray); // 打印矩阵辅助函数 void PrintMatrix(int[,] matrix) { for (int i = 0; i < matrix.GetLength(0); ++i) { for (int j = 0; j < matrix.GetLength(1); ++j) { Console.Write(matrix[i, j] + "\t"); } Console.WriteLine(); } } } } ``` 需要注意的是,在 .NET Core .NET 5+ 版本中,官方已经不再推荐使用 `BinaryFormatter` 来做对象的序列化操作,因为存在安全风险。替代方案可以考虑 JSON 或 XML 的形式进行序列化[^4]。 例如,如果想要采用 JSON 方式,则可借助于 Newtonsoft.Json NuGet 包: ```csharp using Newtonsoft.Json; string jsonSerialized = JsonConvert.SerializeObject(array); Console.WriteLine(jsonSerialized); // 将JSON字符串转换回原始类型的实例 var restoredArray = JsonConvert.DeserializeObject<int[,]>(jsonSerialized); PrintMatrix(restoredArray); ```
评论 6
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周公

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值