将数组中的数字放入 arraylist

发布于 2024-10-09 09:40:22 字数 838 浏览 7 评论 0原文

我有用户在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

节枝 2024-10-16 09:40:22

由于装箱问题,不建议使用数组列表,因此请使用列表:

List<int> lst1 = new List<int>();
lst1.AddRange(nums.Skip(nums.Length/2));

List<int> lst2 = new List<int>();
lst2.AddRange(nums.Take(nums.Length / 2));

第一个列表包含 length/2 到 length,第二个列表包含第一个项目到 length / 2

编辑: 请参阅 101 linq 示例 用于引入 linq。

编辑:显示列表中的项目应遍历列表,list.ToString()返回列表类型请参阅MSDN ToString 不是项目,因此您应该覆盖它并使用您的特定列表或执行以下操作:

foreach (var i in lsss)
{
  MessageBox.Show(i.ToString());
}

lst1.ForEach(x=>MessageBox.Show(x.ToString()));

string strList = "";
lst1.ForEach(x => strList += x + " , ");
MessageBox.Show(strList);

It's not recommended using array list because of boxing issues so use lists:

List<int> lst1 = new List<int>();
lst1.AddRange(nums.Skip(nums.Length/2));

List<int> lst2 = new List<int>();
lst2.AddRange(nums.Take(nums.Length / 2));

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:

foreach (var i in lsss)
{
  MessageBox.Show(i.ToString());
}

Or

lst1.ForEach(x=>MessageBox.Show(x.ToString()));

Or

string strList = "";
lst1.ForEach(x => strList += x + " , ");
MessageBox.Show(strList);
寂寞花火° 2024-10-16 09:40:22
for (int i = 0; i < nums.Length; i++)
 {
 if(i < nums.Length / 2) A.Add(nums[i]);
         else B.Add(nums[i]);
 }

这适用于所有 .NET。考虑使用通用 List,您将避免装箱/拆箱和可能的 InvalidCastException

for (int i = 0; i < nums.Length; i++)
 {
 if(i < nums.Length / 2) A.Add(nums[i]);
         else B.Add(nums[i]);
 }

This will work in all .NETs. Consider using generic List<int>, you will avoid boxing/unboxing and possible InvalidCastException.

半透明的墙 2024-10-16 09:40:22
ArrayList A = new ArrayList();

ArrayList B = new ArrayList(); 

for (int i = 0; i < source.Length; i++) 
{
     if(i % 2 == 1)
          A.Add(Convert.ToInt32(source[i]));
     else
          B.Add(Convert.ToInt32(source[i]));
}
ArrayList A = new ArrayList();

ArrayList B = new ArrayList(); 

for (int i = 0; i < source.Length; i++) 
{
     if(i % 2 == 1)
          A.Add(Convert.ToInt32(source[i]));
     else
          B.Add(Convert.ToInt32(source[i]));
}
是你 2024-10-16 09:40:22
   string[] source = textBox3.Text.Split(',');
   var nums = source.Select(s=>Convert.ToInt32(s));

   ArrayList A = new ArrayList(nums.Take(source.Length/2));

   ArrayList B = new ArrayList(nums.Skip(source.Length/2));

或者要拥有对于值类型更快的通用列表,您可以编写

  List<int> A = nums.Take(source.Length/2).ToList();

  List<int> B = nums.Skip(source.Length/2)).ToList();
   string[] source = textBox3.Text.Split(',');
   var nums = source.Select(s=>Convert.ToInt32(s));

   ArrayList A = new ArrayList(nums.Take(source.Length/2));

   ArrayList B = new ArrayList(nums.Skip(source.Length/2));

Or to have generic List which is more faster for value types, you can write

  List<int> A = nums.Take(source.Length/2).ToList();

  List<int> B = nums.Skip(source.Length/2)).ToList();
卸妝后依然美 2024-10-16 09:40:22

假设数组长度是偶数

 ArrayList A = new ArrayList();  
 ArrayList B = new ArrayList();     
 int backword = source.Length / 2;
 int forward = 0;
 for (int i = 0; i < source.Length/2; i++) 
  {   

   A.Add(Convert.ToInt32(source[++forward]));
   A.Add(Convert.ToInt32(source[++backword]));


 }      

assuming array length is even

 ArrayList A = new ArrayList();  
 ArrayList B = new ArrayList();     
 int backword = source.Length / 2;
 int forward = 0;
 for (int i = 0; i < source.Length/2; i++) 
  {   

   A.Add(Convert.ToInt32(source[++forward]));
   A.Add(Convert.ToInt32(source[++backword]));


 }      
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文