0

I am trying to work with list of lists. how do I remove a specific element from the list? I have the following code:

using System;
using System.Collections.Generic;
namespace TEST3
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            List<List<int>> ar = new List<List<int>> ();
            List<int> innerAr = new List<int> ();
            for (int i = 1; i <= 9; i++) 
            {
                innerAr.Add (i);
            }
            for (int j = 0; j <= 80; j++) 
            {
                ar.Add (innerAr);
            }
            ar[80].RemoveAt(7);
            ar[80].RemoveAt(2);
            Console.WriteLine (ar[80].Count);
            Console.WriteLine (ar[79].Count);
        }
    }
}
Simon
  • 1,244
  • 8
  • 21
Bar Ittah
  • 51
  • 1
  • 6
  • What's wrong with the code you have? I'm going to guess you're wondering why the counts for 79 and 80 are the same? – Charles Mager May 30 '15 at 10:03
  • Here you can find answer. http://stackoverflow.com/questions/1582285/how-to-remove-elements-from-a-generic-list-while-iterating-over-it – dd_ May 30 '15 at 10:07

4 Answers4

1
for (int j = 0; j <= 80; j++) 
{
    ar.Add (innerAr);
}

All elements in ar now contain the same reference to innerAr. There is only a single list that you kept adding to ar, so when you later change innerAr by accessing ar[80], then you also change the innerAr of all the other elements (because it’s the same list).

If you want independent list, you need to create one for each ar item:

List<List<int>> ar = new List<List<int>>();
for (int j = 0; j <= 80; j++) 
{
    List<int> innerAr = new List<int>();
    for (int i = 1; i <= 9; i++) 
    {
        innerAr.Add(i);
    }
    ar.Add(innerAr);
}
poke
  • 369,085
  • 72
  • 557
  • 602
0

The lists have the same Count because is the same list added multiple times.

If you want just one list to be changed due to RemoveAt, you have to create a new one. A simple way to create a new list with same elements is to add ToList().

public static void Main(string[] args)
{
    List<List<int>> ar = new List<List<int>>();
    List<int> innerAr = new List<int>();
    for (int i = 1; i <= 9; i++)
    {

        innerAr.Add(i);
    }
    for (int j = 0; j <= 80; j++)
    {
        ar.Add(innerAr.ToList()); // <- here is the change
    }
    ar[80].RemoveAt(7);
    ar[80].RemoveAt(2);
    Console.WriteLine(ar[80].Count); // 7
    Console.WriteLine(ar[79].Count); // 9
}
adricadar
  • 9,971
  • 5
  • 33
  • 46
  • @BarIttah Welcome to stackoverlow. Don't forget to mark the [accepted answer](http://meta.stackexchange.com/a/5235/286484). – adricadar May 30 '15 at 13:35
0

Your removal is successful. As Charles hinted your only mistake is that the Object innerAt, is the exact same object that is in each of the 80 lists of List. since List is an object reference and not a value, you have the same reference in ar[79] and ar[80]

Guy
  • 1,232
  • 10
  • 21
0

You have parent list

 List<List<int>> parent=new List<List<int>>();

And your child list

List<int> child=new List<int>(){1,2,3};

Adding to parent

parent.Add(child);

child elements 1,2,3

Removing

parent[0].removeAt(0)

child elements 2,3

user786
  • 3,902
  • 4
  • 40
  • 72