今天寫了一串很長的程式,看起來不太好看。想要濃縮一下,腦袋裡就想到有沒有類似“陣列中的陣列"這種方式存在。以下就是有關這個想法的延伸。
首先,我想到是用List的方法,因為List是一種泛型,我們可以隨意指定想要的類型作成動態陣列。思考流程如下:
- 使用List來製作陣列
- List<T>中,T為我們要指定的類型,我們這裡想要用的是陣列,例如:int[]
- 提取數據時的表達方式:List變數名[List索引][List內陣列原索引],例如:somthing[1][6]
範例:
using UnityEngine; using System.Collections; using System.Collections.Generic; public class NewBehaviourScript : MonoBehaviour { public int[] aa = new int[3] { 0, 1, 50 }; public int[] bb = new int[3] { 45, 8, 26 }; public int[] cc = new int[3] { 5, 17, 115 }; public List<int[]> dd = new List<int[]>(); // Use this for initialization void Start() { dd.Add(aa); dd.Add(bb); dd.Add(cc); Debug.Log(dd[2][2]); } // Update is called once per frame void Update() { } }
輸出結果:
參考資料:
不規則陣列 (C# 程式設計手冊)
https://msdn.microsoft.com/zh-tw/library/2s05feca.aspx
超圖解說明多維陣列
http://www.flag.com.tw/book/cento-5105.asp?bokno=F2733&id=980
不規則二維陣列的建立與存取
https://learn.104.com.tw/cfdocs/edu/104reading/mfs_viewer.cfm?img=4&j=9789572186930&CFID=234038049&CFTOKEN=53793899