Array Management
-
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; }
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
-
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
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 -
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 -
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
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
-
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)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 -
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 -
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
-
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; }
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
-
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
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).
-
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; }
thanks friends. i didn't know them. there are always better ways that i offer :).