1

I am trying to add a non-constant value of elements by either looping through x times or just adding x nr of elements directly.

I've tried looping for x amount of times and add an element every iteration, but it only ends up with one element. I've also tried adding a collection, but the same result occurred.

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;



public class tileMapDepth 
{
    public List<bool> tileMapWidth;

    public tileMapDepth(List<bool> realWidth)
    {
        this.tileMapWidth = realWidth;
    }
}

public class TestListTypeAndSuch : MonoBehaviour
{

    public List<tileMapDepth> tileMap = new List<tileMapDepth>(1);
    public int width = 5;
    public int depth = 5;

    void Start()
    {
        for (int i = 0; i < depth; i++)
        {
            tileMap.Add(new tileMapDepth(new List<bool>(new List<bool>(width))));
            foreach (tileMapDepth tile in tileMap)
            {
                for (int j = 0; j < width; j++)
                {
                    tile.tileMapWidth.Add(false);
                }
            }
        }
    }
}

The expected result is to add a nr of elements into a list and not just one element. When i try and add booleans with a constant value as the amount it works fine. But i need to add it with a dynamic variable. The code below is the only one that is working.

        for (int i = 0; i < depth; i++)
        {
            tileMap.Add(new tileMapDepth(new List<bool>(new List<bool>(new bool[13]))));
        }
Nero
  • 11
  • 4

2 Answers2

2

Try

List<T>.AddRange(IEnumerable<T>) Method

Adds the elements of the specified collection to the end of the List.

Shad
  • 1,185
  • 1
  • 12
  • 27
  • I tried adding multiple elements by using this line instead " tile.tileMapWidth.AddRange(new List(width));", but the same result occurred. – Nero Jun 27 '19 at 10:13
  • 1
    your `new List(width)` sets the initial **capacity** of the list but doesn't add any elements yet. Instead I would use `(new bool[width]).ToList()`; – derHugo Jun 27 '19 at 15:35
0

You are trying to add false value to a list 5 times i.e in tileMapWidth. To add same value n times to list you can use Enumerable.Repeat()

List<bool> tileMapWidth = Enumerable.Repeat(false, 5).ToList();

This list you can pass as a parameter to tileMapDepth() class

something like

tileMap.Add(new tileMapDepth(tileMapWidth));
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
  • I tried with the Enumerable.Repeat, and it still gave me one element in the outer and inner list. Also the last line of code you showed, that is not possible because tileMapWidth is a property of class tileMapDepth, so it needs to have a reference to an instance of tileMapDepth first. Thats why it gets tricky maybe. – Nero Jun 27 '19 at 10:10
  • I added first line of code which create instance of `tileMapWidth ` – Prasad Telkikar Jun 27 '19 at 10:15
  • Ah i see. I tried, but only one element in tileMap, aswell as only one element in the property list "tileMapWidth". Yet the capacity of "tileMapWidth" was 4, but i am looking to increase the .Count and not the .Capacity to fill it with elements of x amount. – Nero Jun 27 '19 at 10:21
  • The strange thing here is that when i put in a constant value as the ammount of the collection like: – Nero Jun 27 '19 at 10:24
  • You need to add it in for loop – Prasad Telkikar Jun 27 '19 at 10:27
  • "for (int i = 0; i < depth; i++) { tileMap.Add(new tileMapDepth(new List(new List(width)))); List tileWidth = Enumerable.Repeat(false, width).ToList(); foreach (tileMapDepth tile in tileMap) { for (int j = 0; j < width; j++) { tile.tileMapWidth.AddRange(new bool[8]); } } } " – Nero Jun 27 '19 at 10:31
  • Okey, ive given up on formatting the comments because it doesnt work for me. Anyways i have added it like the code above, and also put it in the inner loop, and it doesnt give me more elements. Could you try and compile this on your pc? – Nero Jun 27 '19 at 10:37