I have a hashtable and I have tried inputting all the values as either objects created from a class or as arrays. the arrays/objects are meant to be stand ins for cards and I want to use a random number generator to choose the keys of the hashtable and then to read out all the values associated to that key. Please can someone help?
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
namespace CardGame
{
internal class Program
{
static Hashtable monsters;
static void Main(string[] args)
{
monsters = new Hashtable();
int[] mon1 = new int[2] { 1, 2 };
int[] mon2 = new int[2] { 1, 2 };
int[] mon3 = new int[2] { 1, 2 };
int[] mon4 = new int[2] { 1, 2 };
int[] mon5 = new int[2] { 1, 2 };
int[] mon6 = new int[2] { 1, 2 };
int[] mon7 = new int[2] { 1, 2 };
int[] mon8 = new int[2] { 1, 2 };
int[] mon9 = new int[2] { 1, 2 };
int[] mon10 = new int[2] { 1, 4 };
int[] mon11 = new int[2] { 2, 4 };
int[] mon12 = new int[2] { 2, 4 };
int[] mon13 = new int[2] { 2, 4 };
int[] mon14 = new int[2] { 2, 4 };
int[] mon15 = new int[2] { 2, 4 };
int[] mon16 = new int[2] { 2, 4 };
int[] mon17 = new int[2] { 2, 4 };
int[] mon18 = new int[2] { 2, 4 };
int[] mon19 = new int[2] { 2, 4 };
int[] mon20 = new int[2] { 2, 4 };
int[] mon21 = new int[2] { 3, 6 };
int[] mon22 = new int[2] { 3, 6 };
int[] mon23 = new int[2] { 3, 6 };
int[] mon24 = new int[2] { 3, 6 };
int[] mon25 = new int[2] { 3, 6 };
int[] mon26 = new int[2] { 3, 6 };
int[] mon27 = new int[2] { 3, 6 };
int[] mon28 = new int[2] { 3, 6 };
int[] mon29 = new int[2] { 3, 6 };
int[] mon30 = new int[2] { 3, 6 };
int[] mon31 = new int[2] { 4, 8 };
int[] mon32 = new int[2] { 4, 8 };
int[] mon33 = new int[2] { 4, 8 };
int[] mon34 = new int[2] { 4, 8 };
int[] mon35 = new int[2] { 4, 8 };
int[] mon36 = new int[2] { 4, 8 };
int[] mon37 = new int[2] { 4, 8 };
int[] mon38 = new int[2] { 4, 8 };
int[] mon39 = new int[2] { 4, 8 };
int[] mon40 = new int[2] { 4, 8 };
int[] mon41 = new int[2] { 5, 10 };
int[] mon42 = new int[2] { 5, 10 };
int[] mon43 = new int[2] { 5, 10 };
int[] mon44 = new int[2] { 5, 10 };
int[] mon45 = new int[2] { 5, 10 };
int[] mon46 = new int[2] { 5, 10 };
int[] mon47 = new int[2] { 5, 10 };
int[] mon48 = new int[2] { 5, 10 };
int[] mon49 = new int[2] { 5, 10 };
int[] mon50 = new int[2] { 5, 10 };
monsters.Add(1, mon1);
monsters.Add(2, mon2);
monsters.Add(3, mon3);
monsters.Add(4, mon4);
monsters.Add(5, mon5);
monsters.Add(6, mon6);
monsters.Add(7, mon7);
monsters.Add(8, mon8);
monsters.Add(9, mon9);
monsters.Add(10, mon10);
monsters.Add(11, mon11);
monsters.Add(12, mon12);
monsters.Add(13, mon13);
monsters.Add(14, mon14);
monsters.Add(15, mon15);
monsters.Add(16, mon16);
monsters.Add(17, mon17);
monsters.Add(18, mon18);
monsters.Add(19, mon19);
monsters.Add(20, mon20);
monsters.Add(21, mon21);
monsters.Add(22, mon22);
monsters.Add(23, mon23);
monsters.Add(24, mon24);
monsters.Add(25, mon25);
monsters.Add(26, mon26);
monsters.Add(27, mon27);
monsters.Add(28, mon28);
monsters.Add(29, mon29);
monsters.Add(30, mon30);
monsters.Add(31, mon31);
monsters.Add(32, mon32);
monsters.Add(33, mon33);
monsters.Add(34, mon34);
monsters.Add(35, mon35);
monsters.Add(36, mon36);
monsters.Add(37, mon37);
monsters.Add(38, mon38);
monsters.Add(39, mon39);
monsters.Add(40, mon40);
monsters.Add(41, mon41);
monsters.Add(42, mon42);
monsters.Add(43, mon43);
monsters.Add(44, mon44);
monsters.Add(45, mon45);
monsters.Add(46, mon46);
monsters.Add(47, mon47);
monsters.Add(48, mon48);
monsters.Add(49, mon49);
monsters.Add(50, mon50);
Random rand = new Random();
var selection = Enumerable.Range(1, 50).OrderBy(x => rand.Next()).Take(10).ToList();
foreach (DictionaryEntry mons in monsters)
{
Console.WriteLine($"monster number {monsters.Keys}");
foreach (var item in mons)
{
Console.WriteLine(item);
}
}
I just cant seem to be able to access each value pairing from the hashtable.
P.S. there's a lot of commented out dead code at the bottom of things that I've tried.
The class Hashtable is not typed. The compiler doesn't know what types are in the table at runtime. So when you take things from the table, you have to specify the type you expect:
foreach (DictionaryEntry mons in monsters)
{
// Use mons.key, not monsters.key to access the number
// Its type is object. Outputting it works, but
// to actually work with it, you have to cast it back to int.
Console.WriteLine($"monster number {mons.Key}");
// To work with the int array inside the table, use
// a cast
foreach (var item in mons.Value as int[])
{
Console.WriteLine(item);
}
}
Instead of using outdated Hashtable, use a generic Dictionary<>, that supports specifying types at compile time:
internal class Program
{
static Dictionary<int, int[]> monsters = new Dictionary<int, int[]>();
static void Main(string[] args)
{
int[] mon1 = new int[2] { 1, 2 };
int[] mon2 = new int[2] { 1, 2 };
int[] mon3 = new int[2] { 1, 2 };
int[] mon4 = new int[2] { 1, 2 };
int[] mon5 = new int[2] { 1, 2 };
int[] mon6 = new int[2] { 1, 2 };
int[] mon7 = new int[2] { 1, 2 };
int[] mon8 = new int[2] { 1, 2 };
int[] mon9 = new int[2] { 1, 2 };
int[] mon10 = new int[2] { 1, 4 };
int[] mon11 = new int[2] { 2, 4 };
int[] mon12 = new int[2] { 2, 4 };
int[] mon13 = new int[2] { 2, 4 };
int[] mon14 = new int[2] { 2, 4 };
int[] mon15 = new int[2] { 2, 4 };
int[] mon16 = new int[2] { 2, 4 };
int[] mon17 = new int[2] { 2, 4 };
int[] mon18 = new int[2] { 2, 4 };
int[] mon19 = new int[2] { 2, 4 };
int[] mon20 = new int[2] { 2, 4 };
int[] mon21 = new int[2] { 3, 6 };
int[] mon22 = new int[2] { 3, 6 };
int[] mon23 = new int[2] { 3, 6 };
int[] mon24 = new int[2] { 3, 6 };
int[] mon25 = new int[2] { 3, 6 };
int[] mon26 = new int[2] { 3, 6 };
int[] mon27 = new int[2] { 3, 6 };
int[] mon28 = new int[2] { 3, 6 };
int[] mon29 = new int[2] { 3, 6 };
int[] mon30 = new int[2] { 3, 6 };
int[] mon31 = new int[2] { 4, 8 };
int[] mon32 = new int[2] { 4, 8 };
int[] mon33 = new int[2] { 4, 8 };
int[] mon34 = new int[2] { 4, 8 };
int[] mon35 = new int[2] { 4, 8 };
int[] mon36 = new int[2] { 4, 8 };
int[] mon37 = new int[2] { 4, 8 };
int[] mon38 = new int[2] { 4, 8 };
int[] mon39 = new int[2] { 4, 8 };
int[] mon40 = new int[2] { 4, 8 };
int[] mon41 = new int[2] { 5, 10 };
int[] mon42 = new int[2] { 5, 10 };
int[] mon43 = new int[2] { 5, 10 };
int[] mon44 = new int[2] { 5, 10 };
int[] mon45 = new int[2] { 5, 10 };
int[] mon46 = new int[2] { 5, 10 };
int[] mon47 = new int[2] { 5, 10 };
int[] mon48 = new int[2] { 5, 10 };
int[] mon49 = new int[2] { 5, 10 };
int[] mon50 = new int[2] { 5, 10 };
monsters.Add(1, mon1);
monsters.Add(2, mon2);
monsters.Add(3, mon3);
monsters.Add(4, mon4);
monsters.Add(5, mon5);
monsters.Add(6, mon6);
monsters.Add(7, mon7);
monsters.Add(8, mon8);
monsters.Add(9, mon9);
monsters.Add(10, mon10);
monsters.Add(11, mon11);
monsters.Add(12, mon12);
monsters.Add(13, mon13);
monsters.Add(14, mon14);
monsters.Add(15, mon15);
monsters.Add(16, mon16);
monsters.Add(17, mon17);
monsters.Add(18, mon18);
monsters.Add(19, mon19);
monsters.Add(20, mon20);
monsters.Add(21, mon21);
monsters.Add(22, mon22);
monsters.Add(23, mon23);
monsters.Add(24, mon24);
monsters.Add(25, mon25);
monsters.Add(26, mon26);
monsters.Add(27, mon27);
monsters.Add(28, mon28);
monsters.Add(29, mon29);
monsters.Add(30, mon30);
monsters.Add(31, mon31);
monsters.Add(32, mon32);
monsters.Add(33, mon33);
monsters.Add(34, mon34);
monsters.Add(35, mon35);
monsters.Add(36, mon36);
monsters.Add(37, mon37);
monsters.Add(38, mon38);
monsters.Add(39, mon39);
monsters.Add(40, mon40);
monsters.Add(41, mon41);
monsters.Add(42, mon42);
monsters.Add(43, mon43);
monsters.Add(44, mon44);
monsters.Add(45, mon45);
monsters.Add(46, mon46);
monsters.Add(47, mon47);
monsters.Add(48, mon48);
monsters.Add(49, mon49);
monsters.Add(50, mon50);
// mons is type KeyValuePair<int, int[]>
foreach (var mons in monsters)
{
Console.WriteLine($"monster number {mons.Key}");
foreach (var item in mons.Value)
{
Console.WriteLine(item);
}
}
}
}
How to get the random selection from the dictionary:
Random rand = new Random();
var selection = Enumerable.Range(1, 50).OrderBy(x => rand.Next()).Take(10).ToList();
foreach (var monId in selection)
{
Console.WriteLine($"monster number {monId}");
int[] monster = monsters[monId];
foreach (var item in monster)
{
Console.WriteLine(item);
}
}
Or a shorter form if you need the random monsters in a list
Random rand = new Random();
var selection = Enumerable.Range(1, 50).OrderBy(x => rand.Next()).Take(10).ToList();
var selectedMonsters = selection.Select(i => monsters[i]);
foreach (var monster in selectedMonsters)
{
Console.WriteLine("monster");
foreach (var item in monster)
{
Console.WriteLine(item);
}
}
Related
Given two Lists of integer arrays of the form:
genData = { {1,2,3,4}, {1,2,3,4}, {1,2,3,4}, {1,2,3,4}};
orgData = {{1,2,3,4}, {1,2,3,4}, {2,4,6,8}, {1,2,3,4}};
I'd like to determine if the sum of two subarrays at the same index in both lists don't match. If the sums match, do nothing. If the sums don't match, convert every integer in both subarrays into a 0.
For example, in the two lists above the subarrays at index 2 have a non-matching sum (10 vs 20). I'd like to convert the lists to
genData = { {1,2,3,4}, {1,2,3,4}, {0,0,0,0}, {1,2,3,4} };
orgData = { {1,2,3,4}, {1,2,3,4}, {0,0,0,0}, {1,2,3,4} };
I'm trying to first create a list if sums by trying
var genDataSum = genDataList.ForEach(x => x.Sum());
But obviously, that's throwing up errors..."Cannot assign void to an implicitly typed value".
Any help or guidance will be greatly appreciated.
You need to use select to get the sum. list.foreach works like normal for loop.
List<int[]> genData = new List<int[]>
{
new int[] { 1, 2, 3, 4 },
new int[] { 1, 2, 3, 4 },
new int[] { 1, 2, 3, 4 },
new int[] { 1, 2, 3, 4 }
};
List<int[]> orgData = new List<int[]>
{
new int[] { 1, 2, 3, 4 },
new int[] { 1, 2, 3, 4 },
new int[] { 2, 4, 6, 8 },
new int[] { 1, 2, 3, 4 }
};
var sumsGenData = genData.Select(a => a.Sum()).ToList();
var sumsOrgData = orgData.Select(a => a.Sum()).ToList();
for (int i = 0; i < sumsGenData.Count; i++)
{
if (sumsGenData[i] != sumsOrgData[i])
{
orgData[i] = new int[] { 0, 0, 0, 0 };
}
}
ForEach doesn't return anything. Use Select.
var orgData = { {1,2,3,4}, {1,2,3,4}, {0,0,0,0}, {1,2,3,4} };
var sums = orgData.Select( a => a.Sum() );
This question already has answers here:
Merge multiple Lists into one List with LINQ
(8 answers)
Closed 4 years ago.
I have 3 lists which their values are obtained dynamically
List<double> input = new List<double>();
List<double> time = new List<double>();
List<double> censor = new List<double>();
How can I convert/combine to double[][] as the example below in an efficient way?
double[][] example =
{
// input time censor
new double[] { 50, 1, 0 },
new double[] { 70, 2, 1 },
new double[] { 45, 3, 0 },
new double[] { 35, 5, 0 },
new double[] { 62, 7, 1 },
new double[] { 50, 11, 0 },
new double[] { 45, 4, 0 },
new double[] { 57, 6, 0 },
new double[] { 32, 8, 0 },
new double[] { 57, 9, 1 },
new double[] { 60, 10, 1 },
};
You could do this
List<double> input = new List<double>();
List<double> time = new List<double>();
List<double> censor = new List<double>();
var results = new double[input.Count][];
for (var i = 0; i < input.Count; i++)
results[i] = new []{ input[i] , time[i] , censor[i] };
Linq appraoch
double[][] example = input.Select((x, i) => new []{ x, time[i], censor[i]}).ToArray();
You could make use of Zip :
List<double> input = new List<double>() {1,2,3} ;
List<double> time = new List<double>() {10,20,30};
List<double> censor = new List<double>() {100,200,300};
var result = input.Zip(time, (i, t) => new double[] {i, t})
.Zip(censor, (x, c) => x.Append(c).ToArray())
.ToArray();
// result is :
// {{ 1, 10, 100},
// { 2, 20, 200},
// { 3, 30, 300}}
How to initialize this rectangular array of ragged arrays?
int[,][] =
{
// something
}
Same as Mohamed with different variables so you can see the results better :
int[,][] abc =
{
{new int[]{100,101,102}, new int[]{110,111,112}},
{new int[]{200,201,202}, new int[]{210,211,212}}
};
You have an 2 dimensional array of arrays. So you should initialize them like this:
int[,][] inputs = new int[,][]
{
{ new int[]{ 1, 2 }, new int[]{ 3, 4 }, new int[]{ 5, 6 }, new int[]{ 7, 8 } },
{ new int[]{ 1, 2 }, new int[]{ 3, 4 }, new int[]{ 5, 6 }, new int[]{ 7, 8 } }
};
This would be an example...
int[,][] x = new int[1, 1][];
x[0, 0] = new[] { 1, 2, 3 };
I think I found out.
int [,][] arr =
{
{
new int[] {1,3,5},
new int[] {4,6}
},
{
new int[] {1},
new int[] {0,0,0,0,0}
},
{
new int[] {6,6,6,6},
new int[] {1,2,3,4}
}
}
I have a list of int arrays.
How do I search for a specific int[]?
Example:
var listIntArray = new List<int[]>();
listIntArray.Add(new int[] { 1, 2, 3, 4, 5, 6 });
var array1 = new int[] { 1, 2, 3, 4, 5, 6 };
bool contains1 = listIntArray.Contains(array1);
//--> should be true
var array2 = new int[] { 4, 5, 6 };
bool contains2 = listIntArray.Contains(array2);
//--> should be false
You could do this with LINQ.
bool result = listIntArray.Any(arr => arr.SequenceEqual(array1));
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 } );