iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 21
0
Modern Web

零經驗ASP .NET Core 30 DAY全紀錄系列 第 21

零經驗 .NET Core 30 DAY----- Day21 string[]和ArrayList()介紹,一個刪除陣列中指定的元素方法

Hello everybody,這個系列就是學習開發的整個過程,學了些什麼就記錄下來,今天會來寫寫string[]和ArrayList()、Length()和Count(),其實前面就有用到,只是這幾天我終於比較清楚使用時機和方法,所以紀錄一下。


一、string Array

在宣告的時候就必須宣告大小,用於已知資料數量的時候。

  • 使用方法
//一維
	//方法一
    string[] str1 = new string[3];
    str1[0] = "a";
    str1[1] = "b";
    str1[2] = "c";
 	//方法二
    string[] str2 = { "a", "b", "c" };
//二维
	//方法一
    string [,] strarray1 = new string[3,2]{{a,aa },{b,bb},{c,cc}};
	//方法二
    string [,] strarray = new string [3, 2];
    strarray[0, 0] = “a”;
    strarray[0, 1] = “aa”;
    strarray[1, 0] = “b”;
    strarray[1, 1] = “bb”;
 	strarray[2, 0] = “c”;
    strarray[2, 1] = “cc”;

二、ArrayList

動態陣列,不必宣告大小,在不知道資料長度的時候非常好用,需要使用using System.Collections

  • 使用方法
ArrayList AL = new ArrayList();
    AL.Add("test");
    AL.Add("array");
    AL.Add("list");

三、List< T >

不固定長度的陣列,是特定type,因為ArrayList是用object儲存,所以會有type存為不同型別的問題,那就要轉型,那List是固定type如果出現不同type在編譯階段就不會過,較為安全。
* 使用方法

List<user> user = new List< user >();
user.Add(new user () {Name="AMY", Id=1234});
user.Add(new user() { Name = "BOB", Id = 1259 });
user.Add(new user() { Name = "TOMMY", Id = 1321 });

四、Length和Count

我在實作我的功能時一直遇到一個問題,我一直認為取的數組大小的方法是一樣的,但很可惜的是當我對array和arraylist使用Count和Length時發現,array必須用Length,不可使用Count,而我上網查了一下,原因是

Count() 不是數組的内容,而是 IEnumerable的内容,C# 2.0中數組不能用 Count(),而 3.0 后就可以用 Count()。

五、判斷一個array是否存在某個元素

string[] test  = {"1","2","3","4","5"};
bool exist = ((IList) test ).Contains("2");
if(exists)
// 存在
else
// 不存在

六、刪除某個array中的元素

string test1 ="hello,world,!";
string[] test2 = Split(“,”);
test2=test2.Where(val => val != “world”).ToArray();
//test2[0] + test1[1] 為 hello! 

下面為我的實作結果

我用上面判斷陣列元素是否存在,如果存在再去刪除那個元素,來做座位表member和admin的權限刪除。

SET TABLE頁面設計的功能

  1. 新增/刪除座位
  2. 更改座位表名稱/是否對外開放
  3. 設定管理者/成員權限

今天要做的是刪除權限!


原本的資料表。
https://ithelp.ithome.com.tw/upload/images/20200927/201300300HRD9gEV17.png
https://ithelp.ithome.com.tw/upload/images/20200927/20130030S6Ocmx2QuY.png
來刪除毛毛的權限。
https://ithelp.ithome.com.tw/upload/images/20200927/20130030ltY3iVdnQg.png
毛毛的id為3,刪除member會連同admin權限也一起被回收。
https://ithelp.ithome.com.tw/upload/images/20200927/20130030ejmIiJ2MKv.png
https://ithelp.ithome.com.tw/upload/images/20200927/20130030NJIZcWb7Fs.png
下面是只刪除毛毛admin權限的結果,刪除admin權限不會一同刪除member權限。
https://ithelp.ithome.com.tw/upload/images/20200927/20130030rOSwVjLI8n.png
https://ithelp.ithome.com.tw/upload/images/20200927/20130030icLNxIEcuJ.png
https://ithelp.ithome.com.tw/upload/images/20200927/20130030DCPCIkqs9k.png


DAY21心得:
辦公室的前輩非常喜歡閱讀一些心靈成長書籍,昨天有講到一句話我覺得非常的有意思,講述一個理論:每天都進步1%,一年後,你會進步37倍;每天都退步1%,一年後,你會弱化到趨近於0!
/images/emoticon/emoticon07.gif恭喜我已經持續成長20天。


上一篇
零經驗 .NET Core 30 DAY----- Day20 功能實作[座位管理系統-管理座位表3]
下一篇
零經驗 .NET Core 30 DAY----- Day22 LINQ MIN、MAX、SUM、AVREAGE
系列文
零經驗ASP .NET Core 30 DAY全紀錄30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
毛毛
iT邦新手 4 級 ‧ 2020-09-28 23:33:46

我的位子/images/emoticon/emoticon02.gif

soft_soft iT邦新手 5 級 ‧ 2020-09-29 01:00:34 檢舉

會幫你加回來啦XD

我要留言

立即登入留言