public static int[] finMaxIndex(int[] array){
int max = array[0];
int index = 0;
int i=0;
while(i<array.length){
if(array[i] > max){
max = array[i];
index= i;
}
i++;
}
return index;
}
Help please about my index. I submitted and it said int cannot be converted to int[] index= i;
Under discussion
Comments (6)
- Popular
- New
- Old
You must be signed in to leave a comment
Switch/Cypher
2 November 2020, 16:37
"index" is an int.
but your method returns an int array:
so you can't return that variable. 0
Switch/Cypher
2 November 2020, 16:41
I don't know what task this is, but looking at your code, I'd guess the thing you want to change is your method signature:
In brackets is what you supply/pass to the method.
After static is what the method gives back/returns. 0
Nghi B Truong
2 November 2020, 22:42
But my autolab wants my code to be public static int[] findMaxIndex(int[] Array).
Could you show me how to fix my code if I keep static int[]???
0
Switch/Cypher
3 November 2020, 10:45
Yes, but theres no need to return an int array.
But if it's required to return an array, I suggest you might be doing the task wrong. Does it ask to return more than one value if there is more than one max? 0
Nghi B Truong
4 November 2020, 17:09
Yes it asks to return more one value if there is more than one max. I try to submit it and it gave me 0. That's weird tho
0
Switch/Cypher
4 November 2020, 20:00
Well, yeah - if there's a possibility of more than one max you need different code.
0