- Home /
Insert a string to a list inside a list
I want to set the string of an index from a list that's inside a list
So I got a variable like this:
var everyRow = new List.< List.<String> >();
My attempt at accessesing the inner List is by:
everyRow.Insert(e, List.<String>());
everyRow[e].Insert(ee, currentLog[ee]);
That line gives me this error though:
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
So I'm guessing there's a better way to solve this. Help?
why do you have dots in betweem List and brackets (List.<)?
Answer by misher · Aug 29, 2017 at 01:15 PM
You nees to initialize every strings list that you want inside parent list. So you first initialize your List of strings lists first, then you must initilalize every inner list (probably in loop).
List<List<string>> parentList = new List<List<string>>();
parentList.Add(new List<string>());
now you can add alement inside string list
parentList[0].Add("some string element");
Answer by chiragjksol · Aug 29, 2017 at 10:13 AM
@vultt3rim You can insert your string like this:
List<List<string>> lists = new List<List<string>>();
lists[0].Add("Hello");
string test = lists[0][0]; // Should return "Hello".
Answer by valtteri_m · Aug 29, 2017 at 05:18 PM
Sorry guys! It seems as though the error came up just because I had
everyRow.Insert(e, List.<String>());
inside a for loop so it kept reseting the list or something
Using Insert() or Add() works.
Your answer
Follow this Question
Related Questions
Set Specific List Value 3 Answers
A node in a childnode? 1 Answer
ArgumentOutOfRangeException: Argument is out of range in Generic.List 2 Answers
JS Argument out of range 1 Answer
Problem with List Sorting (JavaScript) 0 Answers