Reputation: 81
I have tried using array as parameter but when trying to put an array as an argument I get following error:
required: int,int[] found: int,int reason: actual argument int cannot be converted to int[] by method invocation conversion 2 errors
Source Code:
public class utopian{
void height(int t,int[] n){
n = new int[100];
for (int i=0;i<t;i++){
int k = n[i];
if (k%2==0){
for(int j=0;j<=k;j=j+2){
int h=1;
h=h*2;
h=h+1;
}}
else{
for(int j=0;j<=k;j=j+2){
int h=1;
h=h*2;
h=h+1;
}
int h=h*2;
}
System.out.println(h);
}
}
}
--
public class Solution {
public static void main(String[] args) {
utopian heht = new utopian();
heht.height(2,{0,1})
}}
I want to use array as an argument and I cannot understand how.
Upvotes: 1
Views: 966
Reputation: 23012
You can do it like this,
utopian heht = new utopian();
int array[]={0,1};//Declaration of array
heht.height(2,array)//pass array
Other than that inline declaration won't allow you to reuse the array.
heht.height(2, new int[] {0,1});//Here you can't reuse the array in further code
//if you don't want to reuse it than go for this
Upvotes: 0
Reputation: 109613
heht.height(2, new int[] {0,1})
However there are places for a less verbose notation, which probably misled you:
int[] data = {0, 1};
Upvotes: 2