C#把一个集合里面的元素循环,放到一个数组中,该怎么循环。。。。

 我来答
零到0的距离
推荐于2017-11-26 · TA获得超过253个赞
知道小有建树答主
回答量:370
采纳率:100%
帮助的人:276万
展开全部
//声明一个集合
List<string> list = new List<string>();
//声明一个数组
string[] Array = new string[] { };
//接受集合遍历出来的数据
string str = "";
//给集合添加数据
list.Add("张三1");
list.Add("李四2");
list.Add("王五3");
//如果集合中有数据开始遍历
if (list.Count > 0)
{
for (int i = 0; i < list.Count; i++)
{
str += list[i] + ",";//把每个数据之间用','分开
}
}
if (str.Length >= 2)
{
//从开始截取到最后一个,
str = str.Substring(0, str.LastIndexOf(','));
//把累积的数据分割成字符串 用数组接受
Array = str.Split(',');
}
//循环出数组的数据
for (int i = 0; i < Array.Length; i++)
{
//输出 Array[i]
}
xueshumeng0516
2013-09-17
知道答主
回答量:25
采纳率:0%
帮助的人:13.4万
展开全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace List
{
class Program
{
static void Main(string[] args)
{
asdf();

}
public static void asdf()
{
List<string> list = new List<string>();
string[] array = new string[10];
list.Add("张三");
list.Add("李四");
list.Add("王五");
list.Add("马六");
list.Add("赵七");

for (int i = 0; i < list.Count;i++ )
{
array[i] = list[i];

}

for (int i = 0; i < array.Length;i++ )
{
Console.WriteLine(array[i]);

}
}

}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
奇琪居士
2013-09-17 · TA获得超过307个赞
知道答主
回答量:192
采纳率:0%
帮助的人:126万
展开全部
假定数组是a[]
for(inti=0;i<a.length;i++){

console.writeline(a[i]);
}
还有一种专门对集合的遍历
foreach(int i in a)
{
console.writeline(i);
}
int i 为定义一个变量 也可以是 double i 等等 , in a 表示遍历数组a
希望对你有帮助
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
保雨珍pr
2013-09-17 · TA获得超过160个赞
知道小有建树答主
回答量:252
采纳率:0%
帮助的人:134万
展开全部
array[] t = new array[list.count()];
int i =0;
foreach(var item in list)
{
t[0]=item;

i++;

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-09-17
展开全部
用for each
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式