简体   繁体   English

从方法返回没有名称的数组

[英]Return array without name from method

I'm doing an assignment that requires me to complete the following block of code: 我正在做一项作业,要求我完成以下代码块:

public int[] test(String input) {
    return new int[1];
}

I've already filled in part of it (I have to manipulate the string). 我已经填写了一部分(我必须操纵字符串)。 But to return my results in the array, how should I do this? 但是要返回数组中的结果,我应该怎么做? I don't see a clearly labeled array that I can write to; 我看不到可以写的标签清晰的数组; or can I create the array within the return line? 还是可以在返回行中创建数组?

I could just be missing the point, and have to create my own array to replace the one given (just a thought). 我可能只是错过了要点,而不得不创建自己的数组来替换给定的数组(只是一个想法)。

What are the constraints on the assignment? 分配有哪些限制? Would it be wrong to declare a local array, such as: 声明局部数组是否是错误的,例如:

int[] i = new int[1];
/** Modify i as needed */
return i;

Then you could manipulate i . 那你就可以操纵i

If that's not allowed, then you can do something like: 如果不允许这样做,则可以执行以下操作:

return new int[] { /* int value(s) here, separated by commas */ };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM