Solved

Dynamic double array

Posted on 2010-11-16
23
723 Views
Last Modified: 2012-05-10
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.  
0
Comment
Question by:gbcbr
  • 12
  • 7
  • 3
  • +1
23 Comments
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
Comment Utility
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
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
0
 

Author Comment

by:gbcbr
Comment Utility
I can't convert inX[] into X[][]
0
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
Yes. You can't change the dimensions of an array at run time. Make a class or something that has both arrays.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
What are you going to do with these stored elements? How are you going to need to access them?
0
 

Author Comment

by:gbcbr
Comment Utility
These elements for calculation d0 = X[0][0] - X[0][1] for example
0
 

Author Comment

by:gbcbr
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
When, if ever are you going to need, say, X[10][0] and X[10][1] ?
0
 

Author Comment

by:gbcbr
Comment Utility
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
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
Yeah, you need to declare it like this then
double X[3][20]
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:gbcbr
Comment Utility
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
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
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
 

Author Comment

by:gbcbr
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
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
 

Author Comment

by:gbcbr
Comment Utility
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
 

Author Comment

by:gbcbr
Comment Utility
Two dimensional (double) array need to make data comparable, because first array sets currency pair and second historical changes.
0
 

Author Comment

by:gbcbr
Comment Utility
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
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
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
 

Author Comment

by:gbcbr
Comment Utility
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
 

Author Comment

by:gbcbr
Comment Utility
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
 

Author Closing Comment

by:gbcbr
Comment Utility
Thank you
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now