方法接受一个整数数组并返回新的整数数组,甚至没有整数。我已经写了一些东西,但在主要方法中无法正确打印……这就是我所拥有的
//filterAway method taking out evens from string
public static int[] filterArray(int[] x){
int [] arrayOne;
int size = 0;
for(int i=0; i<x.length; i++){
if(x[i] % 2 != 0){
size++;
}
}
arrayOne = new int [size];
int index =0;
for(int i=0; i<x.length; i++){
if(x[i] % 2 != 0){
arrayOne[index] = x[1];
index++;
}
}
return arrayOne;
}
//main
public static void main(String args[]){
int[] f = {1,2,3,4,5,6,7,8,9,10,11,12};
for (int i =0; i <f.length; i++){
System.out.print(f[i] + " ");
}
System.out.println(" ");
//here is where im struggling.
int [] fA= (filterAway); // say the string is 1-12....cannot get
// filtered array to print
for (int i =0; i <fA.length; i++){
System.out.print(arrayOne[i] + " ");
}``
System.out.println(" ");
}