Java 如何返回数组的特定元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19376735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to return a specific element of an array?
提问by Nik
I want to return odd numbers of an array yet Eclipse doesn't seem to accept my return array[i];
code. I think it requires returning a whole array since I set an array as a parameter to my method.
As I said before, I need to pass an array and get a specific element of that array in return. Even if I make that array static, how do I return a single element?
我想返回数组的奇数,但 Eclipse 似乎不接受我的返回array[i];
代码。我认为它需要返回整个数组,因为我将数组设置为我的方法的参数。正如我之前所说,我需要传递一个数组并获取该数组的特定元素作为回报。即使我将该数组设为静态,我如何返回单个元素?
Edit : Alright then, here it is:
编辑:好吧,这里是:
public class newClass{
public static void main(String[] args)
{
int [] newArray= new int [4];
int [] array = {4,5,6,7};
newArray[0] = array[0]+array[1]+array[2]+array[3];
newArray[1] = array[0]*array[1]*array[2]*array[3];
newArray[2] = findOut(array);
}
public static int findOut (int [] array3)
{
int e1=0;
int e2=0;
for (int i=0; i<array3.length; i++)
{
if (array3[i]%2==0)
{
e1+=array3[i];
array3[i]=e1
return array3[i];
}
else
{
e2+=array3[i];
array3[i]=e2;
return array3[i];
}
}
}
}
I know there are probably more than a few mistakes here but I'm working on it and I'm not only returning odd numbers, I also add them together.
我知道这里可能有很多错误,但我正在努力解决它,我不仅返回奇数,还将它们加在一起。
采纳答案by Admit
You code should look like this:
您的代码应如下所示:
public int getElement(int[] arrayOfInts, int index) {
return arrayOfInts[index];
}
Main points here are method return type, it should match with array elements type and if you are working from main()
- this method must be static also.
这里的要点是方法返回类型,它应该与数组元素类型匹配,如果您正在工作main()
- 此方法也必须是静态的。
回答by Batty
Make sure return type of you method is same what you want to return. Eg: `
确保您的方法的返回类型与您想要返回的相同。例如:`
public int get(int[] r)
{
return r[0];
}
`
`
Note : return type is int, not int[], so it is able to return int.
注意:返回类型是int,不是int[],所以可以返回int。
In general, prototype can be
一般来说,原型可以是
public Type get(Type[] array, int index)
{
return array[index];
}
回答by yamafontes
I want to return odd numbers of an array
我想返回数组的奇数
If i read that correctly, you want something like this?
如果我没看错,你想要这样的东西吗?
List<Integer> getOddNumbers(int[] integers) {
List<Integer> oddNumbers = new ArrayList<Integer>();
for (int i : integers)
if (i % 2 != 0)
oddNumbers.add(i);
return oddNumbers;
}
回答by Boann
(Edited.) There are two reasons why it doesn't compile: You're missing a semi-colon at the end of this statement:
(已编辑。)无法编译的原因有两个: 您在此语句末尾缺少分号:
array3[i]=e1
Also the findOut method doesn't return any value if the array length is 0. Adding a return 0;
at the end of the method will make it compile. I've no idea if that will make it do what you want though, as I've no idea what you want it to do.
此外,如果数组长度为 0,则 findOut 方法不会返回任何值return 0;
。在方法末尾添加 a将使其编译。我不知道这是否会让它做你想做的事,因为我不知道你想要它做什么。