zoukankan      html  css  js  c++  java
  • Multidimensional Array And an Array of Arrays

    One is an array of arrays, and one is a 2d array. The former can be jagged, the latter is uniform.

    That is, a double[][] can validly be:

    double[][] x = new double[5][];
    
    x[0] = new double[10];
    x[1] = new double[5];
    x[2] = new double[3];
    x[3] = new double[100];
    x[4] = new double[1];

    Because each entry in the array is a reference to an array of double. With a jagged array, you can do an assignment to an array like you want in your second example:

    x[0] = new double[13];

    On the second item, because it is a uniform 2d array, you can't assign a 1d array to a row or column, because you must index both the row and column, which gets you down to a single double:

    double[,] ServicePoint = new double[10,9];
    
    ServicePoint[0]... // <-- meaningless, a 2d array can't use just one index.

    To clarify based on your question, the reason your #1 had a syntax error is because you had this:

    double[][] ServicePoint = new double[10][9];

    And you can't specify the second index at the time of construction. The key is that ServicePoint is not a 2d array, but an 1d array (of arrays) and thus since you are creating a 1d array (of arrays), you specify only one index:

    double[][] ServicePoint = new double[10][];

    Then, when you create each item in the array, each of those are also arrays, so then you can specify their dimensions (which can be different, hence the term jagged array):

    ServicePoint[0] = new double[13];
    ServicePoint[1] = new double[20];

    Quote From: Multidimensional Array [][] vs [,]

  • 相关阅读:
    C# 哈希表
    C# 连接SQL Server数据库的连接字符串<转>
    C# ADO.NET中的五个主要对象<转>
    C# integrated security=SSPI<转>
    C# ADO.NET访问SQL Server数据库<转>
    C# .NET 页面间传值的几种方法<转>
    C# Datatable删除行的Delete和Remove方法
    C# DataTable转json
    CSP2019 爆炸记
    停更通知
  • 原文地址:https://www.cnblogs.com/liao-hua/p/6280388.html
Copyright © 2011-2022 走看看