| 1 | Programs frequently need to deal with many instances of something: for example consider a program that how many people attended each robotics meeting. You could create a variable for each robotics meeting: |
| 2 | {{{ |
| 3 | int meeting1_attendees; |
| 4 | int meeting2_attendees; |
| 5 | int meeting3_attendees; |
| 6 | int meeting4_attendees; |
| 7 | }}} |
| 8 | But that's awkward (especially if you met 50 times during the season). Fortunately, Java offers a type of variable called an ''array''. |
| 9 | |
| 10 | The following declares a variable that stores 50 integers: |
| 11 | {{{ |
| 12 | int meeting_attendees[50]; |
| 13 | }}} |
| 14 | To print the attendees at meeting 3 you would use: |
| 15 | {{{ |
| 16 | System.out.println("Meeting 3 attendees:"+meeting_attendees[2]); |
| 17 | }}} |
| 18 | Notice that the 3rd element of the array is at index 2 since the first element is at index 0. |
| 19 | |
| 20 | Arrays are particularly powerful when you combine them with loops; consider the following: |
| 21 | {{{ |
| 22 | for (int mtg=0; mtg<50; mtg++) { |
| 23 | System.out.println("Meeting "+mtg+" attendees:"+meeting_attendees[mtg]); |
| 24 | } |
| 25 | }}} |
| 26 | Loops and arrays give you a concise way to programmatically process large collections of data. |
| 27 | |
| 28 | === Extra credit |
| 29 | * Read more about [https://www.geeksforgeeks.org/arrays-in-java/ Java arrays] |