简体   繁体   中英

Fill array with odd numbers

I have to fill an array with a range (here n ) of odd numbers: 1,3,5,7,9... But I always have a 0 between each odd numbers and I don't understand why.

Note: The code under the capital letters in comments were given by our professor...

Here is the code:

public class F5B1 {

    public static java.util.Scanner scanner = new java.util.Scanner(System.in);

    public static void main(String[] args) {

        // Creation of Array : DON'T CHANGE THIS PART OF THE PROGRAM.
        System.out.print("Entrez n : ");
        int n = scanner.nextInt();
        int[] t = new int[n];
        int i;


        // fill the array : PART OF THE PROGRAM TO FULFILL
        // INSTRUCTIONS :
        // ADD NO ADDITIONAL VARIABLES
        // DON'T USE ANY OF THE MATH METHODS
        // DON'T ADD ANY METHODS

        // I wrote this piece of code
        for (i = 0; i < t.length; i++) {
            if (i % 2 != 0) {
                t[i] += i;
            }
        }

        // display of the array : DON'T CHANGE THIS PART OF THE PROGRAM

        System.out.println("Those are the odd numbers : ");
        for (i = 0; i < t.length; i++) {
            System.out.println(t[i]);
        }
    }

}

The output:

Enter n : 10
Those are the odd numbers : 
0
1
0
3
0
5
0
7
0
9

You are getting 0 for each even index because the value at that index is never set.

This line:

int[] t = new int[n];

declares an array of int of length n where each element is initialized to 0. Now consider your code:

for (i = 0; i < t.length; i++) {
    if (i % 2 != 0) {
        t[i] += i;
    }
}

You are saying: when the index of my array is odd, let's set it to this index, otherwise, do nothing (and keep 0). This explains the result you are getting.

What you want to do is not to check whether the index of the array is odd: you want to set an odd value to each index of the array. Consider this code instead:

for (i = 0; i < t.length; i++) {
    t[i] = 2 * i + 1;
}

For each index, this sets the value of the array to an odd number ( 2n+1 is always odd).

(Note that I wrote = instead of += : it gives the intent better and does not rely on the fact that the array was initialized with 0)

With Java 8, IntStream it is as simple as:

IntStream.range(0, n).filter(element -> element % 2 != 0)
                    .forEach(System.out::println);

Usage:

public class F5B1 {

    public static java.util.Scanner scanner = new java.util.Scanner(System.in);

    public static void main(String[] args) {
        System.out.print("Entrez n : ");
        int n = scanner.nextInt();
        IntStream.range(0, n).filter(element -> element % 2 != 0)
                .forEach(System.out::println);
    }
}

Try this instead :

int odd = 1;
for (int i = 0; i < t.length; i++) {
    t[i] = odd;
    odd += 2;
}

The problem is that this :

int[] t = new int[n];

will create an array filled with zeros. In your for loop you are setting the odd numbers so the others are left at zero.

for (i = 0; i < t.length; i++) {if (i % 2 != 0) { t[i/2] = i; } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM