- Home /
Lists and Structs instead of Arrays?
Supposedly, lists and structs are preferred over arrays.
1) Why?
2) What if a property of your struct/subclass contains an array?
class arrayinhere{ var gender:int; var CarLocations:Vector3[]; }
Well, in the question you've linked it's a special case. He got adviced to use a struct or a class to group his different data into a common "data chunk". He used a multidimensional array in the first place to store different things in the array. An array should contain only one type of data. He need to store an int and a float together so it makes more sense to create a struct or class that contains an int and a float and store those structs in a one dimensional array.
I don't get your second question. What's so special when a class contains an array? It's just a class that contains an array variable. Just make sure you create the array since an array is a reference type you have to create it before you can use it.
Actually, the weird part is that I can declare the CarPositions:Vector3[], but all those vector array values return as Zero Vector!! o.O .. Do you get the same results?
Yes, of course.
When you initialize a Vector3, its value will be Vector3.zero until you change it.
This is because it is a struct that contains 3 floats.
Float and ints cannot be null, so they have to get set to something.
Answer by Eric5h5 · Dec 18, 2011 at 06:39 AM
Lists aren't preferred over arrays; they are used for different things. Arrays are faster, Lists are slower but can be be manipulated easily as far as adding/removing objects (but they still use arrays on the back end). If an array doesn't need to be resized, using a List is a waste.
Saying "structs are preferred over arrays" doesn't make any sense; structs and arrays are completely different things. You can have an array of structs, which is what an array of e.g. ints or floats or Vector3s would be.
Well, lists allow the use of structs for "associative arrays". Beyond just being able to resize them, it seems there are more advantages to using a List - such as for custom data types (structs?), as in context of the example linked above?
As I said, you can make an array of structs or classes. There's nothing you can do with Lists that can't be done with arrays--also as I said, Lists are in fact implemented using arrays "behind the scenes". Lists are just easier to use for some things, such as dynamic arrays that have elements added and removed.
speaking of which - is there a limitation for lists with structs to create "pseudo associative array" - the array declared in the list struct can't be populated with non-null values!
Answer by jahroy · Dec 18, 2011 at 06:28 AM
Lists can be preferred over Arrays at times because they can be resized dynamically at run time, they have a handy list of operations that they can perform, and they can take advantage of generics.
I use arrays all the time (whenever possible). There's absolutely nothing wrong with them.
Structs (I think) are desirable because they're fast and similar to primitives: they don't have to be created with the new operator. I don't know much more about them.
I was a java buffoon before I started trying to become a Unity buffoon. To my knowledge there is nothing like a struct in java (I'm probably wrong).
Just updated above with a simple example of a struct/subclass with array!
Yep... Tried to quickly remove my comment before I got busted!
I think arrays vs structs is kinda apples to oranges.
I think the debate would be classes vs structs.
Actually, the weird part is that I can declare the CarPositions:Vector3[], but all those vector array values return as Zero Vector!! o.O .. Do you get the same results?
Structs are same as classes, you can put many things in them but unlike classes, structures are value types. Using one or the other depends on many things such as context, size, the way it will be used adn others, so it's a case per case decision.
Arrays, as said previously, are used with only one type of data but, even though they have less basic functions than lists, they can be multidimensional, while lists a unidimensional.
All in all it all depends on what use you want to do with the data.
Personnaly i use classes and lists all the time because it's easier (for me) to manage simple adresses.
if you combine lists with structs, they become multidimensional though...
Answer by markl · Dec 20, 2011 at 11:24 PM
Firstly the Unity team use Collections not arrays all the time. In the following examples the Lists versions are only 1.5x slower than the array version - and the Unity team use Collections. It's not enough of a speed difference for something so unimportant to warrant writing so much extra code to use low level coding techniques like arrays. Generics gives a lot more than writing everything from scratch with arrays and the difference is not relevant compared to issues like models, textures, lighting and shaders.
Cheers.
using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Linq; using System; using System.Diagnostics;
public class DynamicMegaMod : MonoBehaviour {
private bool Exists(int val, MyClass[] search)
{
foreach (MyClass i in search)
if (i.MyValue == val)
return true;
return false;
}
private MyClass Find(int val, MyClass[] search)
{
foreach (MyClass i in search)
if (i.MyValue == val)
return i;
return null;
}
public class MyClass
{
public int MyValue { get; set; }
public string AnotherValue { get; set; }
public string AnotherValue2 { get; set; }
}
// Use this for initialization
void Start () {
System.Diagnostics.Stopwatch sw1 = new System.Diagnostics.Stopwatch();
//trying to find if an element exists in an array of 100000, 10000 times
sw1.Start();
MyClass[] myArray = new MyClass[100000];
for (int i = 0; i != 100000; i++)
myArray[i] = new MyClass() {MyValue=i};
for (int i = 0; i != 10000; i++)
{
if (Exists(50000, myArray))
{
}
}
sw1.Stop();
System.Diagnostics.Stopwatch sw2 = new System.Diagnostics.Stopwatch();
//trying to find if an element exists in a list of 100000, 10000 times
sw2.Start();
List<MyClass> myList = new List<MyClass>(100000);
for (int i = 0; i != 100000; i++)
myList.Add(new MyClass() {MyValue=i});
for (int i = 0; i != 10000; i++)
{
if (myList.Exists(mv => mv.MyValue == 50000))
{
}
}
sw2.Stop();
System.Diagnostics.Stopwatch sw3 = new System.Diagnostics.Stopwatch();
//trying to find an element in an array of 100000, 10000 times
sw3.Start();
MyClass[] myArray2 = new MyClass[100000];
for (int i = 0; i != 100000; i++)
myArray2[i] = new MyClass() {MyValue=i};
for (int i = 0; i != 10000; i++)
{
if (Find(50000, myArray2) != null)
{
}
}
sw3.Stop();
System.Diagnostics.Stopwatch sw4 = new System.Diagnostics.Stopwatch();
//trying to find an element in a list of 100000, 10000 times
sw4.Start();
List<MyClass> myList2 = new List<MyClass>(100000);
for (int i = 0; i != 100000; i++)
myList2.Add(new MyClass() {MyValue=i});
for (int i = 0; i != 10000; i++)
{
if (myList2.Find(mv => mv.MyValue == 50000) != null)
{
}
}
sw4.Stop();
}
// Update is called once per frame
void Update () {
/* do something */
}
}
Answer by dscroggi · Mar 28, 2014 at 12:59 AM
I cast a vote for IEnumerable
The OP wants us to compare apples to oranges and you're casting a vote for chihuahuas...