I’m having problem with my homework assignment, and hoping to find some help.
I have to read a text file into a 2D int array.
The width of the 2D array is fixed at 5 and will never change. However, the height of the array may change depend on the length of the text file.
I have to begin with a 40×5 array; any file over 40 lines will not acceptable, but less than 40 will be OK. So, I may get only X rows of my array be filled (X <= 40).
I have to calculate the average of each column, so I need to how many elements in a column; Is there any way to get that?
Or can I change the size of my original array (with all the data in it) to Xx5 after I know what X is?
The sample input file:
034 080 055 078 045 060 100 056 078 078 070 010 066 078 056 034 009 077 078 020 045 040 088 078 055
output should be:
48.6 47.8 68.4 78.0 50.8
I wouldn’t recommend using a plain array for a dynamically expanding matrix like this. I recommend reading up on the List & ArrayList documentation for a good implementation.
From the Java doc:
[ArrayList is a] resizable-array implementation of the List interface.In effect, this is Java’s solution to the constantly-defined array limitation of the C days.