
C# ArrayList如何动态添加对象及取值
2个回答
展开全部
先建一个类,类中有两个变量作为坐标值
public class remtbpoint
//remtbpoint
类(用来记忆某对象坐标){
private inprivate int rpointcol;
public int RPointline{
get{return rpointline;}
set { rpointline = value; }}
public int RPointcol{get{ return rpointcol; }
set { rpointcol = value; }}}private void rempoint(int line,int col)//记忆子程序,实例化后,赋值,加到arrayList中{r1 =new ;;;
r1.RPointLine=line;
r1.RPointcol=col;
rembtpointLinst.Add(r1);}
private void button1_Click(object sender,EventArgs e)//从记忆的arrayList中取出值,并动态删除最后一个arrayList元素{
int x=rembtpointLinst.Count;//直接取最末元素
if(x=1) {remtbpoint r3 =rembtpointLinst[x-1]asremtbpoint;//-------对于取数组列表中某元素太难,用这个方法极好用
//do...(r3.RPointLine,
r3.RPointcol);这里就可以把arrayList元素对应的坐标值传给其它子程序使用了.
展开全部
1 添加用add函数,
2 取值根据索引。用中括号例如 myAL[2],
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();
}
}
2 取值根据索引。用中括号例如 myAL[2],
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();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询