C#如何把存放到arraylist中的数据读取出来

如题所述

通过索引器取出ArrayList中的数据。需要注意的是:通过索引器获取的数据类型是object,需通过强制转换得到相应的类型。

例如

// 创建ArrayList
ArrayList al = new ArrayList();
// 添加数据
al.Add(1);       //整数
al.Add(3.14f);   //单精度浮点
al.Add("Hello"); //字符串

// 使用索引器从ArrayList读取数据,然后进行强制转换
int x = (int)al[0];
int f = (float)al[1];
string s = (string)al[3];

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-06-19
foreach 遍历可以

也可以当成 Array 来用,通过下标来取,如 al[0]

取出后作类型转换即可
第2个回答  2011-06-19
using System;
using System.Collections;
public class SamplesArrayList {

public static void Main() {

// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add("Hello");
myAL.Add("World");
myAL.Add("!");

// Displays the properties and values of the ArrayList.
Console.WriteLine( "myAL" );
Console.WriteLine( " Count: {0}", myAL.Count );
Console.WriteLine( " Capacity: {0}", myAL.Capacity );
Console.Write( " Values:" );
PrintValues( myAL );
}

public static void PrintValues( IEnumerable myList ) {
foreach ( Object obj in myList )
Console.Write( " {0}", obj );
Console.WriteLine();
}

}本回答被网友采纳
第3个回答  2011-06-19
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("key", object);

存到request里 在页面request.getAttribute("key") 获取对象,比如List
也可以用strues2标签 EL表达式获取都行