I\'d like to set up a multidimensional list. For reference, I am working on a playlist analyzer.
I have a file/file-list, which my program saves in a standard list.
I used:
List<List<String>> List1 = new List<List<String>>
var List<int> = new List<int>();
List.add("Test");
List.add("Test2");
List1.add(List);
var List<int> = new List<int>();
List.add("Test3");
List1.add(List);
that equals:
List1
(
[0] => List2 // List1[0][x]
(
[0] => Test // List[0][0] etc.
[1] => Test2
)
[1] => List2
(
[0] => Test3
another work around which i have used was...
List<int []> itemIDs = new List<int[]>();
itemIDs.Add( new int[2] { 101, 202 } );
The library i'm working on has a very formal class structure and i didn't wan't extra stuff in there effectively for the privilege of recording two 'related' ints.
Relies on the programmer entering only a 2 item array but as it's not a common item i think it works.
As Jon Skeet mentioned you can do it with a List<Track>
instead. The Track class would look something like this:
public class Track {
public int TrackID { get; set; }
public string Name { get; set; }
public string Artist { get; set; }
public string Album { get; set; }
public int PlayCount { get; set; }
public int SkipCount { get; set; }
}
And to create a track list as a List<Track>
you simply do this:
var trackList = new List<Track>();
Adding tracks can be as simple as this:
trackList.add( new Track {
TrackID = 1234,
Name = "I'm Gonna Be (500 Miles)",
Artist = "The Proclaimers",
Album = "Finest",
PlayCount = 10,
SkipCount = 1
});
Accessing tracks can be done with the indexing operator:
Track firstTrack = trackList[0];
Hope this helps.
This is the easiest way i have found to do it.
List<List<String>> matrix= new List<List<String>>(); //Creates new nested List
matrix.Add(new List<String>()); //Adds new sub List
matrix[0].Add("2349"); //Add values to the sub List at index 0
matrix[0].Add("The Prime of Your Life");
matrix[0].Add("Daft Punk");
matrix[0].Add("Human After All");
matrix[0].Add("3");
matrix[0].Add("2");
To retrieve values is even easier
string title = matrix[0][1]; //Retrieve value at index 1 from sub List at index 0
Here's a little something that I made a while ago for a game engine I was working on. It was used as a local object variable holder. Basically, you use it as a normal list, but it holds the value at the position of what ever the string name is(or ID). A bit of modification, and you will have your 2D list.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GameEngineInterpreter
{
public class VariableList<T>
{
private List<string> list1;
private List<T> list2;
/// <summary>
/// Initialize a new Variable List
/// </summary>
public VariableList()
{
list1 = new List<string>();
list2 = new List<T>();
}
/// <summary>
/// Set the value of a variable. If the variable does not exist, then it is created
/// </summary>
/// <param name="variable">Name or ID of the variable</param>
/// <param name="value">The value of the variable</param>
public void Set(string variable, T value)
{
if (!list1.Contains(variable))
{
list1.Add(variable);
list2.Add(value);
}
else
{
list2[list1.IndexOf(variable)] = value;
}
}
/// <summary>
/// Remove the variable if it exists
/// </summary>
/// <param name="variable">Name or ID of the variable</param>
public void Remove(string variable)
{
if (list1.Contains(variable))
{
list2.RemoveAt(list1.IndexOf(variable));
list1.RemoveAt(list1.IndexOf(variable));
}
}
/// <summary>
/// Clears the variable list
/// </summary>
public void Clear()
{
list1.Clear();
list2.Clear();
}
/// <summary>
/// Get the value of the variable if it exists
/// </summary>
/// <param name="variable">Name or ID of the variable</param>
/// <returns>Value</returns>
public T Get(string variable)
{
if (list1.Contains(variable))
{
return (list2[list1.IndexOf(variable)]);
}
else
{
return default(T);
}
}
/// <summary>
/// Get a string list of all the variables
/// </summary>
/// <returns>List string</string></returns>
public List<string> GetList()
{
return (list1);
}
}
}
Here is how to make a 2 dimensional list
// Generating lists in a loop.
List<List<string>> biglist = new List<List<string>>();
for(int i = 1; i <= 10; i++)
{
List<string> list1 = new List<string>();
biglist.Add(list1);
}
// Populating the lists
for (int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
biglist[i].Add((i).ToString() + " " + j.ToString());
}
}
textbox1.Text = biglist[5][9] + "\n";
Be aware of the danger of accessing a location that is not populated.