Pull values from two different array lists? - c#

I need help on how to make a for loop. Then using the i value as both arrayList index numbers.
My directions say
-Create a loop that uses either the length of the ArrayList or the element to dictate the number of times the loop will run.
-Each time the loop runs, pull another element of the ArrayList and output it in a
meaningful fashion to the console.
static void Main(string[] args)
{
nameArrayLists();
}
public static void nameArrayLists() {
ArrayList teamLists = new ArrayList() {"Cloud 9"};
teamLists.Add("Fnatic");
teamLists.Add("SKT T1");
teamLists.Add("Flash Wolves");
teamLists.Add("EDG");
ArrayList region = new ArrayList() { "North America" };
region.Add("Europe");
region.Add("Korea");
region.Add("Taiwan");
region.Add("China");
So after this, how do I make a for loop using i as both arrayList index numbers? The end result should be like "Fnatic is in the Europe region, cloud 9 is the in north america region" etc.

If i understand correctly, you are looking for something like this:
if (teamLists.Count == region.Count)
{
for (int i = 0; i < teamLists.Count; i++)
{
Console.WriteLine("{0} is in the {1} region", teamLists[i], region[i]);
}
}
else
{
Console.WriteLine("Items in the collections are not matching");
}
Keep in mind: Both the list are in the same order. which means for any i the value at corresponding index should be matching.
There is a best option for you:
Create a simple class with two properties(more if needed), then an overrided ToString() method which is for displaying the text as you described. and then use a List as like the following;
Class definition:
public class TeamNRegion
{
public string TeamName { get; set; }
public string RegionName { get; set; }
public int Id { get; set; }
public override string ToString()
{
return String.Format("{0} is in the {1} region", this.TeamName, this.RegionName);
}
}
Then you can create the list of this class objects like this:
List<TeamNRegion> teamRegionList = new List<TeamNRegion>()
{
new TeamNRegion(){Id=1,TeamName="Fnatic",RegionName="Europe"},
new TeamNRegion(){Id=10,TeamName="SKT T1",RegionName="Korea"},
new TeamNRegion(){Id=11,TeamName="Flash Wolves",RegionName="Taiwan"},
new TeamNRegion(){Id=12,TeamName="EDG",RegionName="China"},
};
// Print the result like this
foreach (TeamNRegion team in teamRegionList)
{
Console.WriteLine(team.ToString());
}

Related

How to create a Class List with different numbers of inputs in C#

I'm working on my first real c# project and I have faced a problem with my way of creating List based on a Class, which I have no idea how to solve.
I’m trying to write some code, which takes an input file (txt/csv) of multiple constructions with multiple layers, put it into my program, and later write the constructions into a new txt/csv file.
When having the same numbers of layers, it works fine. But when the constructions have different numbers of layers it causes trouble and I get a “System.IndexOutOfRangeException”.
My question is: Can I make the Class which I’m basing my List on, dynamic (I don’t know if it is the technical term), so it work with different numbers of inputs? Both when Adding the construction to the program and when I write it to a new file?
My code is:
class Program
{
static void Main(string[] args)
{
// Filepath for the input and output file
string filePathIn_constructions = #"C:\Library\Constructions.txt";
string filePathOut = #"C:\Library\EPlus_Inputfile.txt";
// Creating a list of constructions based on the class. The list is made from the file "filePathIn_constructions"
List<Construction> allConstructions = new List<Construction>();
List<string> lines_constructions = File.ReadAllLines(filePathIn_constructions).ToList(); // add it to a list
// Adding all the data from the fil to the variable "allConstructions"
foreach (var line in lines_constructions)
{
string[] entries = line.Split(',');
Construction newConstruction = new Construction();
newConstruction.EIndex = entries[0];
newConstruction.Name = entries[1];
newConstruction.Layer1 = entries[2];
newConstruction.Layer2 = entries[3];
newConstruction.Layer3 = entries[4];
newConstruction.Layer4 = entries[5];
newConstruction.Layer5 = entries[6];
allConstructions.Add(newConstruction); // Add it to our list of constructions
}
List<string> output = new List<string>();
foreach (var x in allConstructions) // Printing the new
{
output.Add($"{x.EIndex}, {x.Name}, {x.Layer1}, {x.Layer2}, {x.Layer3}, {x.Layer4}, {x.Layer5}");
}
File.WriteAllLines(txtFilePathOut, output);
}
}
My Class for the Constructions is
public class Construction
{
public string EIndex { get; set; }
public string Name { get; set; }
public string Layer1 { get; set; }
public string Layer2 { get; set; }
public string Layer3 { get; set; }
public string Layer4 { get; set; }
public string Layer5 { get; set; }
}
An example of a input/output file could be
Construction,ConcreteWall,Concrete;
Construction,Brickwall1,Birck,Isulation,Brick;
Construction,Brickwall2,Birck,AirGap,Isulation,Brick;
Construction,Wood/Concrete Wall,Wood,Isulation,Concrete,Gypson;
Construction,Wood Wall,Wood,AirGap,Gypson,Isulaiton,Gypson;
I hope someone can help. Thanks.
Edit: I have to be able to excess the construction Name seperatly, because i'm using it to do some sorting of the.
public class Construction
{
public string EIndex { get; set; }
public string Name { get; set; }
public List<string> Layers { get; set; } = new List<string>();
}
foreach (var line in lines_constructions)
{
string[] entries = line.Split(',');
Construction newConstruction = new Construction();
newConstruction.EIndex = entries[0];
newConstruction.Name = entries[1];
for (int i=2; i < entries.Length; i++) {
newConstruction.Layers.Add(entries[i]);
}
allConstructions.Add(newConstruction);
}
foreach(var x in allConstuctions) {
File.AppendAllText(output, $"{x.EIndex}, {x.Name}, {string.Join(", ", x.Layers)}");
}
It is because you are trying to reach a cell of an array that doesn't exist (documentation)
In your input/output file you have lines that have between 3 and 7 values, and you are building an array entries out of those values. This means that you will have arrays with between 3 and 7 cells
The problem is that right after creating those arrays you try to access on every array the cells 0, 1, 2... up to the 7th, even for arrays that have only 3 cells!
What you could do to fix this in a simple way is to add columns to have the same number of separator on each lines (you defined the separator of your lines as column with line.Split(',')). This way, every arrays that you will create will always have 7 cells, even if the value inside is null

How to use for() loop to do the value assignment operation in C#?

I have a DataModel class in my C# program like below.
public class DataModel
{
public string num1 { get; set; }
public string num2 { get; set; }
public string num3 { get; set; }
public string num4 { get; set; }
public string num5 { get; set; }
}
I need to do value assignment as below. The left side num1..num5 are TextBlock. The right side data.num1..data.num5 are initialized in another page already and will assign them to TextBlock's Text property. How to make it by using a for() loop? Thanks!
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.Parameter != null && e.Parameter is DataModel)
{
var data = e.Parameter as DataModel;
string str;
num1TextBlock.Text = data.num1;
num2TextBlock.Text = data.num2;
num3TextBlock.Text = data.num3;
num4TextBlock.Text = data.num4;
num5TextBlock.Text = data.num5;
Sorry for the unclear description. Updated, please check again. Thanks!
More:
If the array count is not fixed to 5 (5 is the minimal count), for example, we pull data from our server and after that we know how much data we need to do the assignment(initializing). How to do it?
Place your TextBlocks into an array.
var nums = new TextBlock[] {num1, num2, num3, num4, num5 };
Refactor your DataModel to also use an array instead of five fields, like so:
public class DataModel
{
public string[] nums {get; set;}
}
Then you can do:
for(int i = 0; i < data.nums.Length; i++)
{
nums[i].Text = data.nums[i];
}
Of course you should check if the number of data fields and the number of TextBlocks match up before running the loop, to avoid any IndexOutOfRange exceptions,
Something like below:
TextBlock[] blocks = new[]{num1, num2, num3, num4, num5};
int[] values = new[]{data.num1, data.num2, data.num3, data.num4, data.num5};
for (int i = 0; i<5;++i)
blocks[i].Text = values[i].ToString();
You can't really loop like that in C#. You can use an array, or even better, use a Dictionary. Dictionaries map really well into javascript objects, this:
var dataModel = new Dictionary<string, string>()
{
{ "num1", "string1" },
{ "num2", "string2" },
{ "num3", "string3" },
{ "num4", "string4" },
{ "num5", "string5" },
};
would easily map into this:
{
num1: "string1",
num2: "string2",
num3: "string3",
num4: "string4",
num5: "string5"
}
You can, however, use Reflection to get all properties of DataModel, iterate over them and match them by name with properties from another model and copy the values, but I wouldn't recommend it.

Cant print elements of arraylist or list

When i call ReadList function it dosent print the elements,what i am doing wrong?i tried even with normal list.What i want to do is if i create 2 accounts by CreateAccount function, i want to print them both through ReadList.
namespace Bank
{
class Program
{
static void Main()
{
int option;
do
{
Menu1 menu1 = new Menu1();
Account account = new Account();
option = menu1.CallMenu1();
if (option == 1)
{
account.CreateAccount();
}else if (option == 2) {
account.ReadList();
break;
}
else
{
break;
}
} while (true);
}
}
class Account
{
int AccountNumber { get; set; }
string Name { get; set; }
float Balance { get; set; }
public void CreateAccount()
{
int AccountNumberInput;
string NameInput;
float BalanceInput;
ArrayList list = new ArrayList();
Console.WriteLine("put id");
AccountNumberInput = int.Parse(Console.ReadLine());
Console.WriteLine("type name");
NameInput =Console.ReadLine();
Console.WriteLine("type balance");
BalanceInput = float.Parse(Console.ReadLine());
list.Add(AccountNumberInput);
list.Add(NameInput);
list.Add(BalanceInput);
}
public void ReadList()
{
ArrayList list = new ArrayList();
foreach (string c in list)
{
Console.WriteLine(c);
}
}
}
}
Your CreateAccount method does this:
Creates a new list
Populates the list
Does nothing with the list
Your ReadList method does this:
Creates a new (empty) list
Prints the contents of the list
The list created in CreateAccount() is never communicated outside the method - how do you expect the ReadList() method to know about it?
The simplest change would be to make CreateAccount() return the list it creates, then change ReadList to accept an ArrayList as a parameter. You'd change your Main method to store the result of CreateAccount in a local variable, then pass it to ReadList when it calls that.
However, beyond that:
I would avoid using ArrayList in any new code. It was superceded many, many years ago by List<T>.
Your CreateAccount method should surely do what it says: create an account. It should probably be a static method returning an Account... then your ReadList method could just be a PrintDetails instance method without any parameters, because you would call it on the instance returned by CreateAccount.
You should consider what you want to do if you someone tries to print the account before creating it...
Because your class is wrong. You are initialising your list inside one of your methods. Once your method completes execution, everything inside it is unloaded from the memory.
For this to work, you have to add a field/property to the class itself that contains the data and can be used by all the methods of the class
class Account
{
int AccountNumber { get; set; }
string Name { get; set; }
float Balance { get; set; }
//added this line
private ArrayList list {get;set;}
public void CreateAccount()
{
int AccountNumberInput;
string NameInput;
float BalanceInput;
//notice this
list = new ArrayList();
Console.WriteLine("put id");
AccountNumberInput = int.Parse(Console.ReadLine());
Console.WriteLine("type name");
NameInput =Console.ReadLine();
Console.WriteLine("type balance");
BalanceInput = float.Parse(Console.ReadLine());
list.Add(AccountNumberInput);
list.Add(NameInput);
list.Add(BalanceInput);
}
public void ReadList()
{
//removed ArrayList list = new ArrayList();
foreach (string c in list)
{
Console.WriteLine(c);
}
}
}

c# array looping with itself

Why I'm doing this:
So I'm trying to make an application for a game called clash royale, after winning the games there you get a "random" chest, which is actually not random... When you create your account you get a digit assigned to you from 0 to 239, and after that it follows a pattern for the chest drops. The applciation I'm making would take a user's entries and compare it to the pattern, thus being able to predict how soon the next chests of a higher quality would drop.
The help I need with the code:
Is it possible to make an array kind of... loop within itself.. So for example when going through the array in a loop, if "i" is 239, then adding +1 would take it back to the beginning, or #0 (239 not necessarily being the limit).
The class (and it's container that I want to loop):
class Chest
{
public int ID { get; set; }
public string Type { get; set; }
public Chest()
{
}
public Chest(int id, string type)
{
ID = id;
Type = type;
}
}
class ChestContainer
{
private Chest[] ChestList = new Chest[240];
public int Count { get; set; }
public ChestContainer(int size)
{
ChestList = new Chest[size];
}
public void Add(Chest chest)
{
ChestList[Count++] = chest;
}
public Chest Get(int index)
{
return ChestList[index];
}
}
Also wouldn't mind any tips to improve my class / container class, at the moment this is what I've been doing for pretty much my entire "career" as this is what we were thought in uni (minus the string override for the class).
You could use Modulo % in order to get a loop kind of thing.
If you replace the Container.Add method with the one below, the index will be "reset" (for lack of better words).
public void Add(Chest chest)
{
ChestList[Count++%(ChestList.Length)] = chest;
}
After updating the method, if you want an example, you can try the code below:
var container = new ChestContainer(240);
for (int i = 0; i < 1000; i++)
container.Add(new Chest(i, $"{i}"));
Edit In order to have the Get method working as well, modifying it as mentioned below will ensure your container works as expected:
public Chest Get(int index)
{
return ChestList[index%(ChestList.Length)];
}
To test it out, you can use the code below:
var container = new ChestContainer(240);
for (int i = 0; i < 1000; i++)
{
container.Add(new Chest(i, $"{i}"));
var value = container.Get(i);
}
You can overload the [] operator to define it's behaviour.
Something like this:
public static Chest operator [] (int index) {
return ChestList[index%240];
}
public Chest Get(int index)
{
return ChestList[index%240]; //put your limit here
}
How it works: % is the modulo operator.
It returns the remainder of a devision.
Example:
5/2 = 2, remaining 1
=> 5%2 = 1
In your case, when numbers higher than 239 are entered, with modulo it just wraps around.

How to shuffle multiple related arrays?

I have some unusual I need to do. I am wondering if anyone can think of an easy
way to make the change that I need. What I have is a
public class Report
{
public string[] Text { get; set; }
public string[] Image { get; set; }
public string[] Explanation { get; set; }
}
The report class can have any number of Texts, Images and Explanations and the size of each array is always the consistent but maybe be different for each report instance.
What I need to do is to be able to sort the array elements in a random order. So for example I might have
Report.Text[0] = "text0";
Report.Text[1] = "text1";
Report.Text[2] = "text2";
Report.Image[0] = "img0";
Report.Image[1] = "img1";
Report.Image[2] = "img2";
Report.Explanation[0] = "exp0";
Report.Explanation[1] = "exp1";
Report.Explanation[2] = "exp2";
then after sorting
Report.Text[0] = "text2";
Report.Text[1] = "text0";
Report.Text[2] = "text1";
Report.Image[0] = "img2";
Report.Image[1] = "img0";
Report.Image[2] = "img1";
Report.Explanation[0] = "exp2";
Report.Explanation[1] = "exp0";
Report.Explanation[2] = "exp1";
Can anyone think of a simple way to do this? All I can think of is that I need to create a
new temporary object of the same size and do some kind of swapping. But I am not sure how
to randomize. The reason I am asking is just in case someone has had this need in the past.
I would strongly recommend that you refactor this to create a single class to encapsulate the { Text, Image, Explanation } tuple. At that point, the code will be cleaner and it'll be trivial to reorder the values. Heck, you may not even need a Report type at that point... you may just be able to have a List<ReportItem> or whatever. You'd only need a separate Report type if you wanted to add extra behaviour or data to tie things together.
(As an aside, I hope you don't really have public fields for these to start with...)
If you then have a question around shuffling a single collection, a modified Fisher-Yates shuffle is probably the easiest approach. You could do this with the multiple arrays as well, but it wouldn't be nice - and would have to be specific to Report... whereas you could easily write a generic Fisher-Yates implementation based on IList<T>. If you search on Stack Overflow, you should easily be able to find a few existing implementations :)
If you choose to change your class to the following:
public class Report
{
public string Text { get; set; }
public string Image { get; set; }
public string Explanation { get; set; }
}
You could then do this using an extension method:
(See answer on this SO question)
Then call it this way:
List<Report> reports = new List<Report> { /* create list of reports */ }
Random rnd = new Random();
foreach (Report r in reports.Shuffle(rnd)) {
/* do something with each report */
}
Why don't you create a class
public class Report
{
public string Text { get; set; }
public string Image { get; set; }
public string Explanation { get; set; }
}
and then create a List of those objects and manage it through the list properties:
IList<Report> yourList = new List<Report>()
Here is my solution
class StringWrapper
{
public int Index;
public string Str;
}
public string[] MixArray(string[] array)
{
Random random = new Random();
StringWrapper[] wrappedArray = WrapArray(array);
for (int i = 0; i < wrappedArray.Length; i++)
{
int randomIndex = random.Next(0, wrappedArray.Length - 1);
wrappedArray[i].Index = randomIndex;
}
Array.Sort(wrappedArray, (str1, str2) => str1.Index.CompareTo(str2.Index));
return wrappedArray.Select(wrappedStr => wrappedStr.Str).ToArray();
}
private StringWrapper[] WrapArray(string[] array)
{
int i = 0;
return array.Select(str => new StringWrapper {Index = ++i, Str = str}).ToArray();
}
Then you can call MixArray for each Report object for each property you wand to randomize.
I am not sure I am fond of this direction, but ...
To do exactly what you ask (the law, not the spirit of the law), you will have to add additional arrays and pull items over. In addition, for each array, you will need a List or similar to store the items you have already randomly pulled over. After that, things are simple. Use the Random class to create random numbers, check if the item has already been moved (using the List), if not store the result in the new array/list, add the value to your List to make sure you do not move the same item twice. Once everything is moved, set this new array to the old array.
Now, what is the business reason for randomizing? That might affect whether or not this is a good idea.
ADDED:
After examination of skeet's response, here is a way to solve this if you can use the following type of class:
public class Report {
public string Text { get; set; }
public string Image { get; set; }
public string Explanation { get; set; }
}
Here is one "down and dirty" type of sort:
private static SortedList<int, Report> SortRandomly(List<Report> reports)
{
Random rnd = new Random((int)DateTime.Now.Ticks);
List<int> usedNumbers = new List<int>();
SortedList<int, Report> sortedReports = new SortedList<int, Report>();
int maxValue = reports.Count;
foreach(Report report in reports)
{
bool finished = false;
int randomNumber = 0;
//Get unique random (refactor out?)
while(!finished)
{
randomNumber = rnd.Next(0, maxValue);
if(!usedNumbers.Contains(randomNumber))
{
finished = true;
usedNumbers.Add(randomNumber);
}
}
sortedReports.Add(randomNumber, report);
}
return sortedReports;
}
Note, you can also work to keep the sort in order and randomly picking from the original list, which means you can, in theory, keep it as a list.
private static List<Report> SortRandomly(List<Report> reports)
{
Random rnd = new Random((int)DateTime.Now.Ticks);
List<Report> outputList = new List<Report>();
List<int> usedNumbers = new List<int>();
int maxValue = reports.Count-1;
while(outputList.Count < reports.Count)
{
int randomNumber = rnd.Next(0, maxValue);
if(!usedNumbers.Contains(randomNumber))
{
outputList.Add(reports[randomNumber]);
}
}
return outputList;
}
Even better, consider sorting the list of numbers first and then grabbing the reports, in an order manner. Once again, the above are down and dirty implementations and using the specific requirements will certainly refine the algorithms.

Categories