P
Paul O'Grady
Hi all,
I have some code that I am converting from C to Java. I have two very
simple structs as ff:
struct a {
float *data ;
int num_elements ;
}
struct b {
int *data ;
int num_elements ;
}
As you can see, the only difference is that one stores an array of
integers and the other stores an array of floats. This leads to some
unnecessarily difficult to read code. I want to simply this in my Java
code by storing the data in an ArrayList. I am thinking of saving the
data as objects so that I can use the same class to store *either*
integers or floats (or any other data tpe later on - ala C++ template
classes).
My proposed new object is as ff:
public class ArrayClass {
private ArrayList data = null ;
.....
ArrayClass( int NumElements)
{
data = new ArrayList(NumElements) ;
......
}
/* Problem is with the methods */
int getItem(int el_num) {} ;
float getItem(int el_num) {} ;
}
My two questions then are:
1). Will I suffer a huge performance penalty because I am storing
objects instead of native types in an array (I chose a ArayList because
I will need to expand the array from time to time)
2). Can I use the overloading of methods to get around the different
datatypes (the Class will only store one data type, and needs to throw
exceptions when an object of a different type is being added to the
ArrayList
3). lastly, is this the best way to implement the required functionality
(I am a Java Newbie)
Look forward to your answer
Thanks
I have some code that I am converting from C to Java. I have two very
simple structs as ff:
struct a {
float *data ;
int num_elements ;
}
struct b {
int *data ;
int num_elements ;
}
As you can see, the only difference is that one stores an array of
integers and the other stores an array of floats. This leads to some
unnecessarily difficult to read code. I want to simply this in my Java
code by storing the data in an ArrayList. I am thinking of saving the
data as objects so that I can use the same class to store *either*
integers or floats (or any other data tpe later on - ala C++ template
classes).
My proposed new object is as ff:
public class ArrayClass {
private ArrayList data = null ;
.....
ArrayClass( int NumElements)
{
data = new ArrayList(NumElements) ;
......
}
/* Problem is with the methods */
int getItem(int el_num) {} ;
float getItem(int el_num) {} ;
}
My two questions then are:
1). Will I suffer a huge performance penalty because I am storing
objects instead of native types in an array (I chose a ArayList because
I will need to expand the array from time to time)
2). Can I use the overloading of methods to get around the different
datatypes (the Class will only store one data type, and needs to throw
exceptions when an object of a different type is being added to the
ArrayList
3). lastly, is this the best way to implement the required functionality
(I am a Java Newbie)
Look forward to your answer
Thanks