kingBook

导航

C# 数组和 List 的使用

  • 数组
int[] arr1;
int[] arr = new int[5]; // 创建5个空元素的 int 数组
int[] arr2Lines = new int[5] {1, 2, 3, 4, 5};
int[] arr1Line = {1, 2, 3, 4, 5};

int[] TaxRates = new int[5]; 
for (int i=0; i<TaxRates.Length; i++) { 
    TaxRates[i] = 0; 
}

// 交错数组
int[][] integers = {
    new int[]{ 1, 2, 3 },
    new int[]{ 4, 5, 6 }
};
int[] integers0 = integers[0]; // { 1, 2, 3 }
int[] integers1 = integers[1]; // { 4, 5, 6 }

int[][] jaggedArray = new int[2][]; 
jaggedArray[0] = new int[4]; 
jaggedArray[1] = new int[3];

int[][] arr = new int[2][];
int[][][] integers3D = {
    new int[][]{
        new int[]{ 1, 2, 3 },
        new int[]{ 4, 5, 6 }
    }
};
int[] subIntegers3D = integers3D[0][0]; // { 1, 2, 3 }

// ================= 多维数组 ==========================================================
// 初始化,可以使用以下一种方法,在同一个语句中创建、设置并初始化多维数组: 
int[,] arr4 = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
int[,] arr5 = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
int[,] arr6 = { { 1, 2, 3 }, { 4, 5, 6 } };
int e4 = arr4[0, 0]; // 1
int e5 = arr5[0, 0]; // 1
int e6 = arr6[0, 0]; // 1

// 可以使用 C# 创建规则的多维数组,多维数组类似于同类型值的矩阵。虽然 Java 和 C# 都支持交错数组,但 C# 还支持多维数组(数组的数组)。 
// 使用以下语法声明多维矩形数组: 
int[,] arr2D; 
float[,,,] arr4D; 

arr2D = new int[5,4]; 
arr2D[4,3] = 906;
  • 注意: 在Unity中交错数组的迭代速度比多维数组快得多

image

数组元素排序:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestSort : MonoBehaviour {

    public class Man {
        public string name;
        public float level;
        public Man (string name,float level) {
            this.name = name;
            this.level = level;
        }
    }

    public class ManComparer : IComparer {
        int IComparer.Compare (object x, object y) {
            Man a = (Man)x;
            Man b = (Man)y;
            return (int)Mathf.Sign(a.level - b.level);
        }
    }
    
    private void PrintMen (Man[] men) {
        for (int i = 0; i < men.Length; i++) {
            Debug.Log($"name: {men[i].name}, level: {men[i].level}");
        }
    }
    
    private void Start () {
        ManComparer manComparer = new ManComparer();
        Man[] men = { new Man("b", 2.1f), new Man("c", 2.2f), new Man("a", 1.1f) };
        Debug.Log("排序前 =============");
        PrintMen(men);
        Array.Sort(men, manComparer);
        //ArraySort(men, (Man a, Man b) => (int)Mathf.Sign(a.level - b.level) ); // 使用lambda 表达式简化
        Debug.Log("排序后 =============");
        PrintMen(men);
        
        // 输出:
        /*
        排序前 =============
        name: b, level: 2.1
        name: c, level: 2.2
        name: a, level: 1.1
        排序后 =============
        name: a, level: 1.1
        name: b, level: 2.1
        name: c, level: 2.2
        */
    }

}

多维数组:
https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/arrays/multidimensional-arrays

交错数组:
https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/arrays/multidimensional-arrays

  • List
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestList : MonoBehaviour {

    private void Start () {
        // 定义一个长度为 5,存储 List<int> 的数组,注意 array2d 的类型为 Array 
        List<int>[] array2d = new List<int>[5];
        for (int i = 0; i < 5; i++) {
            array2d[i] = new List<int>();
        }
        //array2d[5] = new List<int>(); // IndexOutOfRangeException: Index was outside the bounds of the array.
        //Array.Fill(array2d, new List<int>()); // 注意不能使用此方法代替上面的 for 语句,此方法存储的是同一个引用
        
        List<List<int>> list2d = new List<List<int>>(); // 长度不固定
        for (int i = 0; i < 5; i++) {
            //list2d[i] = new List<int>(); // ArgumentOutOfRangeException: Index was out of range. 
            list2d.Add(new List<int>());
        }
        
    }

}

posted on 2021-08-06 10:47  kingBook  阅读(402)  评论(0编辑  收藏  举报