arraylist訪問元素
發布時間: 2023-01-07 14:35:08
A. 在c#中訪問ArrayList集合中的元素時,必須先進行強制類型轉換嗎
這個不一定,如果你只用到了元素的.ToString等object自帶的方法,那就不需要轉換。
如果需要訪問元素的實際成員,那就需要轉換成對應的類型。
B. Java中為什麼說ArrayList在進行隨機訪問和遍歷元素時,它能提供更好的性能
很明顯,ArrayList對象在底層以數組的形式存儲元素.
數組:便於查詢,不利於插入、刪除、修改,,
便於查詢..........至於為什麼便於查詢,主要是因為數組是一排有序的、並有下標關聯的元素的集合..
C. C#中ArrayList類定義的數組 如何訪問數組里的元素
1. 需強制轉換成 你想要的類型
2. 示例代碼如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Collections;
namespaceConsoleApplication2
{
classProgram
{
staticvoidMain(string[]args)
{
ArrayListlst=newArrayList(){2,3,4,5};
//訪問其元素值,強制轉換
for(inti=0;i<lst.Count;i++)
{
Console.WriteLine((int)lst[i]);
}
Console.Read();
}
}
}
3. 運行結果如下:
熱點內容