I have a simple code in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyProject
{
public static class Class1
{
public static int[] Iparray = new int { 12, 9, 4, 99, 120, 1, 3, 10 };
}
}
however on (Ctrl+Shift+B) the error displayed is
Cannot initialize type 'int' with a collection initializer because it does not implement 'System.Collections.IEnumerable'
I am using vs 2010 and
and .NET framework 4
Thank you All
You're missing brackets. Like this:
int[] a = new int[] { 1, 2, 3 };
You have three ways to define an int array:
public static int[] Iparray = { 12, 9, 4, 99, 120, 1, 3, 10 };
public static int[] Iparray = new[] { 12, 9, 4, 99, 120, 1, 3, 10 };
public static int[] Iparray = new int[] { 12, 9, 4, 99, 120, 1, 3, 10 };
new int[] { 12, 9, 4, 99, 120, 1, 3, 10 };
You just missed square brackets;
namespace MyProject
{
public static class Class1
{
public static int[] Iparray = new int[] { 12, 9, 4, 99, 120, 1, 3, 10 };
}
}
Arrays (C# Programming Guide)
Single-Dimensional Arrays (C# Programming Guide)
Alternative ways of declaring an int array;
int[] Iparray = { 12, 9, 4, 99, 120, 1, 3, 10 };
int[] Iparray = new[] { 12, 9, 4, 99, 120, 1, 3, 10 };
int[] values = new int[] { 1, 2, 3 };
or this:
int[] values = new int[3];
values[0] = 1;
values[1] = 2;
values[2] = 3;
and have a look on this http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx
Add [] to your code
public static int[] Iparray = new int[] { 12, 9, 4, 99, 120, 1, 3, 10 };
Try this:-
public static int[] a = new int[] {12, 9, 4, 99, 120, 1, 3, 10 };
instead of
public static int[] Iparray = new int { 12, 9, 4, 99, 120, 1, 3, 10 };
Related
I have two unit tests as follows:
[DataTestMethod]
[DataRow(1, 1, 2)]
[DataRow(2, 2, 4)]
[DataRow(3, 3, 6)]
public void AddTest1(int x, int y, int expected)
{
Assert.AreEqual(expected, x + y);
}
[DataTestMethod]
[DataRow(new int[] { 1, 2, 3, 4 }, new int[] { 1, 3, 6, 10 })]
[DataRow(new int[] { 1, 1, 1, 1, 1 }, new int[] { 1, 2, 3, 4, 5 })]
[DataRow(new int[] { 3, 1, 2, 10, 1 }, new int[] { 3, 4, 6, 16, 17 })]
public void AddTest2(int[] input, int[] expectedOutput)
{
Assert.AreEqual(input[0], expectedOutput[0]);
}
The first unit test runs 3 times, once for each data row.
The second unit test only runs once for the first DataRow.
How can I run the second UnitTests with the arrays once for each DataRow as well?
Most test frameworks require the test cases to use compile-time constants as parameters.
This works because the parameters are constant at compile time:
[DataTestMethod]
[DataRow(1, 1, 2)]
[DataRow(2, 2, 4)]
[DataRow(3, 3, 6)]
public void AddTest1(int x, int y, int expected)
{
Assert.AreEqual(expected, x + y);
}
This doesn't work because you're not using compile-time constants:
[DataTestMethod]
[DataRow(new int[] { 1, 2, 3, 4 }, new int[] { 1, 3, 6, 10 })]
[DataRow(new int[] { 1, 1, 1, 1, 1 }, new int[] { 1, 2, 3, 4, 5 })]
[DataRow(new int[] { 3, 1, 2, 10, 1 }, new int[] { 3, 4, 6, 16, 17 })]
public void AddTest2(int[] input, int[] expectedOutput)
{
Assert.AreEqual(input[0], expectedOutput[0]);
}
The new keyword with an array doesn't allow the test framework to differentiate (looking at the image you linked, it just says System.Int32[]).
A workaround might be this:
[DataTestMethod]
[DataRow(0, new int[] { 1, 2, 3, 4 }, new int[] { 1, 3, 6, 10 })]
[DataRow(1, new int[] { 1, 1, 1, 1, 1 }, new int[] { 1, 2, 3, 4, 5 })]
[DataRow(2, new int[] { 3, 1, 2, 10, 1 }, new int[] { 3, 4, 6, 16, 17 })]
public void AddTest2(int run, int[] input, int[] expectedOutput)
{
Assert.AreEqual(input[0], expectedOutput[0]);
}
That way, the different test cases can be differentiated by a compile-time constant.
This question already has answers here:
Does Distinct() method keep original ordering of sequence intact?
(7 answers)
Closed 2 years ago.
This is the example for the usage of Distinct from https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.distinct
List<int> ages = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 };
IEnumerable<int> distinctAges = ages.Distinct();
Console.WriteLine("Distinct ages:");
foreach (int age in distinctAges)
{
Console.WriteLine(age);
}
/*
This code produces the following output:
Distinct ages:
21
46
55
17
*/
The algorithm seems to be very simple: Iterate the elements. If the current element is unknown, copy it to the result, otherwise discard it.
I want to know if all implementations are required to give this result, or could a future implementation as well output 55, 21, 17, 46?
From your link:
Remarks
The result sequence is unordered.
and later:
The Distinct<TSource>(IEnumerable<TSource>) method returns an unordered sequence that contains no duplicate values
Seems like it always give them back in the order you put them. I tried and these are the results:
IEnumerable pippo = new List { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1, 4, 3, 5, 2, 1, 2, 3, 5, 4, 2, 3, 4, 5, 1, 3, 5, 6, 7 };
pippo.Distinct()
Distinct
Iterator { 1, 2, 3, 4, 5, 6, 7 }
pippo.Distinct()
Distinct
Iterator { 1, 2, 3, 4, 5, 6, 7 }
pippo.Distinct()
Distinct
Iterator { 1, 2, 3, 4, 5, 6, 7 }
IEnumerable pippo = new List { 99, 12, 33 ,1,2,3,4,5,6,7,8,11,33,44,1,2,3,4,5,6,7,99};
pippo.Distinct()
Distinct
Iterator { 99, 12, 33, 1, 2, 3, 4, 5, 6, 7, 8, 11, 44 }
pippo.Distinct()
Distinct
Iterator { 99, 12, 33, 1, 2, 3, 4, 5, 6, 7, 8, 11, 44 }
pippo.Distinct()
Distinct
Iterator { 99, 12, 33, 1, 2, 3, 4, 5, 6, 7, 8, 11, 44 }
Even in different executions and instances I got the same result.
I have been recently asked by a co-worker: Is it possible just take the first five elements and the last five elements by one query from an array?
int[] someArray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
What I've tried:
int[] someArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
var firstFiveResults = someArray.Take(5);
var lastFiveResults = someArray.Skip(someArray.Count() - 5).Take(5);
var result = firstFiveResults;
result = result.Concat(lastFiveResults);
Is it possible to just take the first five elements and the last five elements by one query?
You can use a .Where method with lambda that accepts the element index as its second parameter:
int[] someArray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
int[] newArray = someArray.Where((e, i) => i < 5 || i >= someArray.Length - 5).ToArray();
foreach (var item in newArray)
{
Console.WriteLine(item);
}
Output:
0, 1, 2, 3, 4, 14, 15, 16, 17, 18
A solution with ArraySegment<> (requires .NET 4.5 (2012) or later):
var result = new ArraySegment<int>(someArray, 0, 5)
.Concat(new ArraySegment<int>(someArray, someArray.Length - 5, 5));
And a solution with Enumerable.Range:
var result = Enumerable.Range(0, 5).Concat(Enumerable.Range(someArray.Length - 5, 5))
.Select(idx => someArray[idx]);
Both these solution avoid iterating through the "middle" of the array (indices 5 through 13).
In case you are not playing code puzzles with your co-workers, but just want to create a new array with your criteria, I wouldn't do this with queries at all, but use Array.copy.
There are three distinct cases to consider:
the source array has fewer than 5 items
the source array has 5 to 9 items
the source array has 10 or more items
The third one is the simple case, as the first and last 5 elements are distinct and well defined.
The other two require more thought. I'm going to assume you want the following, check those assumptions:
If the source array has fewer than 5 items, you will want to have an array of 2 * (array length) items, for example [1, 2, 3] becomes [1, 2, 3, 1, 2, 3]
If the source array has between 5 and 9 items, you will want to have an array of exactly 10 items, for example [1, 2, 3, 4, 5, 6] becomes [1, 2, 3, 4, 5, 2, 3, 4, 5, 6]
A demonstration program is
public static void Main()
{
Console.WriteLine(String.Join(", ", headandtail(new int[]{1, 2, 3})));
Console.WriteLine(String.Join(", ", headandtail(new int[]{1, 2, 3, 4, 5, 6})));
Console.WriteLine(String.Join(", ", headandtail(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11})));
}
private static T[] headandtail<T>(T[] src) {
int runlen = Math.Min(src.Length, 5);
T[] result = new T[2 * runlen];
Array.Copy(src, 0, result, 0, runlen);
Array.Copy(src, src.Length - runlen, result, result.Length - runlen, runlen);
return result;
}
which runs in O(1);
If you are playing code puzzles with your co-workers, well all the fun is in the puzzle, isn't it?
It's trivial though.
src.Take(5).Concat(src.Reverse().Take(5).Reverse()).ToArray();
this runs in O(n).
Try this:
var result = someArray.Where((a, i) => i < 5 || i >= someArray.Length - 5);
This should work
someArray.Take(5).Concat(someArray.Skip(someArray.Count() - 5)).Take(5);
Try this:
int[] someArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
var firstFiveResults = someArray.Take(5);
var lastFiveResults = someArray.Reverse().Take(5).Reverse();
var result = firstFiveResults;
result = result.Concat(lastFiveResults);
The second Reverse() reorders the numbers so you won't get 18,17,16,15,14
Please try this:
var result = someArray.Take(5).Union(someArray.Skip(someArray.Count() - 5).Take(5));
Is there a way to access my three dimensional jagged array like this:
jaggedArray[1,2,3];
I got the following code snippets so far:
int[][] jaggedArray = new int[3][]
{
new int[] { 2, 3, 4, 5, 6, 7, 8 },
new int[] { -4, -3, -2, -1, 0, 1},
new int[] { 6, 7, 8, 9, 10 }
};
int[,,] dontWork = new int[,,] // expect 7 everywhere in the last dimension
{
{ { 2, 3, 4, 5, 6, 7, 8 } },
{ { -4, -3, -2, -1, 0, 1} },
{ { 6, 7, 8, 9, 10 } }
};
As for the first question, you're trying to access the 3rd element, of the 2nd element of the 1st element of the jagged array:
jaggedArray[1][2][3]
As for the error, a 3D array expects the same number of elements in each element of a dimension. Let's say, for simplicity's sake, that you have a 2D jagged array, a rough representation of what that looks like in memory would be:
First row -> 2, 3, 4, 5, 6, 7, 8
Second row -> -4, -3, -2, -1, 0, 1
Third row -> 6, 7, 8, 9, 10
You can see that each row is seen as a different array, and can therefore differ in size. A multidimensional array, however, does not have this property. It needs to be filled completely:
Column : 0 1 2 3 4 5 6
------------------------------------
First row : 2, 3, 4, 5, 6, 7, 8
Second row: -4, -3, -2, -1, 0, 1
Third row : 6, 7, 8, 9, 10
Your table is missing some cells, which makes no sense. You need to use the same number of elements per dimension.
You got the syntax for declaring 2D jagged array right, 3D jagged arrays are an extension of that. For example:
int[][][] jagged3d = new int[][][]
{
new int[][] { new int[] { 111, 112 }, new int[] { 121, 122, 123 } },
new int[][] { new int[] { 211 } }
}
But to access it, you need different syntax:
jagged3d[0][1][2]
I have already looked at some array topics but I am still stumped.
I wish to add a new line to my jagged array - and am strugigng to get the syntax right..
int[][] intJaggedArray = new int[7][];
intJaggedArray[0] = new int[3] { 1, 1, 1 };
intJaggedArray[1] = new int[3] { 2, 2, 2 };
intJaggedArray[2] = new int[3] { 3, 3, 3 };
intJaggedArray[3] = new int[3] { 4, 4, 4 };
intJaggedArray[4] = new int[3] { 5, 5, 5 };
intJaggedArray[5] = new int[3] { 6, 6, 6 };
intJaggedArray[6] = new int[3] { 7, 7, 7 };
So now if i want to add
intJaggedArray[0] = new int[3] { 1, 1, 2 };
so the array ends up being as shown below how do I acheive it - thanks in advance - A noob from England. (And a big thanks in advance)
intJaggedArray[0] = new int[3] { 1, 1, 1 };
intJaggedArray[0] = new int[3] { 1, 1, 2 };
intJaggedArray[1] = new int[3] { 2, 2, 2 };
intJaggedArray[2] = new int[3] { 3, 3, 3 };
intJaggedArray[3] = new int[3] { 4, 4, 4 };
intJaggedArray[4] = new int[3] { 5, 5, 5 };
intJaggedArray[5] = new int[3] { 6, 6, 6 };
intJaggedArray[6] = new int[3] { 7, 7, 7 };
What do you want to do? Insert a line between 0 and 1? Or replace the existing line 0?
Your line :
intJaggedArray[0] = new int[3] { 1, 1, 2 };
simply replaces the existing line 0.
You can't insert a line in an array. To do so, use a list instead:
List<int[]> myList = new List<int[]>();
myList.Add(new int[] {...});
myList.Add(new int[] {...});
myList.Add(new int[] {...});
...
myList.Insert(1, new int[] {...});
Or if you want to replace the existing line, then simply:
You might want to create a collection or a List<int[]>
Then you could insert an item into it at a certain index.
List<int[]> x = new List<int[]>();
x.Insert(3, new int[3] { 1, 2, 3 });
If you want the initial list to be of a variable length, you cannot use an array. Use a List instead.
This should work:
List<int[]> intJaggedList = new List<int[]>();
intJaggedList.Add( new int[3] { 1, 1, 1 } );
intJAggedList.Add( new int[3] { 2, 2, 2 } );
...
Then to insert your new array:
intJaggedList.Insert( 1, new int[3] { 1, 1, 2 } );