Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Redimensioning an array in c#
Message
De
05/11/2015 18:45:06
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
VB 9.0
OS:
Windows 10
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01627021
Message ID:
01627138
Vues:
49
>>>>Why not use a List instead?
>>>
>>>Because I need the fields of the records of the text file as an array. Can I Linq to a List Of String[]? Haven't tried it.
>>>
>>>Anyway, thanks for caring.
>>>
>>>
>>>>
>>>>
>>>>>
>>>>>        public static string[,] lines = new string[1,13];
>>>>>        static T[,] ResizeArray<T>(T[,] original, int rows, int cols)
>>>>>        {
>>>>>            var newArray = new T[rows, cols];
>>>>>            int minRows = Math.Min(rows, original.GetLength(0));
>>>>>            int minCols = Math.Min(cols, original.GetLength(1));
>>>>>            for (int i = 0; i < minRows; i++)
>>>>>                for (int j = 0; j < minCols; j++)
>>>>>                    newArray[i, j] = original[i, j];
>>>>>            return newArray;
>>>>>        }
>>>>>        public static void loadUploadedInvoices(string filename)
>>>>>        {
>>>>>            var work = File.ReadAllLines(filename).Select(a => a.Split("\t"[0])).ToArray();
>>>>>            int rows = work.Length-1;
>>>>>            string[,] work2 = new string[rows, 13];
>>>>>            for (int r=0; r<rows; r++)
>>>>>                for (int c= 0; c<13; c++)
>>>>>                {
>>>>>                    work2[r, c] = work[r][ c];
>>>>>                }
>>>>>            lines = AigFundedInvoices.ResizeArray<String>(work2, rows, 13);
>>>>>        }
>>>>>
>>>>>
>>>>>
>>>>>This kinda works, but I'm copying everything twice. Is there a way to do this by copying only once?
>>
>>Linq is designed to work with one dimensional IEnumerable structures. It probably won't work for you unless you can work with jagged arrays/lists. You may find some helpful suggestions at http://stackoverflow.com/questions/9774901/how-to-convert-list-of-arrays-into-a-multidimensional-array.
>>
>Right! Thanks. I wasn't sure what the difference was anymore between string[][] (a jagged array, a difficult denomination for a non English speaker) and string[,] a two dimensional array. I recall vaguely that in js and in php, there is no distinctions. It seems that what I want is a jagged array.
>
>
>>As far as copying everything twice, is there a reason that you need to call the ResizeArray function? Why not just set lines = work2?
>
>Right again. Silly me uh. I found the resize method first, but I couldn't get a two dimensional array out of the LInq statement. But you're absolutely right. I can set lines to work2.
>
>Many thanks.

A jagged array is an array of arrays. It probably is a bit closer to what you are doing since you have rows (the outer array) of fields (the array that goes into each row). The big difference is that the length of each of the inner arrays in a jagged array could be different whereas a two dimensional array forces a fixed size in both dimensions.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform