B4J Question two-dimensional array to file

red30

Well-Known Member
Licensed User
Longtime User
I need to write two-dimensional array of numbers (type Double) to file. Then I want to ask for any row, and the program must show me all the numbers of that row. How to do it? How long is writing and reading?
 

red30

Well-Known Member
Licensed User
Longtime User
Erel, сan you give an example for my case?
There is an array: Amp1(255) As Double
Write it in the first row.
Amp2(255) As Double
Write it in the second row.
And read the second row.
I will be very grateful!
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
Public matrix As List
Public FFTAmp(257) As Double

I'm writing in the "matrix" array of "FFTAmp"
matrix.Add(FFTAmp)

After I multiply each element of the "FFTAmp" by a constant
For col = 0 To 255
FFTAmp(col)=FFTAmp(col)*const
Next

When I read "matrix(0)", then all elements of the array are also multiplied by a constant. Why is this happening? How can this be remedied?
SaveAmp=matrix.Get(0)
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
B4X:
Private matrix As List
raf.Initialize(File.DirApp, "1.dat", False)
raf.WriteB4XObject(matrix, 0)
After "raf.WriteB4XObject" getting an error:
B4X:
Waiting for debugger to connect...
Program started.
Saving changes...
Error occurred on line: 662
java.lang.RuntimeException: This method does not support arrays of primitives.
    at anywheresoftware.b4a.randomaccessfile.B4XSerializator.writeObject(B4XSerializator.java:214)
    at anywheresoftware.b4a.randomaccessfile.B4XSerializator.WriteObject(B4XSerializator.java:102)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.WriteB4XObject(RandomAccessFile.java:295)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:656)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:232)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
    at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
    at anywheresoftware.b4a.BA$2.run(BA.java:165)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Why?
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
Erel, but I HAVE TO use doubles because after FFT I get the array of amplitude in DOUBLES.
Erel, sorry me if i dont understand something...

I get the array of amplitude (0..255) in doubles
Public matrix As List
matrix.Add(FFTAmp)
After I write this array to "matrix" of list format.
I managed to save this array to the memory with the help of WriteObject and to read with ReadObject.

Please anwer several questions:
1)Why when i have already written something in "matrix", and then i change it into the program, it changes in matrix also. I have fixed it but why that was happening..
2)When I add many elements to matrix where do they store - in RAM? Many is realy a loot of and im afrais the memory can be overload.
3)Can I choose the way where to save the matrix (like in save as...)?
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Are you trying to store the array to disk as a way of saving it? As I cannot imagine reading lists from disk to modify data will be as fast as using an in memory array.

Anyway, if you want to be able to store the two dimensional array of doubles to disk , then be able to read it back at a later date , this can be done in <20 lines of code.
B4X:
 'fos , oos, fis, ois are all javaobjects
 Dim fname As String = "C:/temp/testdata.dat" 'name of file for data
 fos.InitializeNewInstance("java.io.FileOutputStream",Array(fname))
 oos.InitializeNewInstance("java.io.ObjectOutputStream",Array(fos))
 ' ar in the next line is the 2 dimensional Double array
 ' I just made it (2,3) but anysize should work
 oos.RunMethod("writeObject",Array(ar)) ' write the data
 oos.RunMethod("close",Null) ' close output object stream
 fos.RunMethod("flush",Null) ' flush the file output buffer
 fos.RunMethod("close",Null) ' close output file
 ' these lines read it back into new array (to prove it works)
 fis.InitializeNewInstance("java.io.FileInputStream",Array(fname))
 ois.InitializeNewInstance("java.io.ObjectInputStream",Array(fis))
 Dim yy(2,3) As Double ' the new array for the data from file
 yy = ois.RunMethod("readObject",Null) ' read the object
 ois.RunMethod("close",Null) ' close the object input stream
 fos.RunMethod("close",Null) ' close the file input stream
 ' this just shows the data read from the file
 For a = 0 To 1
 For b = 0 To 2
 Log("("&a&","&b&") "&yy(a,b))
 Next
 Next
 
Upvote 0
Top