How can I convert a text file into a list of int arrays

后端 未结 5 2011
温柔的废话
温柔的废话 2021-01-16 19:02

I have a text file containing the following content:

0 0 1 0 3 
0 0 1 1 3 
0 0 1 2 3 
0 0 3 0 1 
0 0 0 1 2 1 1 
0 0 1 0 3 
0 0 1 1 3 
0 0 1 2 3 
0 0 3 0 1 
0         


        
5条回答
  •  梦毁少年i
    2021-01-16 19:35

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace FileToIntList
    {
    class Program
    {
        static void Main(string[] args)
        {
            // Read the file as one string.
            System.IO.StreamReader myFile =
               new System.IO.StreamReader("C:\\Users\\M.M.S.I\\Desktop\\test.txt");
            string myString = myFile.ReadToEnd();
    
            myFile.Close();
    
            // Display the file contents.
            //Console.WriteLine(myString);
            char rc = (char)10;
            String[] listLines = myString.Split(rc);
            List> listArrays = new List>();
            for (int i = 0; i < listLines.Length; i++)
            {
                List array = new List();
                String[] listInts = listLines[i].Split(' ');
                for(int j=0;j array in listArrays){
                foreach (int i in array)
                {
                    Console.Write(i + " ");
                }
                Console.WriteLine();
            }
            Console.ReadLine();
    
    
        }
    }
    }
    

提交回复
热议问题