Solved

Dynamic double array

Posted on 2010-11-16
23
732 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
ID: 34152786
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
ID: 34152799
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
ID: 34152822
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

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

Expert Comment

by:TommySzalapski
ID: 34152966
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
ID: 34154008
What are you going to do with these stored elements? How are you going to need to access them?
0
 

Author Comment

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

Author Comment

by:gbcbr
ID: 34154057
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
ID: 34154097
When, if ever are you going to need, say, X[10][0] and X[10][1] ?
0
 

Author Comment

by:gbcbr
ID: 34154122
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
ID: 34157190
Yeah, you need to declare it like this then
double X[3][20]
0
 

Author Comment

by:gbcbr
ID: 34178861
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
ID: 34181414
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
ID: 34181975
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
ID: 34182799
>>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
ID: 34182981
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
ID: 34183058
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
ID: 34183076
Two dimensional (double) array need to make data comparable, because first array sets currency pair and second historical changes.
0
 

Author Comment

by:gbcbr
ID: 34183226
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
ID: 34185411
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
ID: 34185953
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
ID: 34188633
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
ID: 34188648
Thank you
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to generate a random directed graph 5 82
tomcat not starting 6 69
type mismatch (Object[] to double[] 4 33
Notify sent to other threads in Java 9 33
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

830 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