Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Vollmondum · Jul 18, 2015 at 04:52 PM · arrayint

How to exclude elements from int[]

I'm using Mathf.Min within int[] variable.

Is there a way to find minimum, but more than 0?

And second question: How to exclude all elements that are "0" from my array (or whatever it's called when using int[]).

I found some statements like removeAt or push, but those don't work with int[].

Thank you.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Eudaimonium · Jul 18, 2015 at 05:33 PM

Arrays are bit tricky. Any particular reason why are you using integer array, instead of Integer Lists?

If you create,

 List<int> myList = new List<int>();

then you can do: myList.Minimum, Sort, Reverse, RemoveAt, InsertAt, and tons of other cool functions.

If you insist on using int array, then you just gotta do it all by hand.

First use one For loop to count all zeroes in your array. Create another int array that is the size of original array minus zero counts.

Second for loop that copies over original array to new array, except when element is zero.

 int[] original = //assumed
 int[] newArray;
 
 int zeroCounter;
 int minimumInArray = int.maxValue;
 
 for(int i = 0; i<original.Length; i++)
 {
     if (original[i] > 0 && original[i] < minimumInArray)
     minimumInArray = original[i];
 
     if (original[i] == 0)
     zeroCounter++;
 }
 
 newArray = new int[original.Length - zeroCounter];
 
 int index = 0;
 for(int i = 0; i<original.Length; i++)
 {
     if (original[i] != 0)
     newArray[index++] = original[i];
 }
 
 //At this point:
 //minimumInArray contains the lowest value from original array that is not zero
 //newArray contains all values from original except zero elements.


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 Vollmondum · Jul 19, 2015 at 04:40 AM 0
Share

Nice, thanks! I missed that zeroCounter part :)

As for my particular reason: I have too many intersected scripts using int[], that it's way too complex to switch everything back to Lists. But I'll probably remake it later.

avatar image Eudaimonium · Jul 19, 2015 at 08:37 AM 0
Share

O$$anonymous$$ cool, I hope the code snippet helped.

avatar image _Gkxd · Jul 19, 2015 at 04:25 PM 0
Share

I'd like to mention that System.Linq has a lot of useful list functions too. For future reference, the operation of excluding certain elements form a list is commonly known as "filter," and the operation of combining all the elements in a list (finding $$anonymous$$, max, sum, etc.) is commonly known as "reduce." Here, they're renamed to "where" and "aggregate," respectively.

Here is an example of using Where to filter a list: https://msdn.microsoft.com/en-us/library/vstudio/bb549418(v=vs.100).aspx

From that page, it seems that Linq does work with arrays, so put using System.Linq; at the top of your script and you can do something like this:

 int[] myIntArray = /* Some numbers */;
 int[] myIntArrayPositive = myIntArray.Where((number, index) => number > 0);
 int $$anonymous$$imumPositive = myIntArrayPositive.Aggregate((numA, numB) => $$anonymous$$athf.$$anonymous$$in(numA, numB));

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

JS: Array index not taking array.length as valid int? 2 Answers

Checking to see if a loaded level is equal to a member of an integer array 1 Answer

get int based on 3 ints 0 Answers

Extracting Numbers from String Place JS 1 Answer

How do you calculate walls between tiles in a 3d environment? 5 Answers


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