【问题标题】:putting numbers in an array into arraylist将数组中的数字放入arraylist
【发布时间】:2010-12-28 08:42:30
【问题描述】:

我有用户在 textBox3 中输入的数字,我将它们转换为数组 nums 现在我想将其中的一半放在 arraylist A 中,将一半放在 arraylist 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());
        }

【问题讨论】:

  • 它应该是 system.collection.generic[system.int32] 因为我们创建通用列表,如果你想要 ArrayList,请尝试我的第一个版本的答案,或者使用 .ToArray() 期望 .ToList( ) 获取数组

标签: c# winforms arrays arraylist mergesort


【解决方案1】:

由于装箱问题,不建议使用数组列表,所以使用列表:

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));

第一个列表包含长度/2 到长度,第二个列表包含第一项到长度/2

编辑:请参阅 101 linq sample 以了解 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);

【讨论】:

    【解决方案2】:
    for (int i = 0; i < nums.Length; i++)
     {
     if(i < nums.Length / 2) A.Add(nums[i]);
             else B.Add(nums[i]);
     }
    

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

    【讨论】:

      【解决方案3】:
      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]));
      }
      

      【讨论】:

        【解决方案4】:
           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,您可以编写

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

        【讨论】:

          【解决方案5】:

          假设数组长度是偶数

           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]));
          
          
           }      
          

          【讨论】:

          • 不会导致超出范围异常吗?
          • 你应该这样做:for (int i = 0; i
          • 哦,是的..我打字很快..但更正了代码
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-29
          • 2018-06-11
          • 2011-07-02
          • 2016-09-08
          • 1970-01-01
          • 2015-06-25
          相关资源
          最近更新 更多