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 Management

Array Management

Scheduled Pinned Locked Moved C#
questioncsharpdatabasedata-structureshelp
17 Posts 10 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 DwR

    Hi, I'm new to C# and am having an issue understanding how to easily handle something that should be straightforward. That is, I have an array of a fixed length. At each iteration a new double is added to the array and the oldest is to be removed. How do I do this, and re-index all of the other elements in the array accordingly, without rebuilding the entire array each time? Regards, Dave

    S Offline
    S Offline
    Sajjad Izadi
    wrote on last edited by
    #5

    int i=0; try // you can try if there is an array index and catch it if you are going out of your array bound { for(i=0;i { //it replaces the next index with the current index doubleArray[i]=doubleArray[i+1]; } } // if you call doubleArray[doubleArray.Legnth], actully you have called an out of bound index, so you have made an exception and you can solve it with a try-catch statement catch { //now your newValue is replace as doubleArray[doubleArray.Legnth-1] doubleArray[i]=newValue; }

    M D S 3 Replies Last reply
    0
    • D DwR

      New values are time dependant. So, the first entry in the array is always the oldest.

      Regards, Dave

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #6

      A queue will be best here, refer to Ashfield's reply.

      xacc.ide - now with TabsToSpaces support
      IronScheme - 1.0 alpha 4a out now (29 May 2008)

      1 Reply Last reply
      0
      • D DwR

        Hi, I'm new to C# and am having an issue understanding how to easily handle something that should be straightforward. That is, I have an array of a fixed length. At each iteration a new double is added to the array and the oldest is to be removed. How do I do this, and re-index all of the other elements in the array accordingly, without rebuilding the entire array each time? Regards, Dave

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

        You can use a cyclic buffer by keeping a start index pointing to the first item in the array. When adding an item you just advance the start index to shift all items in the array. You can wrap it in a generic class, something like this:

        public class CyclicArray<T> {
        private T[] _data;
        private int _start;
        public CyclicArray(int size){ _data = new T[size]; _start = -1;}
        public int Length { get { return _data.Length; } }
        public T this[int index] { get { return _data[(index + _start) % _data.Length]; } }
        public void Add(T item) { _start++; this[-1] = item;}
        }

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

        1 Reply Last reply
        0
        • S Sajjad Izadi

          int i=0; try // you can try if there is an array index and catch it if you are going out of your array bound { for(i=0;i { //it replaces the next index with the current index doubleArray[i]=doubleArray[i+1]; } } // if you call doubleArray[doubleArray.Legnth], actully you have called an out of bound index, so you have made an exception and you can solve it with a try-catch statement catch { //now your newValue is replace as doubleArray[doubleArray.Legnth-1] doubleArray[i]=newValue; }

          M Offline
          M Offline
          moon_stick
          wrote on last edited by
          #8

          Sajjad Izadi wrote:

          for(i=0;i { //it replaces the next index with the current index doubleArray[i]=doubleArray[i+1]; }

          Urgh!

          It definitely isn't definatley

          1 Reply Last reply
          0
          • D DwR

            Hi, I'm new to C# and am having an issue understanding how to easily handle something that should be straightforward. That is, I have an array of a fixed length. At each iteration a new double is added to the array and the oldest is to be removed. How do I do this, and re-index all of the other elements in the array accordingly, without rebuilding the entire array each time? Regards, Dave

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #9

            Why don't you use a generic list? When you add a new item, simply remove the 0th one.

            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

            L 1 Reply Last reply
            0
            • realJSOPR realJSOP

              Why don't you use a generic list? When you add a new item, simply remove the 0th one.

              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #10

              Now that would not be very efficient! But it will work :)

              xacc.ide - now with TabsToSpaces support
              IronScheme - 1.0 alpha 4a out now (29 May 2008)

              realJSOPR 1 Reply Last reply
              0
              • D DwR

                Hi, I'm new to C# and am having an issue understanding how to easily handle something that should be straightforward. That is, I have an array of a fixed length. At each iteration a new double is added to the array and the oldest is to be removed. How do I do this, and re-index all of the other elements in the array accordingly, without rebuilding the entire array each time? Regards, Dave

                B Offline
                B Offline
                Bhumikabarot
                wrote on last edited by
                #11

                There is no need to rebuild an array each time.U do one thing u take stringbuilder class's object and use it. Which will allow you to take an array of any length.

                Bhumika

                D 1 Reply Last reply
                0
                • L leppie

                  Now that would not be very efficient! But it will work :)

                  xacc.ide - now with TabsToSpaces support
                  IronScheme - 1.0 alpha 4a out now (29 May 2008)

                  realJSOPR Offline
                  realJSOPR Offline
                  realJSOP
                  wrote on last edited by
                  #12

                  leppie wrote:

                  Now that would not be very efficient! But it will work

                  All the good answers were taken.

                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                  -----
                  "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                  L 1 Reply Last reply
                  0
                  • realJSOPR realJSOP

                    leppie wrote:

                    Now that would not be very efficient! But it will work

                    All the good answers were taken.

                    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                    -----
                    "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                    L Offline
                    L Offline
                    leppie
                    wrote on last edited by
                    #13

                    HAHAHA :)

                    xacc.ide - now with TabsToSpaces support
                    IronScheme - 1.0 alpha 4a out now (29 May 2008)

                    1 Reply Last reply
                    0
                    • B Bhumikabarot

                      There is no need to rebuild an array each time.U do one thing u take stringbuilder class's object and use it. Which will allow you to take an array of any length.

                      Bhumika

                      D Offline
                      D Offline
                      DwR
                      wrote on last edited by
                      #14

                      Hi, I'm during numerical calculations here. The array is comprised of doubles and not strings.

                      Regards, Dave

                      1 Reply Last reply
                      0
                      • S Sajjad Izadi

                        int i=0; try // you can try if there is an array index and catch it if you are going out of your array bound { for(i=0;i { //it replaces the next index with the current index doubleArray[i]=doubleArray[i+1]; } } // if you call doubleArray[doubleArray.Legnth], actully you have called an out of bound index, so you have made an exception and you can solve it with a try-catch statement catch { //now your newValue is replace as doubleArray[doubleArray.Legnth-1] doubleArray[i]=newValue; }

                        D Offline
                        D Offline
                        DaveyM69
                        wrote on last edited by
                        #15

                        Is it just me or is this a REALLY bad idea - deliberatley causing an exception to be thrown? Throwing an exception in an object is acceptable if a value passed is outside acceptable limts or whatever (although it's normally better to revert to a defalut value and raise an event), but deliberately causing an exception? Yuck!

                        Dave

                        R 1 Reply Last reply
                        0
                        • D DaveyM69

                          Is it just me or is this a REALLY bad idea - deliberatley causing an exception to be thrown? Throwing an exception in an object is acceptable if a value passed is outside acceptable limts or whatever (although it's normally better to revert to a defalut value and raise an event), but deliberately causing an exception? Yuck!

                          Dave

                          R Offline
                          R Offline
                          Robert C Cartaino
                          wrote on last edited by
                          #16

                          DaveyM69 wrote:

                          Is it just me or is this a REALLY bad idea - deliberatley causing an exception to be thrown?

                          No, It is not just you. I think it is bad design to use exceptions to handle the "normal and expected" flow of execution. When exceptions where first added to the C++, I started seeing code like:

                          open file;
                          try
                          {
                          loop forever:
                          read line from file;
                          }
                          catch
                          {
                          // oops, we must have reached the end of the file
                          }

                          It not usually a good idea to use exceptions to manage the normal flow of execution for a couple of reasons. First, exceptions are expensive to handle so you will almost certainly degrade performance. Also, sprinkling try statements all over the place makes code difficult to read. Conceptually, the whole point of an "exception" is to indicate an exceptional condition (i.e. Out-of-memory) that cannot be easily handled at the time/place the error condition is reached. The whole idea of an exception should be to pass control of the program to a place that can handle the error condition. If you are using exceptions in lieu proper bounds checking or verifying the return value of a method or API call, then, in reality, exceptions are just a high-tech way of implementing a GOTO which can leave your system in an undefined state. These are generalizations and "exception-neutral programming" outlines acceptable techniques for the best use of exceptions (See Exceptional C++, Herb Sutter).

                          1 Reply Last reply
                          0
                          • S Sajjad Izadi

                            int i=0; try // you can try if there is an array index and catch it if you are going out of your array bound { for(i=0;i { //it replaces the next index with the current index doubleArray[i]=doubleArray[i+1]; } } // if you call doubleArray[doubleArray.Legnth], actully you have called an out of bound index, so you have made an exception and you can solve it with a try-catch statement catch { //now your newValue is replace as doubleArray[doubleArray.Legnth-1] doubleArray[i]=newValue; }

                            S Offline
                            S Offline
                            Sajjad Izadi
                            wrote on last edited by
                            #17

                            thanks friends. i didn't know them. there are always better ways that i offer :).

                            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