-2

I have an array and "for each" loop skips the first element of it.

for (int number : nums) {
    System.out.println(nums[number]);
}

Output starts from nums[1]. What is wrong?

89f3a1c
  • 1,430
  • 1
  • 14
  • 24
Sharper
  • 173
  • 4
  • Your for loop is already iterating over `nums`. In other words, the `number` variable is the _element_ of the `nums` array at a specific index (the index is hidden behind-the-scenes). You then use `nums[number]` which would be equivalent to using `nums[nums[index]]`. – Slaw Oct 08 '19 at 19:28
  • 2
    because, maybe `1` is the first element in `nums`. Please verify.. – Lal Oct 08 '19 at 19:29
  • Perhaps you want `System.out.println(number);` because `number` is already an element of the array. You don't need to look it up again. – Dawood ibn Kareem Oct 08 '19 at 19:29
  • What does `nums` look like. Do update that in the question. And as pointed out u're already iterating on `nums` with current element pointed by `number` which is probably what you want. And not access it like `nums[number]` – ambianBeing Oct 08 '19 at 19:29
  • is `for (int number : nums) { System.out.println(number); }` , what you are trying to achieve? – Lal Oct 08 '19 at 19:30
  • Possible duplicate of [How does the Java 'for each' loop work?](https://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work) – jrook Oct 08 '19 at 21:26

4 Answers4

0

Please try the following code:

 for (int currentNumber : nums) {
        System.out.println(currentNumber);
    }

I've tried renaming the variable used in the iteration so it is easier to understand how to print it / manipulate it.

For this reason when you use nums[number] at the 1st iteration you are printing nums[1]

Pitto
  • 8,229
  • 3
  • 42
  • 51
0

You're accessing the array again inside the for clause, when that is what the for is already doing.

With that in mind, your code should look like this:

for (int number : nums) {
    System.out.println(number);
}

What you were doing is access the array at the position indicated by the value from the array being read.

Hope this helps!

89f3a1c
  • 1,430
  • 1
  • 14
  • 24
0

You're using an "enhanced for loop":

for (int number : nums) {
    System.out.println(nums[number]);
}

This is equivalent to:

for (int i = 0; i < nums.length; i++) {
    int number = nums[i];
    System.out.println(nums[number]);
}

So you using nums[number] means you're accessing the array with an index whose value is an element of the array. If the element at nums[0] is 1 then the first element printed out will be the element at nums[1].


Note you probably meant to use:

for (int number : nums) {
    System.out.println(number);
}

Whether or not this solves your problem completely is difficult to say and would depend on what you're actually trying to do.

Slaw
  • 37,820
  • 8
  • 53
  • 80
0

You can get value directly in number varible. You won't get index in number variable. This is exact solution.

for (int number : nums) {
    System.out.println(number);
}