0

I'm a beginner in Java and have been practicing different problems. The task is given a minimum and maximum, print an array of the odd numbers within the range. For example a minimum of 1 and maximum of 10 would print 3 5 7 9.

public class Practice {

static int[] oddNumbers(int minimum, int maximum) {

int[] arr = new int[10];

    for(int i = minimum; i <= maximum; i++)
    {
        if(i % 2 != 0)
        {
            //System.out.println("Odd " + i);
            for(int j = 0; j < arr.length; j++)
            {
                arr[j] = i;
            }
        }
    }
    return arr;
}

public static void main(String[] args) {

    int min = 3;
    int max = 9;

    System.out.println(Arrays.toString(oddNumbers(min, max)));
} 
}   

My current output is "[I@15db9742". Is that the reference to the array? Would it also be better to use an ArrayList? Thanks!

UPDATE: I added Arrays.toString to my output. However my output is now giving me [9, 9, 9, 9, 9, 9, 9, 9, 9, 9] instead of simply 3 5 7 9 so it looks like its saving the last value multiple times.

dollaza
  • 213
  • 3
  • 9

1 Answers1

2

Use Arrays.toString(..) method:

System.out.println(Arrays.toString(oddNumbers(min, max)));
takendarkk
  • 3,347
  • 8
  • 25
  • 37
Ivan Pronin
  • 1,768
  • 16
  • 14