It's a Java array code, but there's a part I don't understand.

Asked 2 years ago, Updated 2 years ago, 42 views

class ForExample5 {

public static void main(String args[]) {
    int arr[] = { 10, 20, 30, 40, 50 };
    for (int cnt = 0; cnt < arr.length; cnt++) { 
        System.out.println(arr[cnt]);
        }
    for (int num : arr) {
        System.out.println(num);
    } 
    System.out.println("Done."); 
} 

}

If you print out the code above,

10

20

30

40

50

10

20

30

40

50

Done.

It's printed like this.

for (int num : arr) {
        System.out.println(num);

I don't understand this part.

I don't know what operation arr is

java array

2022-09-21 18:06

1 Answers

In other languages, it is provided as for in.

Look up objects or elements in the collection one by one (no need to find out the length and compare to tour the array lights).)

That is, intarr[] = { 10, 20, 30, 40, 50}; elements 10, 20, 30, 40, 50 are traversed one by one.


2022-09-21 18:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.