Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by gregzo · Dec 13, 2011 at 11:20 PM · javascriptarray.netresizecopy

How do I Resize an Array in JS?

So, Newbie me just got introduced to .NET array methods by our shepherd Eric5h5. Tried System.Array.Copy, nearly made me cry with a mixture of joy at the prospect of not having to write a new function for each new class I'm resizing an array of, and frustration with having already typed so much useless code... I then tried System.Array.Resize(myArray, myArray.length+1); nope. Is my syntax wrong, or is there only a limited set of .NET methods which we can use in Unity's javascript? If so, where's the list? And are there any iOS compatibility issues? If yes, it would at least make my array manipulation skills less obsolete...

Thanks in advance!

Comment
Add comment · Show 2
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 syclamoth · Dec 13, 2011 at 11:24 PM 2
Share

Why not use lists? They have functionality that arrays do not. Arrays aren't really meant to be resized or anything- they're the simplest data structure.

avatar image Statement · Dec 13, 2011 at 11:32 PM 1
Share

I fully agree with @syclamoth that Lists are superior to arrays for dynamic size scenarios. I understand that you might want to fully grasp the methods coupled with arrays, but the brutal practical fact is that you'll be using these functions extremely rarely. I've worked professionally for 3-4 years and I've been program$$anonymous$$g C# for several years more and I've never had to use it, not even once. You'll be doing yourself a great favor if you move on to List, which is a typical class you'll be working with a lot.

1 Reply

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

Answer by Statement · Dec 13, 2011 at 11:37 PM

System.Array.Resize is a template method and the only way I can get it to work with JS is to explicitly define the template parameter. So yes, your syntax was a bit off there, but here's how it should look:

 var myArray : int[];
 System.Array.Resize.<int>(myArray, myArray.length + 1);
 print(myArray.length);

Given myArray is initially 5, running this script will print "6". I hope this little snippet is self explainable but if you have questions, just post a comment to this answer and I or someone else will get back to you.

The code you posted would generate this error message:

Assets/MyArray.js(2,20): BCE0023:

No appropriate version of 'System.Array.Resize' for the argument list '(int[], int)' was found.

The method clearly is there, it's just hidden as a template method (meaning you can replace the type of the array). The syntax for specifying the template parameter is added as .<T> to the method name, where T is the type of elements in the array.

Comment
Add comment · Show 9 · 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 gregzo · Dec 13, 2011 at 11:51 PM 0
Share

Thanks , I was expecting it had something to do with type, seeing that T, but didn't know the proper syntax... Are lists as fast as arrays? I need the code to be super efficient, as I'm resizing big 2d arrays quite often in a musical game based on user friendly, custom built sequencers, and sync needs to be perfect. What about iOS compatibility? No worries there? Will take a look at List anyway.

avatar image Statement · Dec 14, 2011 at 12:12 AM 2
Share

Arrays are faster in raw access speed, but lists will manage memory faster and more efficiently than resizing arrays frequently, as in your example. I answered a related topic a while ago. Arrays are faster, but read/write is such a small overhead these days. Whatever you are doing per object in an array is probably going to take significantly longer time to process making the difference negligible. It is not the raw access speed you should be concerned about, but what you are doing per object. Your memory usage patterns could benefit from special memory curves for instance if you frequently access objects left, right up and down an element in a 2d array. By organizing your objects in different ways you can benefit from your computers cache, avoiding cache misses and RA$$anonymous$$ lookups which can boost performance a lot. But the best optimization you can make is figuring out how you can make the least amount of work that you need to. For instance, if you wanted to check which object out of 10000 objects are closest to you, you'd naively check each object and keep track of the one that has the least distance to you. A better approach for this would be to divide and conquer the problem by grouping objects in a hierarchical manner and effectively cull (ignore) the objects that aren't anywhere near. Ins$$anonymous$$d of testing 10000 objects, you might end up testing only 10 objects. That is how optimization should be done, not deciding too much about lower detail stuff, but of course it adds to it. The question is how productive you'll be with everything super optimized and how much bang you get for the buck.

$$anonymous$$y bottom line is that you should try to use what's easy and readily available and start focusing on optimization once you notice that there is a pressing need for it (by using a profiler for example). The exception to this is if you're an experienced programmer and know for sure that a certain optimization is necessary to perform a certain task, then you probably are better off implementing the optimization ahead of time. But some people will disagree with me.

avatar image Eric5h5 · Dec 14, 2011 at 12:53 AM 2
Share

Note that the speed difference depends on the type used in the array. An int[] array, for example, was about 6X faster than List.<int> last I checked. However, a GameObject[] array is barely any different than List.<GameObject>. The simpler the type, the greater the speed difference.

Note also that Lists really just do System.Array.Resize on fixed-length arrays anyway when necessary--they start out with a capacity of 4 (unless otherwise specified), and keep doubling that any time the capacity is exceeded. They don't reduce capacity automatically, which is why the TrimExcess method exists (in case you had a really large list, then removed a lot of elements, and want to reclaim the memory). There's not actually such a thing as a true variable-length array in .NET, it's just that List creates the illusion of a variable-length array. So Lists aren't necessarily going to be better for memory than resizing arrays, but they save you the bother of having to do all that array management yourself, which usually results in better code.

avatar image Eric5h5 · Dec 14, 2011 at 01:06 AM 1
Share

Yes, that's what I said. ;)

avatar image gregzo · Dec 14, 2011 at 01:34 AM 1
Share

Thanks to all! I think one of the beginner's mistakes I am making is using array.length to keep track of how many objects I've got, hence all the resizing. From what I understand from your discussion, I could have a much bigger 2d array that I don't resize,extra variables to keep track of which parts of the array I'm iterating through, and that would be much more efficient than resizing all the time(which, you can laugh, I was doing thinking it would save resources). Then use Lists for complex classes such as gameObjects. Correct me if I'm wrong! I'm working on a music game/real time instrument, hence all the attention to optimization: framerate drop is my nemesis... So I'm trying to learn right! Thanks again

Show more comments

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

8 People are following this question.

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

Related Questions

Resizing arrays, and what is the default array type anyway? 3 Answers

Reference JS array of Objects = errors 1 Answer

Sorting an array of vectors in javascript 1 Answer

"For"-Loop only works for final element in array 1 Answer

resizing array[] erases its values to Vector3(0,0,0) 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