0

Premise: Array in bedded in List. User input to array within list, loop 3 strings within array, choose to loop again to create another array within loop. Print list and array with in. What happens/problem: when choosing to create another loop "array" , the second loop "array" overrides value of first loop"array". If first array was given value a,b,c and second loop/array has value d,s,g. When i print it the second array has overridden first array. Prints first array: d,s,g second array d,s,g.

How can i create new arrays within List as many times as the user wants?

List<string[]> Loggbok = new List<string[]>();
string[] input = new string[3] ;

bool tr = true;
while (tr)
{
    Console.WriteLine("Gör en ny Logg inlägg");
    for (int i = 0; i < 3; i++)
    {
        string Object = Console.ReadLine();
        input[i] = Object;
    }

    Loggbok.Add(input);

    Console.Write("Make a nother array press: j if not inputn : ");
    string val = Console.ReadLine();
    if (val == "j")
    {
        tr = true;
    }
    else 
    { 
        tr = false;
    }
}

Console.WriteLine(Loggbok[0][0]);  //print first array in list
Console.WriteLine(Loggbok[0][1]);
Console.WriteLine(Loggbok[0][2]);

Console.WriteLine(Loggbok[1][0]);   //print second array in list
Console.WriteLine(Loggbok[1][1]);
Console.WriteLine(Loggbok[1][2]);

Console.ReadLine();
4
  • 1
    I don't understand your question. Has the code anything to do with the arrays and the List? Can you point out more clearly what you want to achieve? Btw. I would advise against using Object as Name for a string because Object is a reserved Name for the class Object. That could cause many problems. Commented Jun 21, 2018 at 9:11
  • I wrote it a bit to complicated. but i just figured out the solution one minute after i posted the question. the array declaration should have been inside the for loop. Commented Jun 21, 2018 at 9:16
  • I need to create arrays inside of the list then. create as many arrays as the user wants. then print List with all the arrays inside of them. Commented Jun 21, 2018 at 9:18
  • The formatting was not so great so I did not see the List part. See one of the answers below. Commented Jun 21, 2018 at 9:22

2 Answers 2

1

Move the declaration of the string array into the while loop so it creates a new array. Right now you keep using the same array. Thus at every iteration in your while loop your new results overwrite your previous ones, which is why you end up with the values from your last iteration.

//string[] input = new string[3]; <- removed here
while (tr)
{
    Console.WriteLine("Gör en ny Logg inlägg");
    string[] input = new string[3]; //<- added here
    //....
}
Sign up to request clarification or add additional context in comments.

Comments

0

Put this line

string[] input = new string[3] ;

Into the while loop. It worked for me.

4 Comments

Thank you! for your help
You can appreciate the help in upvoting and mark as solution on one of the two answers.
dose any one know how to print the entire List and array?
'foreach (var array in Loggbok) { foreach (var arrayItem in array) { Console.WriteLine(arrayItem); } }'

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.