English 中文(简体)
Java 2D 阵列的实际大小, (2D 阵列的覆盖面积)
原标题:The real size of a 2D Array, (Filled area of 2D array), in Java
  • 时间:2012-05-25 00:50:26
  •  标签:
  • java

我作业有问题,想找人帮忙

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.

我必须从一个 40x5 矩阵开始; 超过 40 行的任何文件都无法接受, 但少于 40 则可以 。 因此, 我可能会只得到我数组中的 X 行被填充( X & lt;= 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?

样本输入文件 :

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

产出应当是:

48.6 47.8 68.4 78.0 50.8
最佳回答

我建议不要用一个平坦的阵列来设置这样的动态扩展矩阵。我建议阅读 List & amp;ArrayList 的文件,以便很好地执行。

From the Java doc: [ArrayList is a] resizable-array implementation of the List interface.

实际上, 这是 Javas 的解决方案, 来解答 C 天的定数限制 。

问题回答

您需要做的是计算每个单独的列的总和( 容易; 您不需要保持整个状态, 只保持每列的运行总数), 加上行数的数。 鉴于此, 计算每列的平均值应该很容易。 您可以一次一次一次一次一次一次一次一次一次一次读取一次, 您的解决方案应该能够处理任何长度的文件 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签