List, Set and Map -1


1. What is an array?
A. Array:  It is a collection of similar elements where the memory is allocated sequentially elements in the array are 

referred using Index Value. Always the index value start with Zero.
      Syntax: Datatype[] arrayName;
                  String[] names;
                  Integer[] ages;
                  Decimal[] salary;

2. Elements in the are referred using?
A. Elements in the array are referred using Index Value. Always the Index value start with zero.

3. What are the drawbacks of array? 
A. An Array Notation of a list using [] can only be one dimensional.

4.What is a List? 
A. List: List is an ordered collection of elements which will allow duplicates.
    Syntax:
    List<datatype> listName = new List<datatype>();
    – The datatype allows both primitive datatypes and non-primitive datatypes
    – Size of the list is dynamically increased.
     List index starts from zero.
5. Syntax of List?
A: List<datatype> listName = new List<datatype>();

6. Will List maintain the insertion order?
A. Yes, Elements in the list are inserted sequentially based on the Insertion Order.

7. Are the duplicates are allowed in the list?
A. Yes, Duplicate values are Accepted in the List.

8. How the elements in the list are reffered?
A. List elements can be accessed with an Index.// List Key Word

9. What is difference between add() and addAll() methods?
A. add() : This method is used to insert new element into the list.
                       Syntax: List<Datatype> listName=new List<Data type> ();
                     listName.add(Datatype Value);
   addAll(): This method is used to add list or set of elements to a list.
                       Ex: List<Integer> ages=new List<Integer>();
                            ages.add(19);
                              ages.add(20);
                           List<Integer> values=new List<Integer>{22,30};
                                ages.addAll(values); output= {19,20,22,30}

10. What is the difference between add(index,ele) and set(index,ele)? 
A. add(Index, ele): This method is used to add element at given Index.
     Note: Index value should be less than the size of the list.
         List<Integer> ages=new List<Integer>();
          ages.add(10); ages.add(20);ages.add(0,15); ages.add(1,25); output={15,25,10,20}
set(Index, ele): This will replace the element in the given index by the given element.
    Ex: List<Integer> ages=new List<Integer>{10,20,30};
           ages.set(1,90);
            output: {10, 90, 30}

11. How the size of a list?
A. Size(): This method will return no.of elements in the list.
       Ex: List<integer> ages=new List<integer>();
           Ages.add(10);
           ages.add(20);
               Integer count=ages.size();
                   Output: size:2;

12. How to sort the elements in the list in the ascending order?
A. Sort(): This method will sort all the elements in the list in ascending order.
              Ex: List<integer> ages=new List<Integer>();
                  ages.add(12); ages.add(52); ages.add(62); ages.add(21);
                 ages.sort();
  output: 12,21,52,62

13.How to sort the elements in the decending order? 
A. desending Order of List 
   List<String> sampleList =new List<String>();
sampleList.add('Andrew');
sampleList.add('Sammy');
sampleList.add('Yogut');
sampleList.sort();  // This function always results in asc order.
 

List<String> finalList = new List<String>();
for(Integer i = sampleList.size()-1; i>=0;i--)
{
   finalList.add(sampleList.get(i));
}

System.debug('Check the Order -->'+finalList); // This should be in descending order

Comments

Post a Comment

Popular posts from this blog

Page Layouts

SOQL Scenario-1