Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ina · Dec 18, 2011 at 06:13 AM · arraylistclassdatastruct

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[];
}
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Dec 18, 2011 at 12:39 PM 1
Share

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.

avatar image ina · Dec 20, 2011 at 11:07 PM 0
Share

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?

avatar image jahroy · Dec 23, 2011 at 02:41 PM 0
Share

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.

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ina · Dec 20, 2011 at 09:34 AM 0
Share

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?

avatar image Eric5h5 · Dec 20, 2011 at 09:39 AM 0
Share

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.

avatar image ina · Dec 20, 2011 at 11:06 PM 0
Share

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!

avatar image
1

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).

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ina · Dec 18, 2011 at 06:33 AM 0
Share

Just updated above with a simple example of a struct/subclass with array!

avatar image jahroy · Dec 18, 2011 at 06:35 AM 0
Share

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.

avatar image ina · Dec 20, 2011 at 09:34 AM 0
Share

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?

avatar image ks13 · Dec 20, 2011 at 09:56 AM 0
Share

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.

avatar image ina · Dec 20, 2011 at 11:05 PM 0
Share

if you combine lists with structs, they become multidimensional though...

Show more comments
avatar image
-1

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 */
     
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-1

Answer by dscroggi · Mar 28, 2014 at 12:59 AM

I cast a vote for IEnumerable

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jahroy · Mar 28, 2014 at 01:32 AM 1
Share

The OP wants us to compare apples to oranges and you're casting a vote for chihuahuas...

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it possible to Change mesh also multi materials by a button or a key 0 Answers

Array or Data Structure with Vector3 as keys 2 Answers

Nullreferenceexception, Struct, Array, List 1 Answer

Reading and Storing External Data into Memory (From Text) 1 Answer

Best way to store a lot of data by script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges