fork download
  1. package org.JavaIncloud.java;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class CommonOpetationWithList
  7. {
  8. public static void main(String...JavaInCloud)
  9. {
  10. List<String> luxuryCars = new ArrayList<String>();
  11.  
  12. //List allows duplicate element to add
  13. luxuryCars.add("BMW"); luxuryCars.add("Audi");
  14. luxuryCars.add("MercedesBenz");luxuryCars.add("Porsche");
  15. luxuryCars.add("Jaguar"); luxuryCars.add("Duesenberg");
  16. luxuryCars.add("RollsRoyce"); luxuryCars.add("RollsRoyce");
  17.  
  18. System.out.println("luxuryCars>>"+luxuryCars);
  19. //luxuryCars>>[BMW, Audi, MercedesBenz, Porsche, Jaguar, Duesenberg, RollsRoyce, RollsRoyce]
  20. System.out.println(luxuryCars.size());//8
  21.  
  22. //check whether BMW is there in the List if yes then remove it
  23. if(luxuryCars.contains("BMW"))
  24. luxuryCars.remove("BMW");
  25.  
  26. System.out.println("luxuryCars after removing>>"+luxuryCars);
  27. //luxuryCars after removing>>[Audi, MercedesBenz, Porsche, Jaguar, Duesenberg, RollsRoyce, RollsRoyce]
  28. System.out.println(luxuryCars.size());//7
  29.  
  30. //check the list size if it's below 8 and it doesn't contain BMW then add it
  31. if(luxuryCars.size() < 8 && !luxuryCars.contains("BMW"))
  32. luxuryCars.add(0,"BMW"); // add BMW at starting index
  33.  
  34. System.out.println("luxuryCars after adding>>"+luxuryCars);
  35. //luxuryCars after adding>>[BMW, Audi, MercedesBenz, Porsche, Jaguar, Duesenberg, RollsRoyce, RollsRoyce]
  36. System.out.println(luxuryCars.size());//8
  37. //How to use for-each over List
  38. for (String car : luxuryCars) {
  39. System.out.println(car);
  40. }
  41. }
  42. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:6: error: class CommonOpetationWithList is public, should be declared in a file named CommonOpetationWithList.java
public class CommonOpetationWithList 
       ^
1 error
stdout
Standard output is empty