Click here to Skip to main content
15,910,773 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello.
I created a array like :
List<List<ulong>>[] Lst = new List<List<ulong>>[20000];

How can I access the List of List in some index like Lst[1500] and add new List into that List of List? If a want to print the element of Lst[1500] then should I use this code:

<br />
var LstLst = Lst[1000];<br />
foreach (var LL in LstLst)<code>

{
foreach (var i in LL)
{
Console.Write(i);
}
Console.WriteLine();
}

??
Please help.
Posted

Lst.Add(new List<ulong>); will add another list.

You access the internal lists by index.
 
Share this answer
 
Not clear. Here is my code:
using System;<br />
using System.Collections.Generic;<br />
<br />
class CoveringCuboid<br />
{<br />
    static void Main()<br />
    {<br />
        List<List<ulong>>[] Lst = new List<List<ulong>>[20000];<br />
        DateTime StartTime = DateTime.Now;<br />
<br />
        ulong Limit = 20000;<br />
        ulong[] LayerArray = new ulong[Limit + 1];<br />
        <br />
        for (ulong X = 1; NumberOfBlock(X,X,X,1)<=Limit; X++)<br />
        {<br />
            for (ulong Y = X; NumberOfBlock(X, Y, Y, 1) <= Limit; Y++)<br />
            {<br />
                for (ulong Z = Y; NumberOfBlock(X, Y, Z, 1) <= Limit; Z++)<br />
                {<br />
                    for (ulong Layer = 1; NumberOfBlock(X, Y, Z, Layer) <= Limit; Layer++)<br />
                    {<br />
                        LayerArray[NumberOfBlock(X, Y, Z, Layer)]++;<br />
                        //I want to add here X, Y, Z, Layer, in the existing List at the index of <br />
                        //Lst[NumberOfBlock(x, Y, Z, Layer)].<br />
                    }<br />
                }<br />
            }<br />
        }<br />
<br />
        Console.WriteLine("\n  Result : " + Array.IndexOf<ulong>(LayerArray, 1000));<br />
<br />
        DateTime EndTime = DateTime.Now;<br />
<br />
        TimeSpan ExecutionTime = EndTime - StartTime;<br />
<br />
        Console.WriteLine("\n  Total Execution Time : {0} millisec.\n", ExecutionTime.TotalMilliseconds);<br />
<br />
        var a = Lst[1000];<br />
<br />
        foreach (var b in a)<br />
        {<br />
            foreach (var c in b)<br />
            {<br />
                Console.Write(c);<br />
<br />
            }<br />
            Console.WriteLine();<br />
        }<br />
    }<br />
<br />
    static ulong NumberOfBlock(ulong X, ulong Y, ulong Z, ulong Layer)<br />
    { <br />
        return 4 * (X + Y + Z + Layer - 2) * (Layer - 1) + 2 * (X * Y + X * Z + Y * Z);<br />
    }<br />
}
 
Share this answer
 
You could consider using some other collection like a Dictionary or a Hashtable instead of using a List of a List.
 
Share this answer
 
v2
C#
Lst[1500].Add(new List<ulong>());

Yes, you could print the list of lists out that way. Though you might want to change Console.Write(i); to Console.Write(i.ToString() + " ");.
 
Share this answer
 
Dictionary is ok but is it possible to create such a data structure?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900