2016-07-11 145 views
0

我正在尝试创建一个排序系统,并试图将字符串形式的相当长的整数列表转换为int数组,以便更容易地对数组进行排序。初始字符串数组是通过从文本文件中读取整数列表形成的。在C中将字符串数组转换为Int数组#

这是代码当前的外观和我目前正在整理这一年的基础上:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 



namespace Climate_Sorting_Application 
{ 
    public class Program 
    { 
     public static string[] monthArray = File.ReadAllLines("Month.txt"); 
     public static string[] yearArrayPre = File.ReadAllLines("Year.txt"); 
     public static string[] afArray = File.ReadAllLines("WS1_AF.txt"); 
     public static string[] rainArray = File.ReadAllLines("WS1_Rain.txt"); 
     public static string[] sunArray = File.ReadAllLines("WS1_Sun.txt"); 
     public static string[] tmaxArray = File.ReadAllLines("WS1_TMax.txt"); 
     public static string[] tminArray = File.ReadAllLines("WS1_TMin.txt"); 
     public static string[] af2Array = File.ReadAllLines("WS2_Rain.txt"); 
     public static string[] rain2Array = File.ReadAllLines("WS2_Rain.txt"); 
     public static string[] sun2Array = File.ReadAllLines("WS2_Sun.txt"); 
     public static string[] tmax2Array = File.ReadAllLines("WS2_TMax.txt"); 
     public static string[] tmin2Array = File.ReadAllLines("WS2_TMin.txt"); 
     public static string arrayToAnalyse; 

     static void Main(string[] args) 
     { 
      Console.WriteLine("Please Specify the Data to be Analysed"); 
      Console.WriteLine("You must specify the Text File name (Do not include .txt"); 
      arrayToAnalyse = Console.ReadLine(); 



      Console.ReadLine(); 
     } 

     private static void sortProcess() 
     { 

     } 
    } 
} 

是否有容易转化为正确的数据类型什么办法?或甚至在初始文件读取期间将其转换为一个int值数组的方法?

回答

5

当然! LINQ真的可以让事情变得简单:

int[] someArray = File.ReadAllLines(filename).Select(int.Parse).ToArray(); 

这将运行每多数民众赞成通过int.Parse()方法读取线,然后将这些结果转换成数组。

相关问题