Z`List Operation in Apex

I am going to show you how to do operation like (Add(), sort(),addall(), clear(),clone) in a list.

Developer Console

developer console

Apex List and Method

  1. //ADD,sort
  2. List<Integer> IntList = new List<Integer>();//Here list type integer
  3. IntList.add(4);// Adds an element to the end of the list.
  4. IntList.add(6);
  5. IntList.add(0,8);// Inserts an element into the list at the specified index position.
  6. IntList.add(1,9);
  7. IntList.sort();
  8. For(Integer c:IntList)
  9. {
  10. System.debug(c); //System.debugs():-Its Help to print the Data as like Printf,system.write() in .net
  11. }
  12. //addALL()
  13. List<Integer> IntList=new List<Integer>();
  14. IntList.Add(2);
  15. List<Integer> IntList2=new List<Integer>();
  16. IntList2.Add(3);
  17. List<Integer> IntListMaster=new List<Integer>();
  18. IntListMaster.addAll(IntList);
  19. IntListMaster.addAll(IntList2);
  20. IntListMaster.sort();
  21. For(Integer DD:IntListMaster)
  22. {
  23. system.debug(DD);
  24. }

Find here the debug result.

result debug

  1. //clear():Removes all elements from a list, consequently setting the list's length to zero.
  2. List<Integer> IntList=new List<Integer>();
  3. IntList.Add(2);
  4. List<Integer> IntList2=new List<Integer>();
  5. IntList2.Add(3);
  6. List<Integer> IntListMaster=new List<Integer>();
  7. IntListMaster.addAll(IntList);
  8. IntListMaster.addAll(IntList2);
  9. IntListMaster.clear();
  10. List<Integer> IntList3=new List<Integer>();
  11. IntList3.Add(5);
  12. IntListMaster.addAll(IntList3);
  13. IntListMaster.sort();
  14. For(Integer DD:IntListMaster)
  15. {
  16. system.debug(DD);
  17. }

Find here the debug result.

debug result

  1. List<Integer> IntList=new List<Integer>();
  2. IntList.Add(2);
  3. List<Integer> IntList2=new List<Integer>();
  4. IntList2.Add(3);
  5. List<Integer> IntListMaster=new List<Integer>();
  6. IntListMaster.addAll(IntList);
  7. IntListMaster.addAll(IntList2);
  8. IntListMaster.clear();
  9. List<Integer> IntList3=new List<Integer>();
  10. IntList3.Add(5);
  11. IntListMaster.addAll(IntList3);
  12. IntListMaster.sort();
  13. List<Integer> IntListMaster1=new List<Integer>();
  14. IntListMaster1=IntListMaster.clone();
  15. system.debug(IntListMaster1.size());
  16. For(Integer DD:IntListMaster1)
  17. {
  18. system.debug(DD);
  19. }