Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8243401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:30:27+00:00 2026-06-07T21:30:27+00:00

Is there any way to automatically and fast swap elements with a build-in fast

  • 0

Is there any way to “automatically and fast” swap elements with a build-in fast method in a 2dimensional list, array oder dictionary in c#?

edit: to be more specific: i would not just swap 2 elements but change the order of maybe n elements / columns

I have something like this: a 2d datastracture with 10k lines (list with 10k elements) and each of those elements is again a list with 5 elements.(e.g. List of List of String>>).

  0 1 2 3 4 
0 A B C D E
1 A A A A A
2 d d c k x
3 ...
.
.
10000 ...

Is there any way to swap for all the 10k lines the element / column with index 0 and 2?

At first i would have on line 0: A B C D E and after swap: C B A D E

Or do i have to loop through all lines and swap by myself the element / column with index 0 and 2?

EDIT: In my businesslogic the 2d datastructure is going to be looped line for line and element for element and the values would be inserted somewhere. So my idea is not to loop foreach element, but to loop every line and the elements in a specific order. Since i do not want to access the elements in the order 0,1,2,3,4 but 2,1,0,3,4. Any concerns that accessing listelements in a specific order and not in a foreach manner would be slower? less performant?

edit: there is a link to java Collections.swap http://www.java-examples.com/swap-elements-java-arraylist-example

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:30:29+00:00Added an answer on June 7, 2026 at 9:30 pm

    I think there’s nothing builtin, but you can create your own extension easily:

    public static class ListExtensions
    {
        public static void Swap<T>(this IList<T> list, int index1, int index2)
        {
            T temp = list[index1];
            list[index1] = list[index2];
            list[index2] = temp;
        }
    }
    

    (based on Reeds Copseys approach: https://stackoverflow.com/a/1111039/284240)

    Here’s some sample data:

    var rnd = new Random();
    var hugeList = new List<List<int>>();
    for (int i = 0; i < 10000; i++)
    {
        var innerList = new List<int>();
        for (int ii = 0; ii < 5; ii++)
        {
            innerList.Add(rnd.Next(1,100000));
        }
        hugeList.Add(innerList);
    }
    

    Swap index 0 and 2 of all inner lists:

    foreach (var innerList in hugeList)
    { 
        innerList.Swap(0, 2);
    }
    

    Note that it works for lists and arrays of any type(since both implement IList<T>).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to automatically call a custom method when switching views usig
Is there any way to automatically create a trigger on creation of new table
Is there any way generating automatically a jax-rs client with HttpUrlConnection or third party
Is there any easy way to automatically deploy a web service / java web
Is there any way in which I can automatically convert a Custom Class Object
Is there any way to stop the callout from automatically appearing on an embedded
Is there any way to skip the builds getting kicked off automatically when certain
Is there a way to specify which namespace includes should be automatically added any
is there any way to automatically create hierachical menus from taxonomy vocabulary in Drupal
Is there any way to automatically create the constructor for a class based on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.