Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Selecting elements from array at specific indices

Selecting elements from array at specific indices

Scheduled Pinned Locked Moved C#
data-structurestutorialquestion
13 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Chesnokov Yuriy

    I was thinking about 1 line of code as in LINQ

    selected = ints.Select(i => i[ints]);

    Чесноков

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #4

    Once you got it I'll be curious to see it. In the mean time I just use foreach(int index in indices) slected.Add(ints[index]); which is one line of code anyway. :)

    Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

    Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

    1 Reply Last reply
    0
    • C Chesnokov Yuriy

      How to extract from array items at specific inidces?

      List ints;
      int[] indices;
      List slected = ints select items at 'indices';

      Чесноков

      W Offline
      W Offline
      Wayne Gaylard
      wrote on last edited by
      #5

      You can use the Where clause ie

      private void indices()
      {
      List<int> listInts = new List<int>() { 1, -23, 45, -723, 29, 49, -90, 628, 476, 39, -10, 30 };
      int[] indices = new int[] { 3, 5, 7, 9 };
      var selected = listInts.Where((i, index) => !indices.Contains(index));
      }

      That will do it.

      ...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....

      L 1 Reply Last reply
      0
      • W Wayne Gaylard

        You can use the Where clause ie

        private void indices()
        {
        List<int> listInts = new List<int>() { 1, -23, 45, -723, 29, 49, -90, 628, 476, 39, -10, 30 };
        int[] indices = new int[] { 3, 5, 7, 9 };
        var selected = listInts.Where((i, index) => !indices.Contains(index));
        }

        That will do it.

        ...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #6

        It may be compact and correct, but I don't like it much as it is an expensive, quadratic, operation: it will look for each element in the list of indices. :)

        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

        W 1 Reply Last reply
        0
        • L Luc Pattyn

          It may be compact and correct, but I don't like it much as it is an expensive, quadratic, operation: it will look for each element in the list of indices. :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          W Offline
          W Offline
          Wayne Gaylard
          wrote on last edited by
          #7

          As I stated, it will work, whether it is efficient is a completely different story. I prefer your method to be honest, but I also like to keep my hand in with these new fangled things MS keeps bringing in. Some of them are actually plenty useful, sometimes, but like all these abstractions that are creeping into our job, supposedly to make our lives easier, you have to weigh up the various options.

          ...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....

          C 1 Reply Last reply
          0
          • C Chesnokov Yuriy

            How to extract from array items at specific inidces?

            List ints;
            int[] indices;
            List slected = ints select items at 'indices';

            Чесноков

            A Offline
            A Offline
            AspDotNetDev
            wrote on last edited by
            #8

            List<int> selected = (from i in indices select ints[i]).ToList();

            [Managing Your JavaScript Library in ASP.NET]

            C 1 Reply Last reply
            0
            • W Wayne Gaylard

              As I stated, it will work, whether it is efficient is a completely different story. I prefer your method to be honest, but I also like to keep my hand in with these new fangled things MS keeps bringing in. Some of them are actually plenty useful, sometimes, but like all these abstractions that are creeping into our job, supposedly to make our lives easier, you have to weigh up the various options.

              ...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....

              C Offline
              C Offline
              Chesnokov Yuriy
              wrote on last edited by
              #9

              thanks for the hint, would you be able to solve assignement in one statement? there is array of ints and array of objects with public int MyValue property. the number of elements in both arrays are equal. How to assign those all ints to every object MyValue in one pass?

              for (int i = 0; i < ints.Length; i++)
              objects[i].MyValue = ints[i];

              Чесноков

              P A 2 Replies Last reply
              0
              • A AspDotNetDev

                List<int> selected = (from i in indices select ints[i]).ToList();

                [Managing Your JavaScript Library in ASP.NET]

                C Offline
                C Offline
                Chesnokov Yuriy
                wrote on last edited by
                #10

                thanks, would you solve another one? http://www.codeproject.com/Messages/3895665/Re-Selecting-elements-from-array-at-specific-indic.aspx[^]

                Чесноков

                1 Reply Last reply
                0
                • C Chesnokov Yuriy

                  thanks for the hint, would you be able to solve assignement in one statement? there is array of ints and array of objects with public int MyValue property. the number of elements in both arrays are equal. How to assign those all ints to every object MyValue in one pass?

                  for (int i = 0; i < ints.Length; i++)
                  objects[i].MyValue = ints[i];

                  Чесноков

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #11

                  Why on earth are you trying to solve this in one statement? The more you try to achieve that, the harder it's going to be for you to remember what your code actually does and how it works.

                  Forgive your enemies - it messes with their heads

                  My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                  C 1 Reply Last reply
                  0
                  • C Chesnokov Yuriy

                    thanks for the hint, would you be able to solve assignement in one statement? there is array of ints and array of objects with public int MyValue property. the number of elements in both arrays are equal. How to assign those all ints to every object MyValue in one pass?

                    for (int i = 0; i < ints.Length; i++)
                    objects[i].MyValue = ints[i];

                    Чесноков

                    A Offline
                    A Offline
                    AspDotNetDev
                    wrote on last edited by
                    #12

                    While I agree with Pete, here is how you would do that:

                    ints.Select((intValue, intIndex) => intIndex).ToList().ForEach((index) => objects[index].MyValue = ints[index]);

                    [Managing Your JavaScript Library in ASP.NET]

                    1 Reply Last reply
                    0
                    • P Pete OHanlon

                      Why on earth are you trying to solve this in one statement? The more you try to achieve that, the harder it's going to be for you to remember what your code actually does and how it works.

                      Forgive your enemies - it messes with their heads

                      My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                      C Offline
                      C Offline
                      Chesnokov Yuriy
                      wrote on last edited by
                      #13

                      just to practice LINQ queries

                      Чесноков

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups