Home > other >  adding an int value to multi array int list
adding an int value to multi array int list

Time:02-17

I got this multiarray list of integers that would look something like this:

List<List<int>> multiarray = new List<List<int>>{    
                new List<int>  { 8, 63  },
                new List<int>  { 4, 2   }, 
                new List<int>  { 0, -55 }, 
                new List<int>  { 8, 57  }, 
                new List<int>  { 2, -120}, 
                new List<int>  { 8, 53  }  
            };

Now let's say I want to create it and add items using a variable, how will I do so? I thought it would be as the following:

                int value1 = 4
                int value2 = 5
                ListStat.Add(value1, value2);

But I get an error saying I cant overload using the method "add", any other command I should use?

CodePudding user response:

int value1 = 4;
int value2 = 5;
multiarray.Add(new List<int>{value1, value2});

CodePudding user response:

(Alternate method for problem solving, without multi List, but it's longer)

//Helper class
class ListStat
{
      public ListStat(int value1, int value2)
      {
           this.value1 = value1;
           this.value2 = value2;
      }

      public int value1 { get; set; }
      public int value2 { get; set; }
}

static void Main(string[] args)
{
      List<ListStat> lList = new List<ListStat>()
      {
           new ListStat(8,63),
           new ListStat(4,2),
           new ListStat(0,-55),
           new ListStat(8,57)
      };
      lList.Add(new ListStat(0, 0)); //Adding values
      Console.WriteLine($"({lList[0].value1};{lList[0].value2})"); //Ref to first element
      //Ref for all element step by step in lList
      foreach (ListStat singleItem in lList)
      {
          Console.WriteLine($"({singleItem.value1};{singleItem.value2})");
      }
}

CodePudding user response:

Use a Dictionary<int,List<int> instead, there you could find your List<int> according to your int Key and add the values to that List.

  • Related