Home > Java > javaTutorial > body text

How to declare an integer array in Java?

王林
Release: 2023-04-24 21:40:14
forward
1776 people have browsed it

Given an integer array

Put all the even numbers in the first half, and put all the odd numbers in the second half of the array

public static void func(int[] array){
        int i = 0;
        int j = array.length - 1;
        while(i <j){
            while(i < j && array[i] % 2 == 0){
                i++;
            }
            while (i < j && array[j] % 2 != 0){
                j--;
            }
            int tmp = array[i];
            array[i] = array[j];
            array[j] = tmp;
        }
    }
 
    public static void main(String[] args) {
        int[] array = {1,4,3,6,8,5,9};
        func(array);
        System.out.println(Arrays.toString(array));
    }
Copy after login

Print the result:

How to declare an integer array in Java?

The above is the detailed content of How to declare an integer array in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template