Selecting elements from array at specific indices
-
How to extract from array items at specific inidces?
List ints;
int[] indices;
List slected = ints select items at 'indices';Чесноков
-
How to extract from array items at specific inidces?
List ints;
int[] indices;
List slected = ints select items at 'indices';Чесноков
I don't see a problem here.
List ints;
int[] indices;
List slected=new List();
ints=...;
indices=...;
foreach(int index in indices) slected.Add(ints[index]);:)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
I don't see a problem here.
List ints;
int[] indices;
List slected=new List();
ints=...;
indices=...;
foreach(int index in indices) slected.Add(ints[index]);:)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
I was thinking about 1 line of code as in LINQ
selected = ints.Select(i => i[ints]);
Чесноков
-
I was thinking about 1 line of code as in LINQ
selected = ints.Select(i => i[ints]);
Чесноков
Once you got it I'll be curious to see it. In the mean time I just use
foreach(int index in indices) slected.Add(ints[index]);
which is one line of code anyway. :)Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
How to extract from array items at specific inidces?
List ints;
int[] indices;
List slected = ints select items at 'indices';Чесноков
You can use the Where clause ie
private void indices()
{
List<int> listInts = new List<int>() { 1, -23, 45, -723, 29, 49, -90, 628, 476, 39, -10, 30 };
int[] indices = new int[] { 3, 5, 7, 9 };
var selected = listInts.Where((i, index) => !indices.Contains(index));
}That will do it.
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
-
You can use the Where clause ie
private void indices()
{
List<int> listInts = new List<int>() { 1, -23, 45, -723, 29, 49, -90, 628, 476, 39, -10, 30 };
int[] indices = new int[] { 3, 5, 7, 9 };
var selected = listInts.Where((i, index) => !indices.Contains(index));
}That will do it.
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
It may be compact and correct, but I don't like it much as it is an expensive, quadratic, operation: it will look for each element in the list of indices. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
It may be compact and correct, but I don't like it much as it is an expensive, quadratic, operation: it will look for each element in the list of indices. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
As I stated, it will work, whether it is efficient is a completely different story. I prefer your method to be honest, but I also like to keep my hand in with these new fangled things MS keeps bringing in. Some of them are actually plenty useful, sometimes, but like all these abstractions that are creeping into our job, supposedly to make our lives easier, you have to weigh up the various options.
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
-
How to extract from array items at specific inidces?
List ints;
int[] indices;
List slected = ints select items at 'indices';Чесноков
List<int> selected = (from i in indices select ints[i]).ToList();
-
As I stated, it will work, whether it is efficient is a completely different story. I prefer your method to be honest, but I also like to keep my hand in with these new fangled things MS keeps bringing in. Some of them are actually plenty useful, sometimes, but like all these abstractions that are creeping into our job, supposedly to make our lives easier, you have to weigh up the various options.
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
thanks for the hint, would you be able to solve assignement in one statement? there is array of ints and array of objects with public int MyValue property. the number of elements in both arrays are equal. How to assign those all ints to every object MyValue in one pass?
for (int i = 0; i < ints.Length; i++)
objects[i].MyValue = ints[i];Чесноков
-
List<int> selected = (from i in indices select ints[i]).ToList();
thanks, would you solve another one? http://www.codeproject.com/Messages/3895665/Re-Selecting-elements-from-array-at-specific-indic.aspx[^]
Чесноков
-
thanks for the hint, would you be able to solve assignement in one statement? there is array of ints and array of objects with public int MyValue property. the number of elements in both arrays are equal. How to assign those all ints to every object MyValue in one pass?
for (int i = 0; i < ints.Length; i++)
objects[i].MyValue = ints[i];Чесноков
Why on earth are you trying to solve this in one statement? The more you try to achieve that, the harder it's going to be for you to remember what your code actually does and how it works.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
thanks for the hint, would you be able to solve assignement in one statement? there is array of ints and array of objects with public int MyValue property. the number of elements in both arrays are equal. How to assign those all ints to every object MyValue in one pass?
for (int i = 0; i < ints.Length; i++)
objects[i].MyValue = ints[i];Чесноков
While I agree with Pete, here is how you would do that:
ints.Select((intValue, intIndex) => intIndex).ToList().ForEach((index) => objects[index].MyValue = ints[index]);
-
Why on earth are you trying to solve this in one statement? The more you try to achieve that, the harder it's going to be for you to remember what your code actually does and how it works.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
just to practice LINQ queries
Чесноков