开发者

putting numbers in an array into arraylist

开发者 https://www.devze.com 2023-02-01 23:41 出处:网络
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

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());
        }
开发者_运维知识库


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


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.


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


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


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


 }      
0

精彩评论

暂无评论...
验证码 换一张
取 消