作者youneverknow (睡覺一直想)
看板C_Sharp
標題[問題] 在class裡宣告強制型別的arraylist屬性
時間Mon Jan 20 15:10:13 2014
小弟新手,
想要在自己寫的類別裡宣告一個ArrayList屬性,
並且把這個ArrayList的型別強制設定為我自己寫的另一個class,
code大致是這樣的:
http://ideone.com/A4nin5 //新手code,見笑了
Line15:ArrayList要怎麼被設定為product型別?
還請指教,謝謝!
==============
補充一下:stackoverflow上的建議都是改用List,
但List有先天很慢的問題,
有沒有非List的解法呢?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.67.63.123
※ 編輯: youneverknow 來自: 111.67.63.123 (01/20 15:19)
1F:推 qwer820404:List有泛型<T> .. 但是ArrayList只有object collection 01/20 15:23
2F:→ qwer820404:ArrayList無法規範型別 只能取出的時候轉型 01/20 15:24
3F:→ qwer820404:也許要判斷存在ArrayList裡面的物件類型 typeof 01/20 15:26
我的確是可以在使用ArrayList[i]的時候再強制轉型,
例如:((product)ArrayList[i]).xxx
但這樣寫感覺好蠢.....
※ 編輯: youneverknow 來自: 111.67.63.123 (01/20 15:29)
4F:→ iterator:怎麼會覺得 List<T> 有先天很慢的問題呢? 01/20 15:59
5F:推 a926:list很慢有文章參考嗎@@ 01/20 16:04
6F:→ ssccg:List先天很慢是哪來的? ArrayList基本上就是List<Object>吧 01/20 16:31
7F:→ ssccg:.net的List<T>底層一樣是array做的啊 01/20 16:34
8F:→ ssccg:看到你提到vector,是指C++的? 所以你以為List是LinkedList? 01/20 17:23
9F:→ ssccg:.net另外有個叫LinkedList的class,List就只是泛型ArrayList 01/20 17:25
10F:推 mepowerlmay:要比應該拿dictionary 跟 list比速度 01/20 20:05
11F:推 YahooTaiwan:List<T>哪裡惹到你 要這樣排擠它 01/20 20:53
小弟C#新手,的確以為List就是C++的List,
那個每次一定要從頭爬的速度怪物,
所以不是嗎?
※ 編輯: youneverknow 來自: 118.160.218.197 (01/20 23:25)