I am trying to create a sorting system and I am attempting to turn a fairly long list of integers in string form into an int array to more easily be able to sort the array. The initial string array is formed via reading a list of integers from a text file.
This is how the code currently looks and I am currently working on the basis of sorting the year:
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()
{
}
}
}
Is there any way of easily converting to the correct data type? Or even a way of converting it to an int value array during the initial file reading?
答案 0 :(得分:5)
Sure! LINQ can really make things simple:
int[] someArray = File.ReadAllLines(filename).Select(int.Parse).ToArray();
That will run every line that's read in through the int.Parse()
method and then convert those results to an array.
答案 1 :(得分:0)
ToCharArray? Char只是内存中的一个int。
或者你可以在A for循环中读取字符串并执行int.parse(index)。
https://msdn.microsoft.com/en-us/library/2c7h58e5(v=vs.110).aspx