简体   繁体   中英

How to use list in this example

Lets say i have an array like that (I know that is not possible on c#):

string[,] arr = new string[,]
{
    {"expensive", "costly", "pricy", 0},
    {"black", "dark", 0}
};

So how I can add this items on list and how I can add new item between "pricy" and 0 ? I couldn't find any example on the net.

Arrays are immutable, so you can't really add or remove items from them. The only thing you can do for example is to copy the items to another array instance, minus the ones you don't want, or do the same but use a higher dimension and add the items you need to add.

I'd recommend using a List<T> here, where T could be a simple type that mirrors the things you're adding to the array. For example:

class Thing {
    public string Prop1 {get; set; }
    public string Prop2 {get; set; }
    public string Prop3 {get; set; }
    public int Prop4 {get; set; }
}

List<Thing> list = new List<Thing>();

list.Add(new Thing() { Prop1 = "expensive", Prop2 = "costly", Prop3 = "pricy", Prop4 = 0};

Then you can insert items:

list.Insert(1, new Thing() { Prop1 = "black", Prop2 = "dark", Prop4 = 0});

Not sure if this would work for you, it depends on whether your 'jagged' data can be made to fit into Thing . Obviously here 'Prop1' and so on would be the actual property names of the data you have in your array.

If you want to Add (Insert) items then do not use arrays. Use List<> .

Your sample might be covered by

var data = new List<string>[2] { new List<string>(), new List<string> () };

You can then use statements like

data[0].Add("expensive");
string s = data[1][1];     // "dark"

It is of course not possible to have 0 in a string array or List. You could use null but try to avoid it first.

Well what do you want a list OF ? Right now you've got strings and integers so object is your common base class

You can do a jagged array (an array of arrays):

object[][] arr = new []
{
    new object[] {"expensive", "costly", "pricy", 0},
    new object[] {"black", "dark", 0}
};

or a list of lists:

List<List<object>> arr = new List<List<object>> 
{
    new List<object> {"expensive", "costly", "pricy", 0},
    new List<object> {"black", "dark", 0}
};

But both of those seem like bad designs. If you give more information on what you're trying to accomplish you can probably get some better suggestions.

You could make it a Dictionary<string,string> , but the key would have to remain unique. You would then be able to loop through like so

Dictionary<string,string> list = new Dictionary<string,string>();
foreach(KeyValuePair kvp in list) 
{
    //Do something here
}

Your task is little strange, and i don't understand where it can be useful. But in your context you can do it without List and so on. You should go through element by indexer(in your example item in string[,] can be get only with TWO indexes).

So here solution that works, i did it only for interesting

var arr = new[,]
                          {
                              {"expensive", "costly", "pricy", "0"},
                              {"black", "dark", "0", "0"}
                          };
            int line = 0;
            int positionInLine = 3;
            string newValue = "NewItem";


            for(var i = 0; i<=line;i++)
            {
                for (int j = 0; j <=positionInLine; j++)
                {
                    if (i == line && positionInLine == j)
                    {
                        var tmp1 = arr[line, positionInLine];
                        arr[line, positionInLine] = newValue;

                        try
                        {
                            // Move other elements
                            for (int rep = j+1; rep < int.MaxValue; rep++)
                            {
                                var tmp2 = arr[line, rep];

                                arr[line, rep] = tmp1;

                                tmp1 = tmp2;

                            }
                        }
                        catch (Exception)
                        {

                            break;
                        }
                    }
                }
            }
class Program
{
    static void Main(string[] args)
    {
        List<Array> _list = new List<Array>();

        _list.Add(new int[2] { 100, 200 });
        _list.Add(new string[2] { "John", "Ankush" });

        foreach (Array _array in _list)
        {
            if (_array.GetType() == typeof(Int32[]))
            {
                foreach (int i in _array)
                    Console.WriteLine(i);
            }
            else if (_array.GetType() == typeof(string[]))
            {
                foreach (string s in _array)
                    Console.WriteLine(s);
            }
        }
        Console.ReadKey();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM