将数组中的数字放入 arraylist
我有用户在 textBox3 中输入的数字,我将它们转换为数组 nums,现在我想将其中一半放入数组列表 A 中,一半放入数组列表 B 中,我该怎么做?谢谢
string[] source = textBox3.Text.Split(',');
int[] nums = new int[source.Length];
for (int i = 0; i < source.Length; i++)
{
nums[i] = Convert.ToInt32(source[i]);
}
ArrayList A = new ArrayList();
ArrayList B = new ArrayList();
编辑:
谢谢,我测试了你的答案,但是你所有代码的输出都是system.collection.generic[system.int32],有什么问题吗?谢谢,
例如我测试了ArsenMkrt写的:
private void button1_Click(object sender, EventArgs e)
{
string[] source = textBox3.Text.Split(',');
int[] nums = new int[source.Length];
List<int> A = nums.Take(source.Length/2).ToList();
List<int> B = nums.Skip(source.Length/2).ToList();
MessageBox.Show(B.ToString());
}
i have numbers that user enter in textBox3 and i converted them to an array nums now i want to put half of them in arraylist A and half of them in arraylist B how can i do that?thanks
string[] source = textBox3.Text.Split(',');
int[] nums = new int[source.Length];
for (int i = 0; i < source.Length; i++)
{
nums[i] = Convert.ToInt32(source[i]);
}
ArrayList A = new ArrayList();
ArrayList B = new ArrayList();
edited:
thanks,i tested your answers but output of all of your codes are system.collection.generic[system.int32],whats the problem?thanks
for example i tested this that ArsenMkrt wrote:
private void button1_Click(object sender, EventArgs e)
{
string[] source = textBox3.Text.Split(',');
int[] nums = new int[source.Length];
List<int> A = nums.Take(source.Length/2).ToList();
List<int> B = nums.Skip(source.Length/2).ToList();
MessageBox.Show(B.ToString());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于装箱问题,不建议使用数组列表,因此请使用列表:
第一个列表包含 length/2 到 length,第二个列表包含第一个项目到 length / 2
编辑: 请参阅 101 linq 示例 用于引入 linq。
编辑:显示列表中的项目应遍历列表,list.ToString()返回列表类型请参阅MSDN ToString 不是项目,因此您应该覆盖它并使用您的特定列表或执行以下操作:
或
或
It's not recommended using array list because of boxing issues so use lists:
first list contains length/2 to length and second list contains first item to length / 2
Edit: See 101 linq sample for interducing to linq.
Edit: for showing the items in list should traverse list, list.ToString() returns type of list See MSDN ToString not items, so you should override it and use your specific list or do:
Or
Or
这适用于所有 .NET。考虑使用通用
List
,您将避免装箱/拆箱和可能的InvalidCastException
。This will work in all .NETs. Consider using generic
List<int>
, you will avoid boxing/unboxing and possibleInvalidCastException
.或者要拥有对于值类型更快的通用列表,您可以编写
Or to have generic List which is more faster for value types, you can write
假设数组长度是偶数
assuming array length is even