- >)轉換為二維陣列
C# 中將列表列表(List>)轉換為二維陣列
C# 中將列表列表(List<List<T>>)轉換為二維陣列
資料來源: https://www.techiedelight.com/zh-tw/convert-a-list-of-lists-to-a-2d-array-in-csharp/
線上編譯器: https://dotnetfiddle.net/
網站Code
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<List<string>> lists = new List<List<string>>()
{
new List<string>() { "C", "C++" },
new List<string>() { "Java", "Kotlin" }
};
string[,] arrays = new string[lists.Count, lists[0].Count];
for (int i = 0; i < lists.Count; i++)
{
for (int j = 0; j < lists[i].Count; j++) {
arrays[i,j] = lists[i][j];
}
}
// 打印二維數組
for(int i=0;i<2;i++)
{
Console.WriteLine(arrays[i,0]+" , "+arrays[i,1]);
}
}
}
自己實際應用Code[動態UI按鈕]
public void SystrmMenuFunction(int intMenuId)//系統浮動菜單UI回應事件函數
{
//MessageBox.Show("SystrmMenuFunction " + intMenuId);
String[] StrAllName = new String[5] { "基本設定", "客顯畫面管理", "客顯資料管理", "VTEAM-KDS參數設定", "悠遊卡參數設定" };
int[,] intAllValue = new int[5, 2] { { 0, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 } };
List<String> ListName = new List<String>();
List<int> ListValue01 = new List<int>();//單列
List<int> ListValue02 = new List<int>();//單列
List<List<int>> ListValue = new List<List<int>>();//二維List
for(int i=0;i< StrAllName.Length;i++)
{
if ((POS_ECMAPI.m_EASY_CARDModule == null) && (i==4))//沒有悠遊卡參數
{
continue;//悠遊卡參數設定UI不用顯示
}
ListName.Add(StrAllName[i]);
ListValue01.Add(intAllValue[i, 0]);
ListValue02.Add(intAllValue[i, 1]);
}
ListValue.Add(ListValue01);
ListValue.Add(ListValue02);
DynamicSubMenuClear();//預防主菜單持續呼叫子菜單UI,但是又不執行子菜單功能的防呆
switch (intMenuId)
{
case 0://系統設定
String[] StrName = ListName.ToArray();//new String[5] { "基本設定", "客顯畫面管理", "客顯資料管理", "VTEAM-KDS參數設定","悠遊卡參數設定"};
int[,] intValue = new int[ListValue[0].Count, ListValue.Count];//new int[5, 2] { { 0, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 } };
for (int i = 0; i < ListValue[0].Count; i++)//二維List轉二維列
{
for (int j = 0; j < ListValue.Count; j++)
{
intValue[i, j] = ListValue[j][i];
}
}
Size SizeButton = new Size(270, 80);
bool blnImageMode = false;
bool blnVertical = true;
int intMaxRanks = StrName.Length;
m_DynamicSubMenuParm = new DynamicSubMenuParm(StrName, intValue, SizeButton, blnImageMode, blnVertical, intMaxRanks);
m_DynamicSubMenuUI = new DynamicSubMenuUI(m_DynamicSubMenuParm);
m_DynamicSubMenuUI.eventDynamicSubMenutrigger = new DynamicSubMenuUI.DynamicSubMenuEventHandler(SystrmSubMenu00); // 宣告承接event委派事件
m_DynamicSubMenuUI.StartPosition = FormStartPosition.Manual;
m_DynamicSubMenuUI.Location = new Point(170+5, 0+5);//main menu(170,0)
m_DynamicSubMenuUI.Owner = m_TranslucentLayeredUI;
m_DynamicSubMenuUI.Show();
break;
case 1://周邊設備
sysMenuClear();
DeviceManagement DeviceManagementBuf = new DeviceManagement();
DeviceManagementBuf.ShowDialog();
break;
case 2://雲報表
sysMenuClear();
break;
case 3://資料更新
sysMenuClear();
if((m_StrPosOrderNumber.Length>0)&&(m_intOrderState==0))//判斷購物車的訂單單號是否還未結帳
{//有未結帳訂單單號
Msg MsgBuf = new Msg("目前購物車尚未完成結帳,無法提供資料更新");
MsgBuf.ShowDialog();
}
else
{//無未結帳訂單單號
QuestionMsg QuestionMsgBuf = new QuestionMsg("確定執行資料更新?");
QuestionMsgBuf.ShowDialog();
if (QuestionMsgBuf.m_blnRun)
{
syncthreadStop();//停用執行序
mainInit(false);
ShowDailyQuestionMsg();
}
}
break;
case 4://登出
sysMenuClear();
ShowLoginUI();
break;
}
}