在数组中排列偶数和奇数

时间:2014-07-21 15:51:57

标签: java arrays

给出输入

1 1 2 2 3 3 4 4 5 5

这是我的代码

String even = "", odd = "";
for(int i = 0; i < parts.length; i++)
        {
            Line[i] = Integer.parseInt(parts[i]);
            if(Line[i] % 2 == 0)
            {
                even = even + " " + Line[i];
            }
            else
            {
                odd = odd + " " + Line[i];
            }
        }

和我的输出

odd: 1 1 3 3 5 5
even: 2 2 4 4

我需要输出像这样

odd: 1 3 5
even: 2 4

任何想法?

4 个答案:

答案 0 :(得分:2)

使用HashSet而不是字符串连接来获取唯一值。

Set<Integer> evenSet = new LinkedHashSet<Integer>();
Set<Integer> oddSet = new LinkedHashSet<Integer>();
f(Line[i] % 2 == 0)
{
    evenSet.add(Line[i]); // add data to a set here instead of string concatenation
}
else
{
    oddSet.add(Line[i]);// add data to a set here instead of string concatenation
}

答案 1 :(得分:1)

在迭代输入时,不应将奇数和偶数中的每一个存储为字符串,而应将它们存储在某种Collection中,可能是Set,因为它会给你输出所需的唯一性。

完成迭代后,您可以以所需的字符串格式输出Set

答案 2 :(得分:0)

使用 Lambda Experssion

,您可以通过减少头痛和减少代码来解决问题

代码:

public class LambdaTest     
{
    public static void main (String[] args)
    {
        List<Integer> objList = Arrays.asList(1, 1 ,2 ,2 ,3 ,3 ,4 ,4 ,5 ,5);
        System.out.print("The even numbers in the list "); 

        objList.stream().filter( i -> i %2 == 0 ).distinct().forEach(i -> System.out.print(" " + i));

        System.out.print("\n The odd numbers in the list ");

        objList.stream().filter( i -> i %2 != 0 ).distinct().forEach(i -> System.out.print(" " + i));
    } 
}

输出:

The even numbers in the list 2 4 
The odd numbers in the list 1 3 5

答案 3 :(得分:0)

//This program moves the odd number to right side and all the even number to left side

package com.java.util.collection;

/**
* Arrange even and odd numbers in an Array
* @author vsinha
*
*/
public class ArrangeEvenOddInArray {


    public static void arrangeEvenOddInArray(int[] arr) {
        int i =0;
        int j =arr.length-1;
        /**
        * Moves all the odd number to right side and all the even number to left side.
        */
        while(i<j) {
            // Left side is even num and right side is odd number, no swapping
            if(isEven(arr[i]) && isOdd(arr[j])) {
                i++;
                j--;
            } else if(!isEven(arr[i]) && !isOdd(arr[j])) {// Left side is odd num and right side is even number, swapping wil come in picture
                swap(i,j,arr);
            } else if(isEven(arr[i])){
                i++;// Number is even
            } else{
                j--;// number is odd
            }

        }   
        isplay(arr);
    }


    /**
    * Display Array
    * @param arr
    */
    public static void display(int[] arr) {
        System.out.println("\n");
        for(int val:arr){
            System.out.print(val +"  ");
        }
    }

    /**
    * Swapping two numbers
    * @param pos1
    * @param pos2
    * @param arr
    */
    private static void swap(int pos1, int pos2, int[] arr) {
        int temp = arr[pos1];
        arr[pos1]= arr[pos2];
        arr[pos2]= temp;
    }

    /**
    * true if number is odd
    * false if number is even
    */

    public static boolean isOdd(int i) {
        return (i & 1) != 0;
    }

    /**
    * true if number is even
    * false if number is odd
    */

    public static boolean isEven(int i) {
        return (i & 1) == 0;
    }
    public static void main(String[] args) {
        int arr[]={1, 1, 2, 2, 3, 3, 4, 4, 5, 5};
        arrangeEvenOddInArray(arr);
    }
}