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
1
Question by DayyanSisson · Apr 08, 2015 at 05:44 PM · c#listsswapswapping

Swapping Items in List Replaces Value Instead

Hey all. I'm trying to swap two items in a list and the values just start replacing each other versus actually switching. For example:

 List<int> list = new List<int>();
 
 list.Add(1);
 list.Add(7);
 list.Add(8);
 list.Add(15);
 
 int temp = list[2];
 list[2] = list[3];
 list[3] = temp;

Theoretically the list should print [1, 7, 15, 8] but instead it prints [1, 7, 15, 15]. I assume its because temp is referencing list[2] so when list[2] becomes 15, so does temp. Anyone know why that is, or how to step around it?

I've looked around pretty earnestly for a solution to this on the web and couldn't find one, which is surprising since this seems like a pretty basic problem. If there is an answer somewhere, a redirect would work just fine :D

If not, an answer here would be appreciated as well. Thanks in advance.

Comment
Add comment · Show 1
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 lordlycastle · Apr 08, 2015 at 06:14 PM 0
Share

I just run this

 List<int> list = new List<int>();
 list.Add(0);
 list.Add(1);
 list.Add(2);
         
 int temp = list[1];
 list[1] = list[2];
 list[2] = temp;
 for (int i = 0; i < list.Count; i++) // Loop with for.
 {
     Console.WriteLine(list[]);
 }

And the output was 0 2 1. Could you be printing the list value before 3rd one is assigned to temp.

1 Reply

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

Answer by DoTA_KAMIKADzE · Apr 08, 2015 at 06:01 PM

You're doing something else with your list apart of the code above.

I've even run it in a single run just to show you the result:

 List<int> list = new List<int>();
 list.Add(1);
 list.Add(7);
 list.Add(8);
 list.Add(15);
 int temp = list[2];
 list[2] = list[3];
 list[3] = temp;
 foreach (int ila in list) Debug.Log(ila);

Result:

alt text

And that^ is an expected behavior.

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 DayyanSisson · Apr 08, 2015 at 06:31 PM 0
Share

Actually you're correct, I was storing it in another variable as such:

 if(list[parent] >= insert){
     int temp = list[parent];
     list[parent] = insert;
     insert = temp;
     index = parent;
 }else{
     ...

Ins$$anonymous$$d of actually changing the values of the list items. Don't know why I overlooked that.

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

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to check if a value exists in an array (C#) 3 Answers

Make Lists within a List 0 Answers

How do i activate a function attached to a listed GameObject from the perspective of itself? 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