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
3
Question by sdgd · Jan 25, 2013 at 09:34 PM · c#addgeneric.listspecific position

generic list how to add item at specific point C#

ok I've figured that I'm able to remove item at specific point but how do I add it back to specific point?

and equip.count doesn't seem to work some said that like that I'm able to tell what's the max number of items in specific generic list

code is C#

Item is class

 public List<Item> equip ;
     void Start () {
     equip.Capacity = 2;
         for (int g = 0; g < 8; g++) {
             equip.Add(new Item(g));
         }
         // equip.RemoveAt(5);
         equip[5] = (new Item(5));
     }

ok I've worked around a bit made that equip at 5 is = to what I want instead to delete it and putting new one in it

but if someone knows how to put 1 inside of in the middle of list I'd be happy to hear it

like deleting at 5 and giving 5 at 5 so 6 isn't named as 5

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 Wolfram · Jan 25, 2013 at 10:36 PM 0
Share

Note the code you posted does nothing special with element 5 - you start from an empty list, keep adding items that are placed at the index of their "identifier". So after the "for" loop, equip[5] will already contain Item(5). The assigment does nothing except replacing Item(5) with a new copy of Item(5).

avatar image ATMEthan · Jan 25, 2013 at 10:43 PM 0
Share

Also note that using equip[5] = (new Item(5)); overwrites whatever is in position 5. To insert and increment all the indices and their values you'll have to use the insert like below or override the [] operator. But I'm not sure if that's possible with lists.

avatar image Wolfram · Jan 25, 2013 at 10:53 PM 0
Share

Ins$$anonymous$$d of your usage of Capacity, create/initialize a new list with:

 equip=new List< Item>();

4 Replies

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

Answer by AndrewGrayGames · Jan 25, 2013 at 11:02 PM

For the List generic, the key operations I use most often:

 List<T> someList = new List();
 someList.Add(x)        // Adds x to the end of the list
 someList.Insert(0, x)  // Adds x at the given index
 someList.Remove(x)     // Removes the first x observed
 someList.RemoveAt(0)   // Removes the item at the given index
 someList.Count()       // Always good to know how many elements you have!
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 sdgd · Jan 25, 2013 at 11:31 PM 0
Share

wow gr8 your post is even more telling stuff

avatar image Wolfram · Jan 25, 2013 at 11:35 PM -1
Share

Umh, the link @rajabala gave you tells you the same, and more...

avatar image N-8-D-e-v · Jun 20, 2020 at 07:06 PM 1
Share

Perfect answer, just what I needed!

avatar image
6

Answer by raja-bala · Jan 25, 2013 at 10:04 PM

You should use the Insert method of the List class to be able to add an item at a particular position. Basic syntax is: listName.Insert(position, item);

A few caveats: When you insert the item at a particular position, the items following it have to be adjusted which results in a lot of copy operations. You should use LinkedList instead for the performance benefit. Do not use capacity if you don't know the number of elements your list will have beforehand.

Have a look at: http://www.dotnetperls.com/list-insert and http://www.dotnetperls.com/list

Comment
Add comment · Show 5 · 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 sdgd · Jan 25, 2013 at 10:29 PM 0
Share

hm thanks works perfectly as I wanted

but for Equip I don't care for performance as it won't be often copyed and thanks for notifying me about performance

used:

equip.Insert(5,new Item(5));

avatar image Wolfram · Jan 25, 2013 at 10:46 PM 0
Share

Deleting and inserting entries will still mess with the remaining indices, which you would need to keep track of to reinsert elements at their correctly ordered position. For example, if you removed entries 2 and 4, equip[5] will point to Item(7); and how do you know at which index you'll need to reinsert a new Item(4)? (which would be at 3, in this case)

What are you actually trying to accomplish?

avatar image sdgd · Jan 25, 2013 at 11:31 PM 0
Share

I'm trying to accomplish learning

IF I'd post my full code 2 pages you'll be bugging your self for 2 days what I want

if I understand simple stuff I can do complicated as well

if I'm not able doing simple stuff than I'll always need to workaround the complicated ones

avatar image Wolfram · Jan 25, 2013 at 11:39 PM 0
Share

That's not what I meant. What are you trying to do with your equipment list, how should it behave? Do the Items have parameters that can change, or are they always the same? Can you only have exactly one item of each type? And so on.

The approach you are taking may work in the end, once you solved your open question - but it may not be the best approach to accomplish what you actually want to do. If you explain that to us, we can help you find the best approach ins$$anonymous$$d.

avatar image GamerSuji · May 12, 2017 at 01:30 AM 0
Share

Why is this not the best answer , Thanks @raja-bala !

avatar image
0

Answer by ATMEthan · Jan 25, 2013 at 11:03 PM

I think you are looking for the insert method. You'll have to save the index of where you want to insert the new Item. Something like this:

         int magicNumbersAreBad = 8;
         int desiredPosition;
         for(int index = 0; index < magicNumbersAreBad; index ++) {
             equip.Add(new Item(null, null, "sadda", 1, 10, 1, g));
             if(index == 5)//the specific spot you want to insert your new Item
                desiredPosition = index;
         }
         equip.RemoveAt(5);
         equip.Insert(desiredPosition, (ew Item(null,null,"asdf",1,10,1,desiredPosition?)));
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
0

Answer by Wolfram · Jan 25, 2013 at 10:47 PM

To answer your question, replace the reference in your list with null. This will "remove" the item from the list, but won't change the list itself or the order of the remaining entries. When processing the list, just test the enries for null before trying to access them. To "reinsert" an entry, you can use the assignment you made above, equip[5] = new Item(5);

Otherwise, see @rajabala's answer.

If the items themselves are not modified, you could also use a fixed array as a "pool" of objects, which you would only have to create once at initialization.

Then use a parallel bool[] to flag whether a particular item is available or not.

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

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

14 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 avatar image avatar image avatar image

Related Questions

Adding lights and cameras to scenes with cSharp script 2 Answers

How do I create a Camera and make it mainCamera with csharp script ? 1 Answer

How to use list to add new items & function? 0 Answers

Convert this Javascript into C#? 1 Answer

Could this be used with js? 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