ãå¤åãã¨ãåç §åã
c#ã«éãããããã°ã©ã ãããã¨åºã¦ãããå¤åãã¨ãåç §åãã
ãããããé¨åã§ãããç¹ã«ãåç
§åãã¯ãã°ã®åå ã«ãªããã¨ãå¤ãã§ãã®ã§ãå®é¨çµæãå
¼ãã¦ã¡ã¢ã
è¨ãæããã°ããããããã¨
åãåå¦è ã®äººã«ãå¤åã¨åç §åã®éããã«ã¤ãã¦è³ªåãããæãä¸è¨ã®èª¬æããã¾ãã
- å¤å(ã®ä»£å ¥)ï¼ãã¡ã¤ã«ã®ãã³ãã¼ï¼ãã¼ã¹ãã
- åç §å(ã®ä»£å ¥)ï¼ãã¡ã¤ã«ã®ãã·ã§ã¼ãã«ããã®ä½æã
åè ã®å ´åãå ã®ãã¡ã¤ã«ã®å 容ããããå¤ãããã¨ãã³ããå ã®ãã¡ã¤ã«ã®å 容ã¯å¤ããã¾ããã
ã§ãå¾è ã®å ´åãä½æå ã®ãã¡ã¤ã«ã®å 容ãå¤ããã°ãã·ã§ã¼ãã«ãããããã«ã¯ãªãã¯ãã¦éãããã¡ã¤ã«ã®å 容ãå¤ãã£ã¦ãã¾ãã
ãªã®ã§ãä¸è¨ã®èª¬æãåããã¨ã
- å¤åï¼ãã¡ã¤ã«ã®ãå 容ã(=å¤)ãã³ãã¼ããã
- åç §åï¼ãã¡ã¤ã«ã®ããªã³ã¯å ã(=æ ¼ç´ã¢ãã¬ã¹)ãã³ãã¼ãã
ã¨èããã¨ãå¤å°ããããããããã
å®éã®ã³ã¼ãã§èª¬æ(ã¯ã©ã¹ã¨æ§é ä½)
ã¨ã¯ãããç¾èã¯ä¸è¦ã«å¦ããããªã®ã§ãå®é¨ç¨ã®ã³ã¼ããä¸è¨ã«è¨è¼ãã¾ããã ãªãããã§ã¯ããä¼¼ã¦éãªããã®ãã¨ãã¦ããä¾ã«åºãããã
- ã¯ã©ã¹(=åç §å)
- æ§é ä½(=å¤å)
ãç¨ãã¦èª¬æãã¾ãã
ã¡ãªã¿ã«ãã¯ã©ã¹ã¨æ§é ä½ã®éãã¨ãã¦ã¯ãä¸è¨ã®éãã
å¦ç | ã¯ã©ã¹ | æ§é ä½ |
---|---|---|
å | åç §å | å¤å |
ãã£ã¼ã«ã宣è¨ã¨åæã«åæå | ã | à |
å¼æ°ç¡ãã³ã³ã¹ãã©ã¯ã¿ã®ä½¿ç¨ | ã | à |
ãã¹ãã©ã¯ã¿ã®ä½¿ç¨ | ã | à |
å¦çæé | é ã | éã |
â»ãå¦çæéãã«é¢ãã¦ã¯ãã大éã®ãã¼ã¿ãå¦çããå ´åã®éãã®æ¯è¼ããªã®ã§ãå¥ã«ãã¯ã©ã¹ã¯ä¸æ¦ã«å¦çãé ããããã§ã¯ãªãã
åç
§ï¼ãC#ãæ§é ä½ã®ä½¿ãæ¹
public partial class Form1 : Form { struct PREF { public string pref1; public string pref2; public string pref3; public string pref4; } private void button1_Click(object sender, EventArgs e) { var prefs = new PREF(); var list1 = new List<PREF>(); var list2 = new List<List<PREF>>(); prefs.pref1 = "æç¥"; prefs.pref2 = "å²é"; prefs.pref3 = "ä¸é"; prefs.pref4 = "é岡"; list1.Add(prefs); list2.Add(list1); // 1 prefs.pref1 = String.Empty; prefs.pref2 = String.Empty; prefs.pref3 = String.Empty; prefs.pref4 = String.Empty; // 2 list1.Clear(); // 3 prefs.pref1 = "徳島"; prefs.pref2 = "é«ç¥"; prefs.pref3 = "æåª"; prefs.pref4 = "é¦å·"; list1.Add(prefs); // 4 list2.Add(list1); // 5 } }
ä¸è¨ã®ã³ã¼ãã®1ï½5ã®ç®æã§ãlist1ãã¨[list2]ã®pref1ï½pref4ã®å¤ããã§ãã¯ããã¨ãä¸è¨ã®éããããã¾ãã
1ï¼2
- list1
- [0]
- [pref1] = "æç¥"
- [pref2] = "å²é"
- [pref3] = "ä¸é"
- [pref4] = "é岡"
- [0]
- list2
- [0]
- [0]
- [pref1] = "æç¥"
- [pref2] = "å²é"
- [pref3] = "ä¸é"
- [pref4] = "é岡"
- [0]
- [0]
- list1
3
- list1
- è¦ç´ ç¡ã
- list2
- [0]
- è¦ç´ ç¡ã
- [0]
- list1
4
- list1
- [0]
- [pref1] = "徳島"
- [pref2] = "é«ç¥"
- [pref3] = "æåª"
- [pref4] = "é¦å·"
- [0]
- list2
- [0]
- [0]
- [pref1] = "徳島"
- [pref2] = "é«ç¥"
- [pref3] = "æåª"
- [pref4] = "é¦å·"
- [0]
- [0]
- list1
5
- list1
- [0]
- [pref1] = "徳島"
- [pref2] = "é«ç¥"
- [pref3] = "æåª"
- [pref4] = "é¦å·"
- [0]
- list2
- [0]
- [0]
- [pref1] = "徳島"
- [pref2] = "é«ç¥"
- [pref3] = "æåª"
- [pref4] = "é¦å·"
- [0]
- [1]
- [0]
- [pref1] = "徳島"
- [pref2] = "é«ç¥"
- [pref3] = "æåª"
- [pref4] = "é¦å·"
- [0]
- [0]
- list1
åä½ã®èª¬æ
1ï¼ç¹ã«åé¡ãªãã¨æãã¾ãã
2ï¼å ã®ãprefsãã®å ¨ãã£ã¼ã«ãã®å¤ããString.Emptyãã«ãã¦ãã¾ãããprefsã¯(å¤åã®)æ§é ä½ãªã®ã§ãå ã®prefsãå¤æ´ããã¦ããlist1ã®å 容ã¯å¤ããã¾ããã
3ï¼ãlist1.clear()ãããã¦ããã®ã§ãlist1ãã®è¦ç´ ã¯ã¯ãªã¢ããã¾ãããlist1ã¯(åç §åã®)ã¯ã©ã¹ãªã®ã§ã1ã®ç´åã«list1ã追å ãããlist2ããããlist1ãã®å¤æ´ã®å½±é¿ãåãã¾ãã (ãã ããlist1èªä½ãç¡ããªã£ãããã§ã¯ãªãã®ã§ãlist1ã®åç §èªä½ã¯æ®ã£ã¦ãã¾ãã(=list2.Countã®å¤ã¯1ã®ã¾ã¾))
4ï¼ãããä¸çªäºæ³ãã«ããç®æã ãlist1ãã«è¦ç´ ãå¤æ´ããã¦ããã®ã¯å½ç¶ã§ããããlist2[0]ãè¦ç´ ã¯ãlist1ã®åç §å ããªã®ã§ããlist1ãã®è¦ç´ ãå¤æ´ãããã°ãlist2[0]ã®å 容ãç´æ¥å½±é¿ãåãã¾ãã
5ï¼4ã§list2[0]ã®è¦ç´ ãå¤æ´ããã¾ããããlist2.add()ãå®è¡ãããã®ã§ãlist2[1]ã«æ°ãã«list1ã®å 容ã追å ããã¾ãã çµæãå ¨ãåããªã¹ã(=list1)ã2ã¤å ¥ã£ã¦ãããã¨ããçµæã«ãªãã¾ãã
ã¨ãåç §åã®ä»£å ¥ã¯ãæ³å®å¤ã®åä½ãããå ´åãããã®ã§ãæ°ãä»ããªãã¨ãæãã¬ãã°ã®åå ã«ãªã£ãããã¾ãã
åé¿ç
ç°¡åã«è¨ãã°ããã¯ã©ã¹ã®å¤ã®ã¿ã³ãã¼ãããªã¹ãã追å ãããå¦çãå®æ½ããã°OKã§ãã
ããæ¹ã§ãããã½ã¼ã¹ã®list2.add(list1)
ã®ç®æãã
list2.add(new List<List<PREF>>(list1))
ã®ããã«ã
- ã³ã³ã¹ãã©ã¯ã¿ã®å¼æ°ã«å¤ã³ãã¼å ã®ãªã¹ããè¨å®ãã¦æ°è¦ä½æãããListã¯ã©ã¹
ã追å ããã°ãå
ã®ãªã¹ã(list1)ã®å¤ãå¤æ´ããããã¨ãlist2ã®å
容ã¯å¤ããã¾ããã
ã¯ã©ã¹ã¨æ§é ä½ã®ä½¿ãåã
æå¾ã«ãªãã¾ããããã¾ã«åèæ¸ãä¸é¨ã®ããã°ã§ã
è¿·ã£ãããã¯ã©ã¹ã使ã£ã¦ããã°åé¡ãªã
ã¨ããè¨è¼ãè¦ããã¾ãããä½ãèããã«ã¯ã©ã¹ã使ãã¨ãä¸è¨ã®ãããªæãã¬ãã°ãå¼ãèµ·ããã®ã§ãè¦æ³¨æã§ãã
å人çã«ã¯ã
- ã¯ã©ã¹ï¼è²ã ãªæ©è½ãä»ãããæ¡å¼µæ§ãæããã
- æ§é ä½ï¼åã«ãå¤ã®åãç¿ãã¨ãã¦ä½¿ç¨ãã
ã¨ãã¦ä½¿ãåãããã®ãè¯ããã¨æãã¾ãã
以ä¸ãä»åã¯é·æã«ãªã£ã¦ãã¾ãã¾ããã