如何将字符串数组转换为int数组而不使用循环。 [英] how to convert string array to int array without using loops.

查看:77
本文介绍了如何将字符串数组转换为int数组而不使用循环。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我创建了一个表单,其中有三个listBox和一个按钮。我动态地将整数值放在listBox1和ListBox2中(从用户到textBoxes)。

我想要,当我点击button1时,首先将两个listBox项连接在一个字符串数组中然后对它们进行排序,并将其添加到listBox3 ...(我希望它不使用循环)

我的代码是 - :

 使用系统; 
使用 System.Windows.Forms;

命名空间 JustForFun
{
public partial class ArrayStringToInt:Form
{
public ArrayStringToInt()
{
InitializeComponent();
}

private void button1_Click( object sender,EventArgs e)
{
string [] strArr = new string [listBox1.Items.Count + listBox2.Items.Count];
int [] intArr = new int [listBox1.Items.Count + listBox2.Items.Count];
listBox1.Items.CopyTo(strArr, 0 );
listBox2.Items.CopyTo(strArr,listBox1.Items.Count);
System.Array.Copy(strArr,intArr,strArr.Length); // 错误在这里
System.Array.Sort(intArr);
System.Array.Copy(intArr,strArr,intArr.Length);
listBox3.Items.AddRange(strArr);
}
}
}





当我运行此代码时会出错:

无法将源数组类型分配给目标数组类型。







我的问题是 - >是否可以这样做(不使用循环)??



任何建议都会有所体现...

解决方案

Array.ConvertAll(strArr,p => Convert.ToInt32(p));

希望这将起作用


No.

每个字符串必须单独转换为int - 如果列表框包含值1,2和3,那么你的字符串将是1,2和3,这些都是no整数。它们是整数的字符串表示。

要转换它们,您需要使用Parse或TryParse方法将每个元素转换为整数。

我会这样做这样:

 List< int> items =  new  List< int>(); 
foreach string s in listBox1.Items)
{
items.Add( int .Parse(s));
}
foreach string s in listBox2.Items)
{
items.Add( int .Parse(s));
}

如果我在此之后需要数据作为数组,我会转换它:

  int  [] array = items.ToArray(); 


您无法自动将 int 转换为字符串;两者完全不同类型 [ ^ ]。并且,通过扩展,您无法自动将 int 的数组转换为 string s的数组。

Hi,
I created a form in which, I have three listBoxes and one button. I put the integer value in listBox1 and ListBox2 dynamically(from user through textBoxes).
I want, when i click on button1 first Concatenate the two listBox items in one string array and then it sort that values, and added it to listBox3 ... (I want it without using loops)
My code is -:

using System;
using System.Windows.Forms;

namespace JustForFun
{
    public partial class ArrayStringToInt : Form
    {
        public ArrayStringToInt()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] strArr = new string[listBox1.Items.Count + listBox2.Items.Count];
            int[] intArr = new int[listBox1.Items.Count + listBox2.Items.Count];
            listBox1.Items.CopyTo(strArr, 0);
            listBox2.Items.CopyTo(strArr, listBox1.Items.Count);
            System.Array.Copy(strArr, intArr, strArr.Length);//Error is here
            System.Array.Sort(intArr);
            System.Array.Copy(intArr, strArr, intArr.Length);
            listBox3.Items.AddRange(strArr);
        }
    }
}



When i Run this code its give an error :

Source array type cannot be assigned to destination array type.




My question is -> is it possible to do this (without using loops) ??

Any suggestion will be appreciate...

解决方案

Array.ConvertAll(strArr ,p=>Convert.ToInt32(p));
Hope this will Work


No.
Each string has to be converted to an int individually - if your list box contains the values 1, 2, and 3, then your strings will be "1", "2" and "3", which are no integers. They are the string representation of integers instead.
To convert them, you need to convert each element to an integer, using the Parse or TryParse methods.
I would do it this way:

List<int> items = new List<int>();
foreach (string s in listBox1.Items)
    {
    items.Add(int.Parse(s));
    }
foreach (string s in listBox2.Items)
    {
    items.Add(int.Parse(s));
    }

If I needed the data as an array after this, I would convert it:

int[] array = items.ToArray();


You cannot automatically convert an int to a string; the two are totally different types[^]. And, by extension, you cannot automatically convert an array of ints to an array of strings.


这篇关于如何将字符串数组转换为int数组而不使用循环。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆