Changes between Initial Version and Version 1 of ControlSystems/SoftwareTeam/Training/GettingStarted/Arrays


Ignore:
Timestamp:
Nov 20, 2019, 6:52:58 PM (6 years ago)
Author:
David Albert
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/Training/GettingStarted/Arrays

    v1 v1  
     1Programs 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}}}
     8But that's awkward (especially if you met 50 times during the season).  Fortunately, Java offers a type of variable called an ''array''. 
     9
     10The following declares a variable that stores 50 integers:
     11{{{
     12   int meeting_attendees[50];
     13}}}
     14To print the attendees at meeting 3 you would use:
     15{{{
     16   System.out.println("Meeting 3 attendees:"+meeting_attendees[2]);
     17}}}
     18Notice that the 3rd element of the array is at index 2 since the first element is at index 0.
     19
     20Arrays 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}}}
     26Loops 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]