1

I have 1 array with many string lines.

For every line in that array, if a condition is met, I have a console app that displays the line from the array, with some modifications (I call this the outputLine).

How do I make an array from the list of outputLines ?

class Program
{
    static void Main(string[] args)
    {
        string[] linesArray = File.ReadAllLines(@"C:\Users\AK1\Desktop\CC2_B.TXT");
        int linecount = File.ReadAllLines(@"C:\Users\AK1\Desktop\CC2_B.TXT").Length;            

        for (int i = 0; i < linecount; i++)
        {
            if (linesArray[i].Contains("2008/12"))
            {
                string[] outputArray = linesArray;

                string outputLine = "yes    " + outputArray[i];
                Console.WriteLine(outputLine);

            }

        }

        Console.ReadLine();
    }
}
5
  • Can you add the tag with the language you're using, please? Commented Sep 6, 2016 at 13:39
  • 1
    Can't you create an array and keep adding the outputLine to it? Commented Sep 6, 2016 at 13:39
  • This sounds like a wise good idea. I assume i declare it outside the FOR loop, but how to I keep adding it to the new array ? Commented Sep 6, 2016 at 13:51
  • It actually leads to very poor performance, as you'd have to reallocate space for the array and copy every value over every time you enlarge it by adding a new line or allocate a massive array that might contain useless space (which might be better than a List is performance is critical and RAM is cheap/your file isn't too big) Commented Sep 6, 2016 at 13:54
  • Also @AjK string[] outputArray = linesArray; is a bit useless. While it won't create a copy of the array (thankfully), why bother creating a new reference to the same array, just use linesArray directly. Commented Sep 6, 2016 at 13:57

4 Answers 4

1

Rather than an array, which has to define its size at declaration in C#, you're looking for a List<string>. You might want to refer to this answer about adding elements to an array to better understand the difference between List and array

Add using System.Collections.Generic at the top of your file and modify your code as such:

class Program
{
    static void Main(string[] args)
    {
        string[] linesArray = File.ReadAllLines(@"C:\Users\AK1\Desktop\CC2_B.TXT");
        int linecount = File.ReadAllLines(@"C:\Users\AK1\Desktop\CC2_B.TXT").Length;            
        List<string> outputLines = new List<string>();

        for (int i = 0; i < linecount; i++)
        {
            if (linesArray[i].Contains("2008/12"))
            {
                string outputLine = "yes    " + linesArray[i];
                outputLines.Add(outputLine);
                Console.WriteLine(outputLine);
            }

        }
        Console.ReadLine();
    }
}

Then if you really need it to be an array (like to pass it to another function), you can call outputLines.ToArray()

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks - this is good. So this adds the "outputLine" to the "outputLines" list which is great. But if i change the output from "outputLine" to "outputLines" in the console app - i dont see the list in the console app
Not sure what you mean, could you add the new problematic code at the end of your question please? Are you asking how to output the entire List after your look rather than outputting it during the loop? If so you could put outputLines.ForEach(x => Console.WriteLine(x)) just before Console.ReadLine but it boils down to basically the same.
If your second question is "How do I output a list of strings to the console?" then you could refer to this very in depth answer: stackoverflow.com/a/949955/2830652.
Also if any of the answers here solve your initial question, could you please click the tick next to it to mark it as the accepted answer and click the upwards arrow next to any answer you find at least helpful?
Not sure what you did to mark the answers as useful but there seem to be no upvotes on any answers nor an accepted answer. Not going to push on this much further but since you're new to the website I just wanted to tell you what's expected.
|
1

why not first filter your lines with desired condition by using linq? then you can output in console.

string[] lines = File.ReadAllLines(@"C:\Users\AK1\Desktop\CC2_B.TXT");
string[] filteredLines = lines.Where(line => line.Contains("2008/12")).ToArray();
foreach(string line in filteredLines){
    Console.WriteLine("yes    " + line);
}

1 Comment

This solution is elegant in terms of written code but requires iterating through the full array once and then through the filtered array once again so if there are many results and performance is a concern it is preferable to go with a good ol' loop
0

Declare an array with a capacity of linecount, in case you need to add every single line. I suppose this is c++? so you can not increase an array size in runtime.

static void Main(string[] args)
{
    int outputCounter=0;
    .......
        if (linesArray[i].Contains("2008/12"))
        {
            string outputLine = "yes    " + outputArray[i];

             outputArray[outputCounter]= outputLine;
             outputCounter++;  
             Console.WriteLine(outputLine);   
        }

    }

Update: For c# just do something like this

static void Main(string[] args)
{
    List<string> outputList = new List<string>();
    .......
        if (linesArray[i].Contains("2008/12"))
        {
            string outputLine = "yes    " + outputArray[i];

             outputList.Add(outputLine);
             Console.WriteLine(outputLine);   
        }

    }

1 Comment

This is actually C# but your answer holds anyway.
0

Try working with Lists instead of arrays if you're wanting to make dynamic additions. For example;

class Program
{
    static void Main(string[] args)
    {
        string[] linesArray = File.ReadAllLines(@"C:\Users\AK1\Desktop\CC2_B.TXT");
        int linecount = File.ReadAllLines(@"C:\Users\AK1\Desktop\CC2_B.TXT").Length;
        // Delcare your list here
        List<string> outputList = new List<string>();            

        for (int i = 0; i < linecount; i++)
        {
            if (linesArray[i].Contains("2008/12"))
            {
                // You can "Add" to lists easily like this
                outputList.Add(linesArray[i]);

                // This will return you the last item in the list
                string outputLine = "yes    " + outputList.Last();
                Console.WriteLine(outputLine);
            }
        }

        // Then if you specifically want it back to an Array
        string[] outputArray = outputList.ToArray();

        Console.ReadLine();
    }
}

Hope this helps :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.