Note that there are some explanatory texts on larger screens.

plurals
  1. POInsert an underlying value into a non-existing index
    text
    copied!<p>I'm trying to solve a simple algorithm a specific way where it takes the current row and adds it to the top most row. I know there are plenty of ways to solve this but currently I have a text file that gets read line by line. Each line is converted to an sbyte (there's a certain reason why I am using sbyte but it's irrelevant to my post and I won't mention it here) and added to a list. From there, the line is reversed and added to another list. Here's the code I have for that first part:</p> <pre><code> List&lt;List&lt;sbyte&gt;&gt; largeNumbers = new List&lt;List&lt;sbyte&gt;&gt;(); List&lt;string&gt; total = new List&lt;string&gt;(); string bigIntFile = @"C:\Users\Justin\Documents\BigNumbers.txt"; string result; StreamReader streamReader = new StreamReader(bigIntFile); while ((result = streamReader.ReadLine()) != null) { List&lt;sbyte&gt; largeNumber = new List&lt;sbyte&gt;(); for (int i = 0; i &lt; result.Length; i++) { sbyte singleConvertedDigit = Convert.ToSByte(result.Substring(i, 1)); largeNumber.Add(singleConvertedDigit); } largeNumber.Reverse(); largeNumbers.Add(largeNumber); } </code></pre> <p>From there, I want to use an empty list that stores strings which I will be using later for adding my numbers. However, I want to be able to add numbers to this new list named "total". The numbers I'll be adding to it are not all the same length and because so, I need to check if an index exists at a certain location, if it does I'll be adding the value I'm looking at to the number that resides in that index, if not, I need to create that index and set it's value to 0. In trying to do so, I keep getting an IndexOutOfRange exception (obviously because that index doesn't exist). :</p> <pre><code> foreach (var largeNumber in largeNumbers) { int totalIndex = 0; foreach (var digit in largeNumber) { if (total.Count == 0) { total[totalIndex] = digit.ToString(); //Index out of Range exception occurs here } else { total[totalIndex] = (Convert.ToSByte(total[totalIndex]) + digit).ToString(); } totalIndex ++; } } </code></pre> <p>I'm just at a loss. Any Ideas on how to check if that index exists; if it does not create it and set it's underlying value equal to 0? This is just a fun exercise for me but I am hitting a brick wall with this lovely index portion. I've tried to use SingleOrDefault as well as ElementAtOrDefault but they don't seem to be working so hot for me. Thanks in advance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload