Problem initializing an array because of a Parameter Index Error
up vote
1
down vote
favorite
This is from a series of computer science exercises I'm working through for which I'm stuck on the following compiler error when trying to split a list of integers into sublists. I've tried many variations of initializing the list of integer arrays but can't seem to find a configuration that works in the documentation or on stack overflow.
char array = digits.ToCharArray();
var intArray = new List<int>(new int[span]);
for (int i = 0; i < span; i++)
{
intArray[i] = (int)Char.GetNumericValue(array.ElementAt(i));
}
var data = new List<int>();
int n = 0;
while (data[n].Length == span)
{
data[n] = intArray.Skip(n).Take(span).ToArray();
n++;
}
Output:
Parameter name: index
at System.Collections.Generic.List`1.get_Item(Int32 index)
at LargestSeriesProduct.GetLargestProduct(String digits, Int32 span) in /Users/.../LargestSeriesProduct.cs:line 20
at seriesTest.Main() in /Users/.../LargestSeriesProduct.cs:line 53
Line 20 comes up in my IDE as while (data[n].Length == span)
.
Updated
I thought it might be a helpful exercise to write the code in Mathematica first to get more insight into the list of list problems I'm having in C#. It certainly was enlightening (the partition
function is super helpful in this situation) but I still don't have a solution for my initial problem.
largestSeriesProduct[string_, int_Integer] := Module[{numbers,lists},
numbers = ToExpression /@ Characters @ string;
lists = Times @@@ Partition[numbers,int,1];
Max @ lists
]
Which for example returns:
largestSeriesProduct["73167176531330624919225119674426574742355349194934",6]
23520
c#
|
show 1 more comment
up vote
1
down vote
favorite
This is from a series of computer science exercises I'm working through for which I'm stuck on the following compiler error when trying to split a list of integers into sublists. I've tried many variations of initializing the list of integer arrays but can't seem to find a configuration that works in the documentation or on stack overflow.
char array = digits.ToCharArray();
var intArray = new List<int>(new int[span]);
for (int i = 0; i < span; i++)
{
intArray[i] = (int)Char.GetNumericValue(array.ElementAt(i));
}
var data = new List<int>();
int n = 0;
while (data[n].Length == span)
{
data[n] = intArray.Skip(n).Take(span).ToArray();
n++;
}
Output:
Parameter name: index
at System.Collections.Generic.List`1.get_Item(Int32 index)
at LargestSeriesProduct.GetLargestProduct(String digits, Int32 span) in /Users/.../LargestSeriesProduct.cs:line 20
at seriesTest.Main() in /Users/.../LargestSeriesProduct.cs:line 53
Line 20 comes up in my IDE as while (data[n].Length == span)
.
Updated
I thought it might be a helpful exercise to write the code in Mathematica first to get more insight into the list of list problems I'm having in C#. It certainly was enlightening (the partition
function is super helpful in this situation) but I still don't have a solution for my initial problem.
largestSeriesProduct[string_, int_Integer] := Module[{numbers,lists},
numbers = ToExpression /@ Characters @ string;
lists = Times @@@ Partition[numbers,int,1];
Max @ lists
]
Which for example returns:
largestSeriesProduct["73167176531330624919225119674426574742355349194934",6]
23520
c#
this is not a compiler error
– Adrian
Nov 10 at 17:41
1
The type/name of the exception itself should give you quite a big hint... ;-)
– elgonzo
Nov 10 at 17:43
n is the counter starting at zero and then n++
– BBirdsell
Nov 10 at 17:43
data
is not an array, it is a List. It is an empry List with nothing in it because it was just created 2 lines earlier. use the AWESOME Step Debugger
– Disaffected 1070452
Nov 10 at 17:45
1
Possible duplicate of What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
– mjwills
Nov 12 at 5:23
|
show 1 more comment
up vote
1
down vote
favorite
up vote
1
down vote
favorite
This is from a series of computer science exercises I'm working through for which I'm stuck on the following compiler error when trying to split a list of integers into sublists. I've tried many variations of initializing the list of integer arrays but can't seem to find a configuration that works in the documentation or on stack overflow.
char array = digits.ToCharArray();
var intArray = new List<int>(new int[span]);
for (int i = 0; i < span; i++)
{
intArray[i] = (int)Char.GetNumericValue(array.ElementAt(i));
}
var data = new List<int>();
int n = 0;
while (data[n].Length == span)
{
data[n] = intArray.Skip(n).Take(span).ToArray();
n++;
}
Output:
Parameter name: index
at System.Collections.Generic.List`1.get_Item(Int32 index)
at LargestSeriesProduct.GetLargestProduct(String digits, Int32 span) in /Users/.../LargestSeriesProduct.cs:line 20
at seriesTest.Main() in /Users/.../LargestSeriesProduct.cs:line 53
Line 20 comes up in my IDE as while (data[n].Length == span)
.
Updated
I thought it might be a helpful exercise to write the code in Mathematica first to get more insight into the list of list problems I'm having in C#. It certainly was enlightening (the partition
function is super helpful in this situation) but I still don't have a solution for my initial problem.
largestSeriesProduct[string_, int_Integer] := Module[{numbers,lists},
numbers = ToExpression /@ Characters @ string;
lists = Times @@@ Partition[numbers,int,1];
Max @ lists
]
Which for example returns:
largestSeriesProduct["73167176531330624919225119674426574742355349194934",6]
23520
c#
This is from a series of computer science exercises I'm working through for which I'm stuck on the following compiler error when trying to split a list of integers into sublists. I've tried many variations of initializing the list of integer arrays but can't seem to find a configuration that works in the documentation or on stack overflow.
char array = digits.ToCharArray();
var intArray = new List<int>(new int[span]);
for (int i = 0; i < span; i++)
{
intArray[i] = (int)Char.GetNumericValue(array.ElementAt(i));
}
var data = new List<int>();
int n = 0;
while (data[n].Length == span)
{
data[n] = intArray.Skip(n).Take(span).ToArray();
n++;
}
Output:
Parameter name: index
at System.Collections.Generic.List`1.get_Item(Int32 index)
at LargestSeriesProduct.GetLargestProduct(String digits, Int32 span) in /Users/.../LargestSeriesProduct.cs:line 20
at seriesTest.Main() in /Users/.../LargestSeriesProduct.cs:line 53
Line 20 comes up in my IDE as while (data[n].Length == span)
.
Updated
I thought it might be a helpful exercise to write the code in Mathematica first to get more insight into the list of list problems I'm having in C#. It certainly was enlightening (the partition
function is super helpful in this situation) but I still don't have a solution for my initial problem.
largestSeriesProduct[string_, int_Integer] := Module[{numbers,lists},
numbers = ToExpression /@ Characters @ string;
lists = Times @@@ Partition[numbers,int,1];
Max @ lists
]
Which for example returns:
largestSeriesProduct["73167176531330624919225119674426574742355349194934",6]
23520
c#
c#
edited Nov 12 at 19:30
asked Nov 10 at 17:40
BBirdsell
1194
1194
this is not a compiler error
– Adrian
Nov 10 at 17:41
1
The type/name of the exception itself should give you quite a big hint... ;-)
– elgonzo
Nov 10 at 17:43
n is the counter starting at zero and then n++
– BBirdsell
Nov 10 at 17:43
data
is not an array, it is a List. It is an empry List with nothing in it because it was just created 2 lines earlier. use the AWESOME Step Debugger
– Disaffected 1070452
Nov 10 at 17:45
1
Possible duplicate of What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
– mjwills
Nov 12 at 5:23
|
show 1 more comment
this is not a compiler error
– Adrian
Nov 10 at 17:41
1
The type/name of the exception itself should give you quite a big hint... ;-)
– elgonzo
Nov 10 at 17:43
n is the counter starting at zero and then n++
– BBirdsell
Nov 10 at 17:43
data
is not an array, it is a List. It is an empry List with nothing in it because it was just created 2 lines earlier. use the AWESOME Step Debugger
– Disaffected 1070452
Nov 10 at 17:45
1
Possible duplicate of What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
– mjwills
Nov 12 at 5:23
this is not a compiler error
– Adrian
Nov 10 at 17:41
this is not a compiler error
– Adrian
Nov 10 at 17:41
1
1
The type/name of the exception itself should give you quite a big hint... ;-)
– elgonzo
Nov 10 at 17:43
The type/name of the exception itself should give you quite a big hint... ;-)
– elgonzo
Nov 10 at 17:43
n is the counter starting at zero and then n++
– BBirdsell
Nov 10 at 17:43
n is the counter starting at zero and then n++
– BBirdsell
Nov 10 at 17:43
data
is not an array, it is a List. It is an empry List with nothing in it because it was just created 2 lines earlier. use the AWESOME Step Debugger– Disaffected 1070452
Nov 10 at 17:45
data
is not an array, it is a List. It is an empry List with nothing in it because it was just created 2 lines earlier. use the AWESOME Step Debugger– Disaffected 1070452
Nov 10 at 17:45
1
1
Possible duplicate of What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
– mjwills
Nov 12 at 5:23
Possible duplicate of What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
– mjwills
Nov 12 at 5:23
|
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
0
down vote
to help you out:
var intArray = new List<int>(new int[span]);
for (int i = 0; i < span; i++)
{
intArray.Add((int)Char.GetNumericValue(array.ElementAt(i)));
}
That is indeed an improvement. I've updated the question.
– BBirdsell
Nov 11 at 20:05
Is there a better option thanElementAt
?
– mjwills
Nov 12 at 5:22
Yes, simply array[i]
– Aldert
Nov 12 at 7:19
add a comment |
up vote
0
down vote
My solution.
var intArray = new List<int>(new int[array.Length]);
for (int i = 0; i < array.Length; i++)
{
intArray[i] = (int)Char.GetNumericValue(array.ElementAt(i));
}
var data = new List<int>();
int n = 0;
while (intArray.Skip(n).Take(span).ToArray().Length == span)
{
data.Add(intArray.Skip(n).Take(span).ToArray());
n++;
}
var list = new List<long>(data.Count());
foreach (int nums in data)
{
list.Add((long)nums.Aggregate((total, next) => total * next));
}
long maxProduct = list.Max();
return maxProduct;
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
to help you out:
var intArray = new List<int>(new int[span]);
for (int i = 0; i < span; i++)
{
intArray.Add((int)Char.GetNumericValue(array.ElementAt(i)));
}
That is indeed an improvement. I've updated the question.
– BBirdsell
Nov 11 at 20:05
Is there a better option thanElementAt
?
– mjwills
Nov 12 at 5:22
Yes, simply array[i]
– Aldert
Nov 12 at 7:19
add a comment |
up vote
0
down vote
to help you out:
var intArray = new List<int>(new int[span]);
for (int i = 0; i < span; i++)
{
intArray.Add((int)Char.GetNumericValue(array.ElementAt(i)));
}
That is indeed an improvement. I've updated the question.
– BBirdsell
Nov 11 at 20:05
Is there a better option thanElementAt
?
– mjwills
Nov 12 at 5:22
Yes, simply array[i]
– Aldert
Nov 12 at 7:19
add a comment |
up vote
0
down vote
up vote
0
down vote
to help you out:
var intArray = new List<int>(new int[span]);
for (int i = 0; i < span; i++)
{
intArray.Add((int)Char.GetNumericValue(array.ElementAt(i)));
}
to help you out:
var intArray = new List<int>(new int[span]);
for (int i = 0; i < span; i++)
{
intArray.Add((int)Char.GetNumericValue(array.ElementAt(i)));
}
answered Nov 10 at 18:46
Aldert
747210
747210
That is indeed an improvement. I've updated the question.
– BBirdsell
Nov 11 at 20:05
Is there a better option thanElementAt
?
– mjwills
Nov 12 at 5:22
Yes, simply array[i]
– Aldert
Nov 12 at 7:19
add a comment |
That is indeed an improvement. I've updated the question.
– BBirdsell
Nov 11 at 20:05
Is there a better option thanElementAt
?
– mjwills
Nov 12 at 5:22
Yes, simply array[i]
– Aldert
Nov 12 at 7:19
That is indeed an improvement. I've updated the question.
– BBirdsell
Nov 11 at 20:05
That is indeed an improvement. I've updated the question.
– BBirdsell
Nov 11 at 20:05
Is there a better option than
ElementAt
?– mjwills
Nov 12 at 5:22
Is there a better option than
ElementAt
?– mjwills
Nov 12 at 5:22
Yes, simply array[i]
– Aldert
Nov 12 at 7:19
Yes, simply array[i]
– Aldert
Nov 12 at 7:19
add a comment |
up vote
0
down vote
My solution.
var intArray = new List<int>(new int[array.Length]);
for (int i = 0; i < array.Length; i++)
{
intArray[i] = (int)Char.GetNumericValue(array.ElementAt(i));
}
var data = new List<int>();
int n = 0;
while (intArray.Skip(n).Take(span).ToArray().Length == span)
{
data.Add(intArray.Skip(n).Take(span).ToArray());
n++;
}
var list = new List<long>(data.Count());
foreach (int nums in data)
{
list.Add((long)nums.Aggregate((total, next) => total * next));
}
long maxProduct = list.Max();
return maxProduct;
add a comment |
up vote
0
down vote
My solution.
var intArray = new List<int>(new int[array.Length]);
for (int i = 0; i < array.Length; i++)
{
intArray[i] = (int)Char.GetNumericValue(array.ElementAt(i));
}
var data = new List<int>();
int n = 0;
while (intArray.Skip(n).Take(span).ToArray().Length == span)
{
data.Add(intArray.Skip(n).Take(span).ToArray());
n++;
}
var list = new List<long>(data.Count());
foreach (int nums in data)
{
list.Add((long)nums.Aggregate((total, next) => total * next));
}
long maxProduct = list.Max();
return maxProduct;
add a comment |
up vote
0
down vote
up vote
0
down vote
My solution.
var intArray = new List<int>(new int[array.Length]);
for (int i = 0; i < array.Length; i++)
{
intArray[i] = (int)Char.GetNumericValue(array.ElementAt(i));
}
var data = new List<int>();
int n = 0;
while (intArray.Skip(n).Take(span).ToArray().Length == span)
{
data.Add(intArray.Skip(n).Take(span).ToArray());
n++;
}
var list = new List<long>(data.Count());
foreach (int nums in data)
{
list.Add((long)nums.Aggregate((total, next) => total * next));
}
long maxProduct = list.Max();
return maxProduct;
My solution.
var intArray = new List<int>(new int[array.Length]);
for (int i = 0; i < array.Length; i++)
{
intArray[i] = (int)Char.GetNumericValue(array.ElementAt(i));
}
var data = new List<int>();
int n = 0;
while (intArray.Skip(n).Take(span).ToArray().Length == span)
{
data.Add(intArray.Skip(n).Take(span).ToArray());
n++;
}
var list = new List<long>(data.Count());
foreach (int nums in data)
{
list.Add((long)nums.Aggregate((total, next) => total * next));
}
long maxProduct = list.Max();
return maxProduct;
answered Nov 12 at 19:29
BBirdsell
1194
1194
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241686%2fproblem-initializing-an-array-because-of-a-parameter-index-error%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
this is not a compiler error
– Adrian
Nov 10 at 17:41
1
The type/name of the exception itself should give you quite a big hint... ;-)
– elgonzo
Nov 10 at 17:43
n is the counter starting at zero and then n++
– BBirdsell
Nov 10 at 17:43
data
is not an array, it is a List. It is an empry List with nothing in it because it was just created 2 lines earlier. use the AWESOME Step Debugger– Disaffected 1070452
Nov 10 at 17:45
1
Possible duplicate of What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
– mjwills
Nov 12 at 5:23