过滤掉阵列上的整数

时间:2013-05-11 01:42:22

标签: java arrays printing filter int

方法接受一个int数组并返回新的int数组,甚至没有整数。我已经写了一些东西,但无法在主要方法中获得正确的印刷品......这就是我拥有的东西

 //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(" ");
                }

3 个答案:

答案 0 :(得分:1)

解决显示问题

尝试使用此修订后的主要方法,只需将 YourClassName 替换为您的班级名称:

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 = YourClassName.filterArray(f);
    // filtered array to print
    for (int i = 0; i < fA.length; i++) {
        System.out.print(fA[i] + " ");
    }
    System.out.println(" ");
}

然而,你会发现你最终可能会得到像2,2,2,2那样的东西。您需要重新访问filterArray函数。

修复filterArray功能

由于您的问题的标题是在Java中过滤掉Ints,这是罪魁祸首,将 1 更改为 i ,这就是它给出2,2,2的原因, 2.此外,你想要偶数,所以你应该寻找0模数,将比较器改为==,而不是!=。

enter image description here

答案 1 :(得分:0)

您需要将f[]作为参数传递给filterAway()方法。

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(" ");
    int [] fA= filterArray(f);               
    for (int i =0; i <fA.length; i++){
        System.out.print(fA[i] + " ");
    }
    System.out.println();
}

答案 2 :(得分:0)

以下是您应该如何处理您的要求:

package com.sandbox;

public class Sandbox {

    public static void main(String[] args) {
        int[] all = new int[]{1, 2, 3, 4, 5, 6};

        int[] filtered = odds(all);
        for (int i : filtered) {
            System.out.println(i);
        }
    }

    private static int[] odds(int[] all) {
        int sizeOfResult = 0;
        for (int i : all) {
            if (isOdd(i)) {
                sizeOfResult++;
            }
        }

        int[] result = new int[sizeOfResult];
        int resultIndex = 0;
        for (int i : all) {
            if (isOdd(i)) {
                result[resultIndex] = i;
                resultIndex++;
            }
        }
        return result;
    }

    private static boolean isOdd(int i) {
        return i % 2 != 0;
    }
}

如果我自己写这篇文章,那就是我要做的事情:我使用List而不是数组,我会使用CollectionUtils.filter。这是一段可以满足您需求的代码:

package com.sandbox;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;

import java.util.ArrayList;
import java.util.List;

import static com.google.common.primitives.Ints.asList;

public class Sandbox {

    public static void main(String[] args) {
        List<Integer> result = new ArrayList<Integer>(asList(1, 2, 3, 4, 5, 6));
        CollectionUtils.filter(result, new Predicate() {
            @Override
            public boolean evaluate(Object o) {
                return isOdd((Integer) o);
            }
        });
        for (Integer integer : result) {
            System.out.println(integer);
        }
    }

    private static boolean isOdd(int i) {
        return i % 2 != 0;
    }
}