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. array help please

array help please

Scheduled Pinned Locked Moved C#
questioncsharpdata-structureshelp
11 Posts 3 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.
  • B Offline
    B Offline
    BDJones
    wrote on last edited by
    #1

    Sorry, I'm new to C# so please forgive the ignorance... I have a list, and want to make another list from the first list: int[] list1 = { 1, 3, 5, 7, 9 }; int[,] list2; for (int i = 0; i < list1.Length; i++) { list2[i] = [item from list1] , [i]; } How do i put items in list2? I'm looking for: {{1,0} {3,2} {5,3} {7,4} {9,5}} Thank you for your time.

    L C 2 Replies Last reply
    0
    • B BDJones

      Sorry, I'm new to C# so please forgive the ignorance... I have a list, and want to make another list from the first list: int[] list1 = { 1, 3, 5, 7, 9 }; int[,] list2; for (int i = 0; i < list1.Length; i++) { list2[i] = [item from list1] , [i]; } How do i put items in list2? I'm looking for: {{1,0} {3,2} {5,3} {7,4} {9,5}} Thank you for your time.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      What are you really looking for? Your example does not make sense to me.

      B 1 Reply Last reply
      0
      • B BDJones

        Sorry, I'm new to C# so please forgive the ignorance... I have a list, and want to make another list from the first list: int[] list1 = { 1, 3, 5, 7, 9 }; int[,] list2; for (int i = 0; i < list1.Length; i++) { list2[i] = [item from list1] , [i]; } How do i put items in list2? I'm looking for: {{1,0} {3,2} {5,3} {7,4} {9,5}} Thank you for your time.

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        int[,] gives you a two dimensional list, not a list of pairs of numbers. int[3,3] gives you a grid of 9 values, all of them a single int. To store pairs you'd need to define a struct, or you could use a map if you wanted ( which pairs values and lets you look up one value based on the other instead of an index ). I suspect that you're just experimenting, b/c I can't see any use for your final example ( the second value is always the same as the array index, so you have access to that number all the time anyhow ). Imagine I had a tic tac toe board for a game:

        | |
        ___________
        | |
        ___________
        | |

        Now - I can create a grid that's 3x3 with List[3,3] and I can use co-ordinates to look up positions in that list and track the game positions. That's the sort of thing a 2D array is used for.

        Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

        B 1 Reply Last reply
        0
        • L Lost User

          What are you really looking for? Your example does not make sense to me.

          B Offline
          B Offline
          BDJones
          wrote on last edited by
          #4

          I have this list: int[] list1 = { 1, 3, 5, 7, 9 }; Using 'for', how do I create the following list using the first list? {{1,0} {3,2} {5,3} {7,4} {9,5}} Thanks.

          L 1 Reply Last reply
          0
          • C Christian Graus

            int[,] gives you a two dimensional list, not a list of pairs of numbers. int[3,3] gives you a grid of 9 values, all of them a single int. To store pairs you'd need to define a struct, or you could use a map if you wanted ( which pairs values and lets you look up one value based on the other instead of an index ). I suspect that you're just experimenting, b/c I can't see any use for your final example ( the second value is always the same as the array index, so you have access to that number all the time anyhow ). Imagine I had a tic tac toe board for a game:

            | |
            ___________
            | |
            ___________
            | |

            Now - I can create a grid that's 3x3 with List[3,3] and I can use co-ordinates to look up positions in that list and track the game positions. That's the sort of thing a 2D array is used for.

            Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

            B Offline
            B Offline
            BDJones
            wrote on last edited by
            #5

            Thank you. I want to get: {{1,0} {3,2} {5,3} {7,4} {9,5}} from an original list of: int [] list1 = { 1, 3, 5, 7, 9 }; using the increment in 'for' as the second number

            C 1 Reply Last reply
            0
            • B BDJones

              I have this list: int[] list1 = { 1, 3, 5, 7, 9 }; Using 'for', how do I create the following list using the first list? {{1,0} {3,2} {5,3} {7,4} {9,5}} Thanks.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              The second elements of the sub-array's in your second array does not make enough sense and does not match your description. {{1,0} {3,1} {5,2} {7,3} {9,4}} would make sense and match your description.

              B 1 Reply Last reply
              0
              • L Lost User

                The second elements of the sub-array's in your second array does not make enough sense and does not match your description. {{1,0} {3,1} {5,2} {7,3} {9,4}} would make sense and match your description.

                B Offline
                B Offline
                BDJones
                wrote on last edited by
                #7

                I'm sorry all, my example was poor. Yes, this is what is needed. I can't figure out the syntax needed inside 'for'. Could you help me please?

                L 1 Reply Last reply
                0
                • B BDJones

                  I'm sorry all, my example was poor. Yes, this is what is needed. I can't figure out the syntax needed inside 'for'. Could you help me please?

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  warning: untested

                  int[] list1 = { 1, 3, 5, 7, 9 };
                  int[,] list2 = new int[5,2];

                  for (int i = 0; i < list1.Length; i++)
                  {
                  list2[i, 0] = list1[i];
                  list2[i, 1] = i;
                  }

                  B 1 Reply Last reply
                  0
                  • B BDJones

                    Thank you. I want to get: {{1,0} {3,2} {5,3} {7,4} {9,5}} from an original list of: int [] list1 = { 1, 3, 5, 7, 9 }; using the increment in 'for' as the second number

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #9

                    Then you need to define a struct that contains those two numbers and build a list of them. struct myStruct { public int value; public int index; } will work just fine. Of course then you need to write your own code if you want to search or sort the list.

                    Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                    1 Reply Last reply
                    0
                    • L Lost User

                      warning: untested

                      int[] list1 = { 1, 3, 5, 7, 9 };
                      int[,] list2 = new int[5,2];

                      for (int i = 0; i < list1.Length; i++)
                      {
                      list2[i, 0] = list1[i];
                      list2[i, 1] = i;
                      }

                      B Offline
                      B Offline
                      BDJones
                      wrote on last edited by
                      #10

                      Thank you. I wasn't even close & wasn't aware of 'code block' which scrambled my example. Thanks again for your patience.

                      L 1 Reply Last reply
                      0
                      • B BDJones

                        Thank you. I wasn't even close & wasn't aware of 'code block' which scrambled my example. Thanks again for your patience.

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        You're welcome

                        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