简体   繁体   中英

Error: incompatible types: Integer cannot be converted to Integer[]

I'm attempting to create a method that will return an index within an array.

private static Integer[] day1 = new Integer[6];

public Integer[] getDay1(Integer team) {
      return day1[team];
}

When I try to do this, however, it highlights the opening bracket following day1 with this error.

Days.java:32: error: incompatible types: Integer cannot be converted to Integer[]
  return day1[team];
             ^

Any idea why this is? Any help would be appreciated

return day1[team]; will return the team th value of your array. However the return type of your method is Integer[] (array). Change your method to:

public Integer getDay1(Integer team) {
      return day1[team];
}

you are returning an Integer when you have defined your method to return an array or integers, either change to

public Integer getDay1(Integer team) {
  return day1[team];
}

or

public Integer[] getDay1(Integer team) {
  return day1;
}

Integer is one integer value, Integer[] is an array of single Integer s. The signature of your getDay1 method promises an array as return value, but you are actually returning one single element (the one at position team ).

I assume you are looking for

public Integer getDay1(Integer team) {
      return day1[team];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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