I have the following, which creates a list of arrays:

        static List<string[]> ReadFileIntoList(string fileName)
        {
            List<string[]> parsedData = new List<string[]>();
            string fullLine;
            string[] row;

            try
            {
                StreamReader sr = new StreamReader(new FileStream(fileName, FileMode.Open, FileAccess.Read));
                while ((fullLine = sr.ReadLine()) != null)
                {
                    row = fullLine.Split('\t');
                    parsedData.Add(row);
                }
                sr.Close();
            }
            catch (Exception e)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(e.Message);
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Yellow;
            }

            return parsedData;
        }

Once this list is created I need to add one additional element to the end of each array within the list and that one additional element will be coming from another list of floating point values; how can I do that?

Dani AI

Generated

Arrays in C# are fixed-size — a string[] cannot be expanded in place. is correct to suggest iterating, and correctly points out that using a List<string> (or List<List<string>>) makes appending trivial. ' example that calls parsedData.Add(...) would append a whole new row, not add an element to each existing row, so that one needs clarification.

Two practical approaches:

  • Replace each string[] with a new array one element larger (no signature change required).
  • Convert rows to List<string> (or keep List<List<string>>) and call .Add(...) for each row (simpler if modifying many rows).

Example: replace arrays in-place with an extended copy (handles mismatched counts safely; CultureInfo.InvariantCulture keeps float formatting consistent):

using System.Globalization;

int n = Math.Min(parsedData.Count, floatValues.Count);
for (int i = 0; i < n; i++)
{
    var old = parsedData[i];
    var extended = new string[old.Length + 1];
    Array.Copy(old, extended, old.Length);
    extended[old.Length] = floatValues[i].ToString("G", CultureInfo.InvariantCulture);
    parsedData[i] = extended;
}

Alternative: convert to lists, add, and (optionally) convert back:

using System.Linq;
using System.Globalization;

var rows = parsedData.Select(r => r.ToList()).ToList();
int n = Math.Min(rows.Count, floatValues.Count);
for (int i = 0; i < n; i++)
    rows[i].Add(floatValues[i].ToString("G", CultureInfo.InvariantCulture));

// if a List<string[]> is still required:
parsedData = rows.Select(r => r.ToArray()).ToList();

Notes: prefer keeping the extra value as a numeric type (double) if further arithmetic is needed rather than converting to string; verify the counts of parsedData and the float list and decide how to handle mismatches; for large files, parse directly into List<List<string>> to avoid repeated array allocations.

Recommended Answers

All 3 Replies

There are many ways to do this

foreach (String[] arr in parsedData) {
    // add to your string array here
}

// or you can

for (int i = 0; i < parsedData.Count; i++) {
   parsedData[i] ... // this is the string array, add to it
}

Without knowing more about what you mean by 'adding to the array' and what exactly you want to add to the array, there isn't much more I can show you.

try
{
StreamReader sr = new StreamReader(new FileStream(fileName, FileMode.Open, FileAccess.Read));
while ((fullLine = sr.ReadLine()) != null)
{
row = fullLine.Split('\t');
parsedData.Add(row);
}
sr.Close();
//define a string[] of your choice or the string that you want, ex string[] rowAdditional,
parsedData.Add(rowAdditiona);
}

I suppose this should be what you want, though am not sure, like momerath said, be more specific with what you want.

You could, of course, fake it out by making it a

List<List<string>>
// calling the .Add() method on each inner list

and return it as a

 List<string[]>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.