fork download
  1. // Creating a list of cities
  2. List<String> cities = new List<String>{'Mumbai', 'Delhi', 'Bangalore', 'Chennai'};
  3.  
  4. // Adding elements
  5. cities.add('Hyderabad');
  6. cities.add(2, 'Pune'); // Inserts at index 2
  7.  
  8. // Retrieving elements
  9. System.debug(cities.get(2)); // Output: Pune
  10.  
  11. // Updating an element
  12. cities.set(2, 'Kolkata');
  13.  
  14. // Checking properties
  15. System.debug(cities.size()); // Output: 5
  16. System.debug(cities.contains('Delhi')); // Output: true
  17. System.debug(cities.indexOf('Chennai')); // Output: 3
  18.  
  19. // Sorting
  20. cities.sort(); // Now sorted alphabetically
  21.  
  22. // Removing elements
  23. cities.remove(1); // Removes the element at index 1
  24. cities.clear(); // Clears all elements
Success #stdin #stdout #stderr 0.01s 7848KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
./prog:1: expected expression