+ 5
what is wrong in the codes i would appreciate any help
8 ответов
+ 8
You need to create an array, first in main method.
int[] x={5,6,7,9,0};
Then,
System.out.println(hello(x));
+ 8
5 is not an array.
Method hello requires an array.
+ 7
Also,you need to declare hello as static.
public static ..........hello .....
+ 7
There's a couple problems here:
5 is not an array.
You would need something like this:
int[] myArray = new int[] {5};
System.out.print(hello(myArray));
You're calling a non-static method from a static method, this isn't allowed since there's no instance of the class to call the non-static method from.
so change:
public int hello(... etc)
to:
private static int hello(...etc.)
Then that's it.
Note* the reason I made it private instead of public is simply because there's no need to have it public (it's considered good programming practice / standard).
+ 7
@Merharban @Edward
int[] a = {5};
is the same as
int[] a = new int[]{5};
The difference is that on 'new' declaration by giving the array it's size, it will fill the default values in for you.
so:
int[] a = new int[5];
is: [0,0,0,0,0].
rather than having to write:
int[] a = {0,0,0,0,0}.
which is: [0,0,0,0,0].
So, by writing int[] a = {}; your forced to put in all the values. In this case, that could be better though. That method also cannot be used when the array is already declared. Example:
int[] a;
a = {5}; // is illegal
+ 5
@Restoring faith,I don't think you can create an array like that.
It should be
int[] myArray=new int[5];
+ 5
you can create an array like int[ ] arr = {5};
its simpler than @restoring faiths method
+ 4
can u plz explain how to do it in parameters.