Re: how to "translate" dynamic array in FORTRAN 90 to Java code?
- From: tam@xxxxxxxxxxxxxxxxxxxxx
- Date: 17 Nov 2006 07:53:59 -0800
Shawn wrote:
Hi,
I am translating a Fortran program to Java code. Starting FORTRAN 90, an
array size can be un-specified, for example:
DIMENSION PS(*)
is an array with size unspecified. On the run-time, its size can be
increased automatically if needed.
Previously, I use Object array in Java to translate FORTRAN array. But
now, array in Java have to be fixed size.
If I use collections in Java, like Vector or ArrayList, the problem is,
as stated in my posting previously and copied to below:
Vector vec = new Vector();
vec.add(0, new Integer(4));
vec.add(1, new Double(4.4));
vec.add(2, "How are you?");
vec.add(5, "morning");
The last one is one error and the program cannot run. I purposely
skipped a couple positions.
There is a reason that I need skipping: I am "translating" Fortran code
to Java. In the original Fortran code, it takes an array several item
positions to hold a long string, like "How are you?". In Java, it only
needs one item position to hold it. I hope to keep index in Fortran and
Java correspondent, so I need skipping to let several item positions
unused. But the program will not run now.
Maybe I need:
vec.add(3, null);
vec.add(4, null);
?
Thank you again for your help.
Hi Shawn,
I think both you and some of the posters responding may have a
misunderstanding about
the meaning of the Fortran specification.
There are two kinds Fortran array specifications where the size is not
given.
The notation
real array(*)
is normally used when the array is a parameter of a subroutine, and the
real dimension will be specified in the argument...
E.g.,
program main
real myArray(100)
call sub(myArray)
end
subroutine sub(array)
real array(*)
...
end
This is exactly analagous to
void method() {
float[] myArray = new float[100];
sub(myArray);
}
void sub(float[] array) {
...
}
Fortran also has dynamically allocated arrays where the
size is determined at run time and the array
can be reallocated. I'm a bit rusty on the syntax here but it's
something like.
real, allocatable :: myArray(:)
...
allocate(myArray(2*n))
...
deallocate(myArray)
....
allocate(myArray(3*n))
...
This corresponds in Java to something like:
float[] myArray;
...
myArray = new float[2*n];
...
...
myArray = new float[3*n];
...
I'm pretty sure that anything like:
program main
real myArray(*)
myArray(3) = 3.14
end
or
real myArray(3)
myArray(5) = 25
end
are illegal in all versions of the Fortran standard. Neither Fortran
nor Java provides for automatic extensions of arrays when
a user asks for an element beyond the end of the allocated array.
So your job in translating from Fortran to Java may be a bit easier
than you have been led to believe...
Regards,
Tom McGlynn
.
- References:
- Prev by Date: Re: Rune-Time Error
- Next by Date: Re: Algorithm to find pairs in array [can not order]
- Previous by thread: Re: how to "translate" dynamic array in FORTRAN 90 to Java code?
- Next by thread: Re: how to "translate" dynamic array in FORTRAN 90 to Java code?
- Index(es):
Relevant Pages
|