Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List with 2 arguments

I'm currently having this code:

List<int> list = new List<int>();
list.Add(0);
list.Add(1);
list.Add(2);
list.Add(3);
color0 = list[1];
color1 = list[2];
color2 = list[3];
color3 = list[4];

Is there a possible way, this list could take 2 arguments in 1 element? What I mean is:

List<int,int> list = new List<int,int>();
list.Add(0,3);
list.Add(1,8);
color0=list[1][2]; //output 3
color1=list[1][1]; //output 0
color2=list[2][2]; //output 8
color3=list[2][1]; //output 1

Is there a possible was i can achieve something similar to this?

like image 368
Andics Avatar asked Aug 01 '15 13:08

Andics


People also ask

Can list have two parameters in Java?

You can add as many parameters as you want, just separate them with a comma.

How do you pass two lists as an argument in Python?

Passing two lists and 'sum' function to map() Passing sum function, list1 and list2 to map(). The element at index 0 from both the list will pass on as argument to sum function and their sum will be returned. This loop continues till elements of one list get exhausted. The result will be stored in result list.

Can I pass list as an argument?

You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.

How many arguments can we pass in list() method in Python?

In Python, functions can take either no arguments, a single argument, or more than one argument.


2 Answers

You can use Tuple:

var list = new List<Tuple<int, int>>();
list.Add(new Tuple<int, int>(1, 2));

For easier use you can create an extension method:

public static class Extensions
{
    public static void Add(this List<Tuple<int, int>> list, int x, int y)
    {
        list.Add(new Tuple<int, int>(x, y));
    }
}

Then you can add an element using this code:

list.Add(1, 2);

To access the items:

var listItem = list[0]; // first item of list
int value = listItem.Item2; // second "column" of the item
like image 170
Flat Eric Avatar answered Sep 21 '22 05:09

Flat Eric


You can go with dictionary also if you have a key value pair:

static void Main()
    {
    Dictionary<int, int> dictionary =
        new Dictionary<int, int>();

    dictionary.Add(0,3);
    dictionary.Add(1,8);
    dictionary.Add(2, 13);
    dictionary.Add(3, -1);
    }
like image 31
DeshDeep Singh Avatar answered Sep 19 '22 05:09

DeshDeep Singh