Reputation: 2228
Please i want to serialize a several type of array to kwon what is the worst on memory. So i create a function to serialize a generic type int calcul(T a)
and it return a size on int.
public class NewClass<T> {
public static void main(String[] args) throws IOException {
byte[] tabByte = new byte[56666];
Byte[] tabByte2 = new Byte[56666];
int[] tabInt = new int[56666];
ArrayList<Byte> arr=new ArrayList<Byte>(56666);
System.out.println("size array byte[]"+calcul(tabByte));
System.out.println("size array Byte[]"+calcul(tabByte2));
System.out.println("size array int[]"+calcul(tabInt));
System.out.println("size array ArrayList<Byte>"+calcul(arr));
}
static int calcul(T a) throws IOException {
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
ObjectOutputStream stream = new ObjectOutputStream(byteOutput);
stream.writeObject(a);
int a = byteOutput.toByteArray().length;
stream.flush();
stream.close();
byteOutput.flush();
byteOutput.close();
return a;
}
}
But i have this error
non-static type variable T cannot be referenced from a static context
How can i do to make a generic variable as static and run my program ?
Upvotes: 0
Views: 134
Reputation: 504
You can follow this approach
static int calcul(T extends Serialazable a) throws IOException;
Thanks
Upvotes: 2
Reputation: 262774
What are the generics for? I think you want
static int calcul(Serializable a) throws IOException;
And you should take the length after you flush and close the streams.
And to answer your original question, you can do calculations (and read up here) about the memory requirements of primitive and wrapped ArrayLists and arrays (but the experiment should be instructive, too).
Upvotes: 1