The ArrayList type is very useful for storing objects (for example Strings). The advantage of an ArrayList is that the size is flexible. It is easy to add items to an arrayList and check if an item is already added to an ArrayList.
With the add() method, a new item can be added to the ArrayList.
import java.util.ArrayList; { // Define a ArrayList to hold String objects ArrayListjobDefs = new ArrayList (); // Add an item to the ArrayList String jobdef = "System_Sleep" ; jobDefs.add(jobdef) ; }
To check if an items has already been added to an ArrayList, use the contains() method:
import java.util.ArrayList; { // Define a ArrayList to hold String objects ArrayListjobDefs = new ArrayList (); // Add an item to the ArrayList String jobdef = "System_Sleep" ; jobDefs.add(jobdef) ; if ( jobDefs.contains(jobdef) == false ) { jcsOut.println("The ArrayList does not contain " + jobdef + " yet") ; } }
Loop through the ArrayList:
import java.util.ArrayList; { // Define a ArrayList to hold String objects ArrayListjobDefs = new ArrayList (); // Add an item to the ArrayList String jobdef = "System_Sleep" ; jobDefs.add(jobdef) ; for(String jobDef : jobDefs) { jcsOut.println("Item=" + jobDef) ; } }