Java:使用一个函数返回几种不同类型的值

发布于 2024-08-06 06:02:34 字数 727 浏览 6 评论 0原文

我有一个 readData() 函数,它读取文件并返回一些不同的解析数据对象。现在,readData() 的返回类型是Object[]

Object[] data = readData();
MyGenome genome = data[0];
Species[] breeds = data[1];
//etc

这感觉很尴尬。有没有更好的方法来返回这些数据?我不想使用像 readGenome()readSpecies() 这样的单独函数,因为这需要对文件进行两次迭代。另外,我宁愿将所有数据收集包装在一个函数中。

类似的问题:返回两个字符串之间至少四个字符的匹配的函数:

public int[][] findMatch(String g0, String g1) { /* cool stuff */ }
//...
int[][] bounds = findMatch("ACOIICOCOCICOICA", "AOCCCCCCICCIIIIIOIAOCICOICOICA");

其中 bounds[0][0] 是 g0 的左边界,bounds[0][ 1] 是 g0 的右边界,bounds[1][0] 是 g1 的左边界,等等。这也感觉有点尴尬。如果不不断查找键,就很难用结果进行编码。

I have a readData() function that reads files a returns a few different objects of parsed data. Right now, the return type of readData() is Object[]:

Object[] data = readData();
MyGenome genome = data[0];
Species[] breeds = data[1];
//etc

This feels awkward. Is there a better way to return this data? I don't want to have separate functions like readGenome() and readSpecies() because that would require iterating over the files twice. Also, I'd rather wrap up all the gathering of data in one function.

A similar issue: a function that returns a match of at least four characters between two strings:

public int[][] findMatch(String g0, String g1) { /* cool stuff */ }
//...
int[][] bounds = findMatch("ACOIICOCOCICOICA", "AOCCCCCCICCIIIIIOIAOCICOICOICA");

where bounds[0][0] is the left bound on the g0, bounds[0][1] is the right bound on g0, bounds[1][0] is the left bound on g1, etc. This also feels sort of awkward. It is difficult to code with the result without continuously looking up the keys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

层林尽染 2024-08-13 06:02:34

创建一个新类:

class MyAnalysedGenome {
    MyGenome genome;
    Species[] species
    ...
}

并返回它。您可能会发现其他功能也应该包含在其中。也许是围绕 getData() 调用的代码。

Create a new Class:

class MyAnalysedGenome {
    MyGenome genome;
    Species[] species
    ...
}

and return that. You'll probably find you have other functionality that should go in there too. Perhaps the code that surrounds your getData() call.

猫卆 2024-08-13 06:02:34

使用强类型类来表示 readData() 的复杂返回类型怎么样?

public class Taxonomy
{
    public MyGenome genome;
    public Species[] breeds;
    //etc
{

Taxonomy data = readData();

您可以对搜索范围问题做同样的事情

public class SearchBoundary
{
    public int left;
    public int right;
}

SearchBoundary resultBounds = findMatch(searchBounds);

How about using a strongly typed class to represent the complex return type of readData()?

public class Taxonomy
{
    public MyGenome genome;
    public Species[] breeds;
    //etc
{

Taxonomy data = readData();

You can do the same thing for your search bounds problem

public class SearchBoundary
{
    public int left;
    public int right;
}

SearchBoundary resultBounds = findMatch(searchBounds);
|煩躁 2024-08-13 06:02:34

对于第一个问题,你不能简单地使用中间数据表示吗?我的意思是你可以读取你的文件一次,这将为你提供文件内容(你可以按照你想要的方式格式化),然后创建两个方法 readGenome()readSpecies()< /code> 会将此文件内容作为参数。

For the first issue,couldn't you simply use an intermediate data representation ? I mean you could read your file once, which would give you the file content (that you could format the way you want), and then create two methods readGenome() and readSpecies() that would take this file content as a parameter.

善良天后 2024-08-13 06:02:34

您可以创建一个以基因组和物种作为字段的类。

...
class DataToBeRead {
    MyGenome  genome;
    Species[] breeds;
}
...


DataToBeRead data   = readData();
MyGenome     genome = data.genome;
Species[]    breeds = data.breeds;

如果您认为其他人不会使用该类,则可以将该类设为私有;如果其他人会使用它,则可以将其设为公开。

如果您不想为其创建单独的文件,也可以将其设为静态。

希望这有帮助。

You can create a class that have genome and species as fields.

...
class DataToBeRead {
    MyGenome  genome;
    Species[] breeds;
}
...


DataToBeRead data   = readData();
MyGenome     genome = data.genome;
Species[]    breeds = data.breeds;

You can make the class private if you do not think anybody else will used it or make it public if someone else will use it.

You can also make it static if you do not want to create a separate file for it.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文