[英]incompatible types: java.lang.String cannot be converted to int[]
I am making a java program that prints the even and odd numbers of an array.我正在制作一个打印数组的偶数和奇数的 java 程序。 This is my even code that returns the evens variable and prints the evens in the runner case.这是我的偶数代码,它返回 evens 变量并在 runner 案例中打印偶数。
public static int[] getAllEvens(int[] array)
{
String evens;
for(int i=0; i<array.length; i++)
{
if(array[i]%2==0)
{
evens = (array[i]+" ");
}
}
return evens;
}
But when I compile, it says that the evens variable cannot be converted to int[].但是当我编译时,它说evens 变量不能转换为int[]。 I get that String or int shouldn't be used to define evens but I do not know how else I can return array[i]+" ".This is so difficult for me.我知道不应使用 String 或 int 来定义事件,但我不知道我还能如何返回 array[i]+" "。这对我来说太困难了。
Edit 1: Using
Scanner
& using temporaryArrayList
to makeint[]
compact编辑 1:使用Scanner
和使用临时ArrayList
使int[]
紧凑
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int[] evens = getAllEvens1(arr);
for (int i : evens) {
System.out.print(i + " ");
}
}
public static int[] getAllEvens1(int[] array) {
int[] temp = new int[array.length]; // if all of array are `even`..
Arrays.fill(temp, -1); //or any flag.. to indicate it's unnecessary
int j = 0;
for (int i : array) {
if (i % 2 == 0) {
temp[j++] = i;
}
}
int[] ret = new int[j];
j = 0;
for (int i : temp) {
if (i != -1) {
ret[j++] = i;
}
}
return ret;
}
public static int[] getAllEvens(int[] array) {
ArrayList<Integer> temp = new ArrayList();
for (int i : array) {
if (i % 2 == 0) {
temp.add(i);
}
}
return temp.stream().mapToInt(Integer::intValue).toArray();
}
}
There are several problems in your code.. First you're trying to return String
whereas your function return type is int[]
Secondly, you're assigning only current even number to the String
which hasn't even initialized yet..您的代码中有几个问题。首先,您尝试返回String
而您的函数返回类型是int[]
其次,您仅将当前偶数分配给尚未初始化的String
..
Fix:使固定:
public static String getAllEvens(int[] array) {
StringBuilder evens = new StringBuilder();
for (int i = 0; i < array.length; i++) {
if (array[i] % 2 == 0) {
evens.append(array[i]).append(" ");
}
}
return evens.toString();
}
Or by using String
instead of StringBuilder
:或者使用String
而不是StringBuilder
:
public static String getAllEvens(int[] array) {
String ret = "";
for (int i = 0; i < array.length; i++) {
if (array[i] % 2 == 0) {
ret += array[i] + " ";
}
}
return ret;
}
please change the return type of the method.请更改方法的返回类型。 your returning String but method return type is int Array.您返回的 String 但方法返回类型是 int Array。 That is the issue you're unable to compile the code.这就是您无法编译代码的问题。
you could do some thing like this:你可以做这样的事情:
FYI: I have not tested this but should be ok.仅供参考:我没有测试过这个,但应该没问题。
public static int[] getAllEvens(int[] array) {
int[] tmp = new int[array.length];
for (i = 0; i < array.length; i++) {
if (array[i] % 2 == 0) {
tmp[even_count++] = array[i];
}
}
return tmp;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.