2016-07-11 11 views
0

Ich versuche, ein Sortiersystem zu erstellen und ich versuche, eine ziemlich lange Liste von ganzen Zahlen in String-Form in ein int-Array zu verwandeln, um das Array leichter sortieren zu können. Das anfängliche String-Array wird gebildet, indem eine Liste von ganzen Zahlen aus einer Textdatei gelesen wird.Konvertieren eines String-Arrays in ein Int-Array in C#

Dies ist, wie der Code zur Zeit aussieht, und ich bin derzeit auf der Grundlage das Jahr der Sortierung:

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

     } 
    } 
} 

Gibt es eine Möglichkeit von leicht auf den richtigen Datentyp umzuwandeln? Oder sogar eine Möglichkeit, es während des ersten Lesens der Datei in ein int-Wert-Array umzuwandeln?

Antwort

5

Sicher! LINQ kann wirklich die Dinge einfach:

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

, dass jede Zeile ausgeführt wird, die in durch die int.Parse() Methode gelesen hat und dann die Ergebnisse in ein Array umwandeln.