Dynamic double array

I have an array updated every second. For practical needs I need to save last 20 values in double array with permanent shifting values from 0 to 19.
Incoming data:
Avg[0] = (openBid[0] + openAsk[0])/2;
After it has to be converted in double array:
Avg[0] = X[0][0];
and then, when new value arrive into X[0][0], value of X[0][0] has to be shifted into the X[0][1];
after each new arriving all values have to be shifted down up to X[0][19] and this process has to be endless (loop);
So, we will have permanently updated dynamic double array list of last 20 values of Avg[0].

Please advice sample of Java code which will shift values from one array member to another.  
gbcbrAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
TommySzalapskiConnect With a Mentor Commented:
You just need a for loop like
for(int i = 19; i  > 0; i--)
{
  X[0][i] = X[0][i-1];
}

Open in new window

0
 
TommySzalapskiCommented:
That's C++ syntax but it's either the same as Java or close enough to where you can get it working.
There's no real need to check how many need to be shifted, you just shift all of them every time. If there is junk, it just gets shifted around and no one gets hurt.

Technically I'd use a circular queue for this, but if you don't have any idea what I'm talking about, just use the for loop and it will work just fine.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
gbcbrAuthor Commented:
I can't convert inX[] into X[][]
0
 
TommySzalapskiCommented:
Yes. You can't change the dimensions of an array at run time. Make a class or something that has both arrays.
0
 
CEHJCommented:
What are you going to do with these stored elements? How are you going to need to access them?
0
 
gbcbrAuthor Commented:
These elements for calculation d0 = X[0][0] - X[0][1] for example
0
 
gbcbrAuthor Commented:
I try to initialize:
double[][] X;
........
X[0][i] = inX[0];

Open in new window

java.lang.NullPointerException
        at lionfx_algo.algobot.AlgoBot.algoBot(AlgoBot.java:78)
0
 
CEHJCommented:
When, if ever are you going to need, say, X[10][0] and X[10][1] ?
0
 
gbcbrAuthor Commented:
No, I have max array X[2][0];
so it will be:
X[0][0] => X[0][19]
X[1][0] => X[2][19]
X[2][0] => X[2][19]
only
0
 
TommySzalapskiCommented:
Yeah, you need to declare it like this then
double X[3][20]
0
 
gbcbrAuthor Commented:
this is not a declaration:
X[0][0] => X[0][19]
just I show range which I need.
I tried different ways to transform single array list into double, but no success.
0
 
TommySzalapskiCommented:
No. I'm saying you can't transform a single dimension array to a two dimensional one. You have to declare it as a 2D array like
double X[3][20]
Then you'll have what you need. Three lists of 20 items.
0
 
gbcbrAuthor Commented:
I have two tasks which I have to solve:
When I get data from the table and send it to input of neural network it has to be single array because nn doesn't accept double.
But after this I have to create list of last 20 values for statistical calculations, for only this reason I try to transform single array into double.
0
 
CEHJCommented:
>>for only this reason I try to transform single array into double

I assume by that you mean into a two-dimensional array. You should probably use a circular queue of one-dimensional arrays
0
 
TommySzalapskiCommented:
Oh, well you just need two arrays then.
double bids[3]
double stats[3][20]

Use the first array for the values and the second for the statistical calculations.

A 3x20 array only contains 60 values total so you'd lose the original numbers.

Or if you define a circular queue class then do

double bids[3]
CircQueue stats[20]
0
 
gbcbrAuthor Commented:
Yes, this what I try to do, but how to set data from double bids[3] into double stats[3][20]?
Generally speaking, double stats[3][20] is extension of double bids[3], because we just add second array list to the first.
As I read in all tutorials about arrays - multidimensional array is just a set of few simple arrays.
One we already have, we need to get value from first array ant set it into second array list.
But bids[0] = stats[0][0]; not works because of different dimensions of arrays.
Lifetime of values bids[3] is one second, after this period all array has new values, so for statistical analysis we have only variables with last second value.
0
 
gbcbrAuthor Commented:
Two dimensional (double) array need to make data comparable, because first array sets currency pair and second historical changes.
0
 
gbcbrAuthor Commented:
I found circQueue.java and even create this class in my project:
http://read.pudn.com/downloads9/sourcecode/internet/telnet/server/32411/¿¿¿¿¿¿/circQueue.java__.htm
but I still don't understand how it can help me for statistical data
0
 
TommySzalapskiCommented:
bids[0] = stats[0][0] does work. Because although bids and stats have different dimensions. bids[0] and stats[0][0] are both doubles.

Now you said you need the single array to use as input for your nn. Why not do this? Declare your array transposed (flipped) like this.
double X[20][3]
Then to shift, you just do X[19][0] = X[18][0] etc and let's say you want to pass the first set into your nn you just send it X[0] (X[0] will represent an array containing X[0][0], X[0][1], and X[0][2]). So X is your 2D array and X[0] is your 1D array. In this way you can actually pass any set of three values into the nn.

The reason everyone has suggested circular queues is because it is like the shifting array you described in the original post only it's much more efficient. But if you are only using an array of size 20, you won't see any speed improvement so if it's too complicated to mess with yet, don't bother.
0
 
gbcbrAuthor Commented:
This I have when try to convert from 1D into 2D:
 
int i;
double[] inX = new double[3]; 
double[][] X = new double [3][i];
..................
i = 20;
X[0][i] = inX[0];
for (int i = 19; i > 0; i--)
{
X[0][i] = X[0][i-1];
}
java.lang.ArrayIndexOutOfBoundsException: 20

Open in new window


About flipping array I didn't understand yet how I can set 20 values on it and how I can send 1D array data to nn?

Also about circular queues, can you advice how I can use it, I have already made this class, but I don't understand how to apply my array data on it?

Please advice
0
 
gbcbrAuthor Commented:
This is the solution:
 
public class AlgoCalc {

    int j;

    double[][] X = new double[3][20];

    public void algoCalc(double[] inX) {

        for (int j = 19; j > 0; j--) {
            X[0][j] = X[0][j - 1];
     }
        X[0][j] = inX[0];
        X[1][j] = inX[1];
        X[2][j] = inX[2];

        System.out.println(" ixX[0] " + inX[0] + " : " + " inX[1] " + inX[1] + " : " + new java.util.Date());
        System.out.println(" X[0][0] " + X[0][0] + " : " + " X[0][1] " + X[0][1]  + " : " + " X[0][2] " + X[0][2]  + " : " +
                " X[0][3] " + X[0][3]  + " : " + " X[0][4] " + X[0][4]  + " : " + " X[0][5] " + X[0][5]  + " : " +
                " X[0][6] " + X[0][6]  + " : " + " X[0][7] " + X[0][7]  + " : " + " X[0][8] " + X[0][8]  + " : " +
                " X[0][9] " + X[0][9]  + " : " + " X[0][10] " + X[0][10]  + " : " + " X[0][11] " + X[0][11]  + " : " +
                " X[0][12] " + X[0][12]  + " : " + " X[0][13] " + X[0][13]  + " : " +" X[0][14] " + X[0][14]  + " : " +
                " X[0][15] " + X[0][15]  + " : " + " X[0][16] " + X[0][16]  + " : " + " X[0][17] " + X[0][17]  + " : " +
                " X[0][18] " + X[0][18]  +  " : " + " X[0][19] " + X[0][19] + " : " + new java.util.Date());
    }
}

Open in new window

X[0][0] 0.06825125000000001 :  X[0][1] 0.06825624999999999 :  X[0][2] 0.06825775 :  X[0][3] 0.06825775 :  X[0][4] 0.06825775 :  X[0][5] 0.06825775 :  X[0][6] 0.06825775 :  X[0][7] 0.06825574999999999 :  X[0][8] 0.06825574999999999 :  X[0][9] 0.06825975 :  X[0][10] 0.06825975 :  X[0][11] 0.06826375 :  X[0][12] 0.06826375 :  X[0][13] 0.06826125000000001 :  X[0][14] 0.06826125000000001 :  X[0][15] 0.06826125000000001 :  X[0][16] 0.06826125000000001 :  X[0][17] 0.06826125000000001 :  X[0][18] 0.06826025 :  X[0][19] 0.06826025 : Mon Nov 22 17:11:39 EET 2010
0
 
gbcbrAuthor Commented:
Thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.