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. Visual Basic
  4. How to add items to the beginning of an array

How to add items to the beginning of an array

Scheduled Pinned Locked Moved Visual Basic
data-structureshelptutorialquestion
11 Posts 7 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.
  • D Offline
    D Offline
    darthjimmy138
    wrote on last edited by
    #1

    I want to add a string to the beginning of my array and still keep the existing strings in the array. I'm thinking it has something to do with moving the existing items down. Can anyone help me with this?

    L L G 3 Replies Last reply
    0
    • D darthjimmy138

      I want to add a string to the beginning of my array and still keep the existing strings in the array. I'm thinking it has something to do with moving the existing items down. Can anyone help me with this?

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

      You could move all the items in the array down one by one, but performance-wise that's probably not the best way. You could reverse[^] the array, add the new item, and then reverse the array again, which, depending upon arrays are implemented in .Net, may or may not provide any better performance. If top performance is a priority, use a linked list[^] or Data Structures : Part 1 - Singly Linked Lists[^][^].

      1 Reply Last reply
      0
      • D darthjimmy138

        I want to add a string to the beginning of my array and still keep the existing strings in the array. I'm thinking it has something to do with moving the existing items down. Can anyone help me with this?

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

        Hi, an array can't do that without a performance hit, since all its data has to be moved. That is exactly why they invented all those other fancy data structures, such as linked lists. Now linked lists may be too expensive for your needs (depends on how often you want to prefix something to a loaded array). If only seldom, I would suggest a linked list of arrays, i.e. each node in the linked list contains an array. So you would start off with a single node containing the original array, then prefix one node with the one new data item in a single-element array. Of course, whatever you choose, if it is not an array, then you may not get some of the nice features an array offers, such as fast indexing, automatic sorting, reversing, etc. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Voting for dummies? No thanks. X|


        1 Reply Last reply
        0
        • D darthjimmy138

          I want to add a string to the beginning of my array and still keep the existing strings in the array. I'm thinking it has something to do with moving the existing items down. Can anyone help me with this?

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          You can't add anything to an array, as it can't be resized. You would have to create a new array and copy all the data from the current array. For a collection with a dynamic size, you should use something like a List(Of String) instead. Although you can insert an item at the beginning of a list, you should consider adding it at the end of the list intead, as that doesn't mean that every item in the list has to be moved. Which end of a list is which, only depends on how you look on it. You can just as easily loop from the last item to the first, in which case the last added items comes first. Perhaps even a Queue(Of String) would be better for what you are doing.

          Despite everything, the person most likely to be fooling you next is yourself.

          C 1 Reply Last reply
          0
          • G Guffa

            You can't add anything to an array, as it can't be resized. You would have to create a new array and copy all the data from the current array. For a collection with a dynamic size, you should use something like a List(Of String) instead. Although you can insert an item at the beginning of a list, you should consider adding it at the end of the list intead, as that doesn't mean that every item in the list has to be moved. Which end of a list is which, only depends on how you look on it. You can just as easily loop from the last item to the first, in which case the last added items comes first. Perhaps even a Queue(Of String) would be better for what you are doing.

            Despite everything, the person most likely to be fooling you next is yourself.

            C Offline
            C Offline
            Chinners
            wrote on last edited by
            #5

            Just a quick note - an array can be resized :o) dim Arr(10) as string redim Arr(100) (works, clearing array first) redim preserve arr(100) works, preserving data... However, I agree a list would be a better choice. You can always dump it back to an array using the .ToArray function of the variable: dim L as list(of string) dim Arr() as string=l.ToArray. Mind you, I guess this would take quite a hefty performance hit if doing it too often.

            M G 2 Replies Last reply
            0
            • C Chinners

              Just a quick note - an array can be resized :o) dim Arr(10) as string redim Arr(100) (works, clearing array first) redim preserve arr(100) works, preserving data... However, I agree a list would be a better choice. You can always dump it back to an array using the .ToArray function of the variable: dim L as list(of string) dim Arr() as string=l.ToArray. Mind you, I guess this would take quite a hefty performance hit if doing it too often.

              M Offline
              M Offline
              Mark Churchill
              wrote on last edited by
              #6

              I'm pretty sure you'll find that redim creates a new array and (optionally) copies the old data in. Logically an array can't be increased if it's packed amongst other stuff in memory without moving it anyway.

              Mark Churchill Director Dunn & Churchill Free Download:
              Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.

              1 Reply Last reply
              0
              • C Chinners

                Just a quick note - an array can be resized :o) dim Arr(10) as string redim Arr(100) (works, clearing array first) redim preserve arr(100) works, preserving data... However, I agree a list would be a better choice. You can always dump it back to an array using the .ToArray function of the variable: dim L as list(of string) dim Arr() as string=l.ToArray. Mind you, I guess this would take quite a hefty performance hit if doing it too often.

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                Jasey9 wrote:

                Just a quick note - an array can be resized [Red faced] ) dim Arr(10) as string redim Arr(100) (works, clearing array first) redim preserve arr(100) works, preserving data...

                No, it can't be resized. The ReDim Preserve command creates a new array and copies the data from the original array to it.

                Despite everything, the person most likely to be fooling you next is yourself.

                C 1 Reply Last reply
                0
                • G Guffa

                  Jasey9 wrote:

                  Just a quick note - an array can be resized [Red faced] ) dim Arr(10) as string redim Arr(100) (works, clearing array first) redim preserve arr(100) works, preserving data...

                  No, it can't be resized. The ReDim Preserve command creates a new array and copies the data from the original array to it.

                  Despite everything, the person most likely to be fooling you next is yourself.

                  C Offline
                  C Offline
                  Chinners
                  wrote on last edited by
                  #8

                  Yep. That is how VB resizes an array. I never said it was efficient. I was responding to the statement saying you can’t resize an array. The original statement inferred it was a manual job to copy data from one array to another. However, from a runtime point of view, the array has been resized. It is not be the most efficient thing to do (well, it may be more efficient on small arrays, but then I am possibly overcomplicating things :)), But the end result of a redim will be to have changed the upper bound of the array. I would never use this sort of "feature" in a loop with lots of elements in the array, but for an occasional hit, especially if the rest of the app written is geared towards arrays, then I would say it is appropriate. I did, however suggest that a list would be more efficient... It was just the word "CANT" I didnt like to see :)

                  D 1 Reply Last reply
                  0
                  • C Chinners

                    Yep. That is how VB resizes an array. I never said it was efficient. I was responding to the statement saying you can’t resize an array. The original statement inferred it was a manual job to copy data from one array to another. However, from a runtime point of view, the array has been resized. It is not be the most efficient thing to do (well, it may be more efficient on small arrays, but then I am possibly overcomplicating things :)), But the end result of a redim will be to have changed the upper bound of the array. I would never use this sort of "feature" in a loop with lots of elements in the array, but for an occasional hit, especially if the rest of the app written is geared towards arrays, then I would say it is appropriate. I did, however suggest that a list would be more efficient... It was just the word "CANT" I didnt like to see :)

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #9

                    Jasey9 wrote:

                    I was responding to the statement saying you can’t resize an array

                    You can't. Array's dimensions are immutable. Creating a new array and copying the data is NOT resizing an existing array. It's mearly creating a new one using a very expensive process. This is not an ability unique to VB since it can be done in just about any language. The only advantage VB has over some other languages is that it's built into the VB runtime and it supply syntactic sugar to relieve you the stress of coding this little function yourself.

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                         2006, 2007, 2008

                    C 1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      Jasey9 wrote:

                      I was responding to the statement saying you can’t resize an array

                      You can't. Array's dimensions are immutable. Creating a new array and copying the data is NOT resizing an existing array. It's mearly creating a new one using a very expensive process. This is not an ability unique to VB since it can be done in just about any language. The only advantage VB has over some other languages is that it's built into the VB runtime and it supply syntactic sugar to relieve you the stress of coding this little function yourself.

                      A guide to posting questions on CodeProject[^]
                      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                           2006, 2007, 2008

                      C Offline
                      C Offline
                      Chinners
                      wrote on last edited by
                      #10

                      "The only advantage VB has over some other languages is that it's built into the VB runtime" Not sure how to quote, but the above says what I mean. You know, I know, advanced programmers know that behind the scenes, a lot is going on (although, if the compiler has any sense it will be reduced to a "REP MOVSD" command in the end, which is very quick in small data situations). "It's mearly creating a new one using a very expensive process" The point I am trying to make is that in VB, there are commands that appear to resize an array. How this is done, to an average user, doesn't matter. I mean, if we were all looking for the "least expensive" ways of doing stuff, we would do most stuff in raw assembler. Sure, it will take more time to develop, but code costs running times would be reduced. I am not meaning to start any arguments here, all I am saying is that the dimentions of an array can be changed. There are many more, optimal ways of achiving the same thing... but increasing an array using "redim" is cheaper, in terms of man hours, than developing a proper linked list. VB does hide all the inefficiences of the procedure, but to the end user, the array has been resized

                      D 1 Reply Last reply
                      0
                      • C Chinners

                        "The only advantage VB has over some other languages is that it's built into the VB runtime" Not sure how to quote, but the above says what I mean. You know, I know, advanced programmers know that behind the scenes, a lot is going on (although, if the compiler has any sense it will be reduced to a "REP MOVSD" command in the end, which is very quick in small data situations). "It's mearly creating a new one using a very expensive process" The point I am trying to make is that in VB, there are commands that appear to resize an array. How this is done, to an average user, doesn't matter. I mean, if we were all looking for the "least expensive" ways of doing stuff, we would do most stuff in raw assembler. Sure, it will take more time to develop, but code costs running times would be reduced. I am not meaning to start any arguments here, all I am saying is that the dimentions of an array can be changed. There are many more, optimal ways of achiving the same thing... but increasing an array using "redim" is cheaper, in terms of man hours, than developing a proper linked list. VB does hide all the inefficiences of the procedure, but to the end user, the array has been resized

                        D Offline
                        D Offline
                        Dave Kreskowiak
                        wrote on last edited by
                        #11

                        Jasey9 wrote:

                        but to the end user, the array has been resized

                        More properly, the end user gets the illusion of a array resized array. Talking intelligently about the process, as you've no doubt have found in this thread, requires that you understand and use specific descriptions of processes. Even though VB, regretably, hides the true inner workings of such a function, this does NOT relieve the programmer from understanding what is truely going on behind the scenes. This is the one drawback to VB that causes the C, C++, C#, (insert flavor here) programmers to belittle VB programmers, saying that they don't know what they're doing.

                        A guide to posting questions on CodeProject[^]
                        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                             2006, 2007, 2008

                        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