- Home /
what are Lists?
It is written in somewhere that Lists are better compared to Arrays for ease of use.
Are they as fast? what are they? are they in unity 3? where can i read about Lists?
Answer by whydoidoit · Oct 07, 2012 at 09:33 PM
Lists are typesafe ways of producing a collection that can be modified at run time - increasing or decreasing the size. They are preferable to arrays because of typesafety, they also have a number of useful methods that can really speed things up. A List provides the same [] array access operator and can be used in place of arrays in many circumstances. Here is the documentation on lists http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
Lists are very fast - adding to a list can cause the need for garbage collection however.
In C# you must add
using System.Collections.Generic;
To your file and then you define a list like this:
List<int> myIntList = new List<int>();
List<MyOwnClass> myClassList = new List<MyOwnClass>();
In Javascript it works like this:
var myOwnClass = new List.<MyOwnClass>();
var myIntList = new List.<int>();
You can add elements using Add or AddRange - remove them using Remove and RemoveAt, access elements using myIntList[1], the number of elements in a list is stored in a Count property rather than Length. You can sort lists etc.
Lists are preferable to Arrays and ArrayList, but not to native arrays like int[10] if the array size is fixed.
Thanks! i will learn them and then will post some code here :)
Your answer
Follow this Question
Related Questions
Problem with arrays in a list 1 Answer
Undo/back system using a List/Array 2 Answers
Copy values between two classes in two lists. 1 Answer
How to modify array values? 1 Answer