C# ArrayList如何动态添加对象及取值
答案:3 悬赏:0 手机版
解决时间 2021-01-23 08:02
- 提问者网友:风月客
- 2021-01-22 12:55
C# ArrayList如何动态添加对象及取值
最佳答案
- 五星知识达人网友:痴妹与他
- 2021-01-22 14:20
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();
}
}
全部回答
- 1楼网友:低音帝王
- 2021-01-22 16:37
先建一个类,类中有两个变量作为坐标值
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元素对应的坐标值传给其它子程序使用了.
- 2楼网友:掌灯师
- 2021-01-22 15:01
sb.add(name,courseid,value)
没有这个写法吧?arraylist类没有三个参数的add方法.
sb.add(new string[]{name,courseid,value})才对吧?
sb.add(new course (name,courseid,value))
是往arraylist里加入一个course 的对象
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯