Java Collection | Set 1 (ArrayList) Part-2 | Practice | GeeksforGeeks

Implement different operations on a ArrayList A .
 
Input:
The first line of input contains an integer T denoting the no of test cases . Then T test cases follow. The first line of input contains an integer Q denoting the no of queries . Then in the next line are Q space separated queries .
A query can be of five types 
1. a x (Adds an element x to the ArrayList A at the end )
2. b (Sorts the ArrayList A in ascending order )
3. c (Reverses the ArrayList A)
4. d (prints the size of the ArrayList)
5. e (prints space separated values of the ArrayList)
5. f  (Sorts the ArrayList A in descending order)


Output:
The output for each test case will  be space separated integers denoting the results of each query . 

Constraints:
1<=T<=100
1<=Q<=100

Example:
Input
2
6
a 4 a 6 a 7 b c e
4
a 55 a 11 d e
 
Output
7 6 4
2 55 11

Explanation :
For the first test case
There are six queries. Queries are performed in this order
1. a 4 { ArrayList has 4  }
2. a 7 {ArrayList has 7 }
3. a 6 {ArrayList has 6}
4. b {sorts the ArrayList in ascending order, ArrayList now is 5 6 7}
5. c {reverse the ArrayList}
6. e {prints the element of the ArrayList 7 6 4}

For the sec test case 
There are four queries. Queries are performed in this order
1. a 55  (ArrayList A has 55}
2. a 11  (ArrayList A has 55 ,11}
3. d      (prints the size of the ArrayList A ie. 2 )
4. e      (prints the elements of the ArrayList A ie 55 11)


Note:The Input/Output format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
 
0
mda976714 months ago
class GfG{   /*inserts an element x at    the back of the ArrayList A */   void add_to_ArrayList(ArrayList<Integer> A, int x)   {// Your code here  A.add(x);   }    /*sort the ArrayList A in ascending order*/   void sort_ArrayList_Asc(ArrayList<Integer> A)   {// Your code here   Collections.sort(A);   }   /*reverses the ArrayList A*/   void reverse_ArrayList(ArrayList<Integer> A)   {// Your code hereCollections.reverse(A);   }   /*returns the size of the ArrayList A */   int size_Of_ArrayList(ArrayList<Integer> A)   {// Your code here   return A.size();   }   /*sorts the ArrayList A in descending order*/   void sort_ArrayList_Desc(ArrayList<Integer> A)   {// Your code here   Collections.sort(A,Collections.reverseOrder());   }   /*prints space separated    elements of ArrayList A*/   void print_ArrayList(ArrayList<Integer> A)   {       // Your code here       for(Integer x:A)       {                  System.out.print(x+" ");       }   }} 
+1
Debojyoti Sinha1 year ago
Debojyoti Sinha
Correct Answer.Correct AnswerExecution Time:0.32
class GfG{    /*inserts an element x at     the back of the ArrayList A */    void add_to_ArrayList(ArrayList<integer> A, int x)    {        A.add(x);    }    /*sort the ArrayList A in ascending order*/    void sort_ArrayList_Asc(ArrayList<integer> A)    {        Collections.sort(A);    }    /*reverses the ArrayList A*/    void reverse_ArrayList(ArrayList<integer> A)    {        Collections.reverse(A);    }    /*returns the size of the ArrayList A */    int size_Of_ArrayList(ArrayList<integer> A)    {        return A.size();    }    /*sorts the ArrayList A in descending order*/    void sort_ArrayList_Desc(ArrayList<integer> A)    {        Collections.sort(A, Collections.reverseOrder());    }    /*prints space separated     elements of ArrayList A*/    void print_ArrayList(ArrayList<integer> A)    {        for(Integer x: A)        {            System.out.print(x + " ");        }    }}
+1
Amir Ansari1 year ago
Amir Ansari
Correct AnswerExecution Time:0.31
class GfG{    /*inserts an element x at     the back of the ArrayList A */    void add_to_ArrayList(ArrayList<integer> A, int x)    {// Your code here   A.add(x);    }
    /*sort the ArrayList A in ascending order*/    void sort_ArrayList_Asc(ArrayList<integer> A)    {// Your code here  Collections.sort(A);    }
    /*reverses the ArrayList A*/    void reverse_ArrayList(ArrayList<integer> A)    {// Your code hereCollections.reverse(A);    }
    /*returns the size of the ArrayList A */    int size_Of_ArrayList(ArrayList<integer> A)    {// Your code here   return A.size();    }
    /*sorts the ArrayList A in descending order*/    void sort_ArrayList_Desc(ArrayList<integer> A)    {// Your code here Collections.sort(A,Collections.reverseOrder());    }
    /*prints space separated     elements of ArrayList A*/    void print_ArrayList(ArrayList<integer> A)    {        // Your code here        for(int i = 0 ;i < A.size();i++){            System.out.print(A.get(i) +" ");        }    }}
0
Shreya Patil2 years ago
Shreya Patil
class GfG{    /*inserts an element x at     the back of the ArrayList A */    void add_to_ArrayList(ArrayList<integer> A, int x)    {// Your code here    A.add(x);    }
    /*sort the ArrayList A in ascending order*/    void sort_ArrayList_Asc(ArrayList<integer> A)    {// Your code here    Collections.sort(A);    }
    /*reverses the ArrayList A*/    void reverse_ArrayList(ArrayList<integer> A)    {        Collections.reverse(A);    }
    /*returns the size of the ArrayList A */    int size_Of_ArrayList(ArrayList<integer> A)    {// Your code here    return A.size();    }
    /*sorts the ArrayList A in descending order*/    void sort_ArrayList_Desc(ArrayList<integer> A)    {// Your code here    Collections.sort(A,Collections.reverseOrder());    }
    /*prints space separated     elements of ArrayList A*/    void print_ArrayList(ArrayList<integer> A)    {        // Your code here        for(int i=0;i<a.size();i++) system.out.print(a.get(i)+"="" ");="" }="" }="" <code="">
0
Pratikshya Mishra2 years ago
Pratikshya Mishra
// { Driver Code Startsimport java.util.Scanner;import java.util.*;
class ArrayList_Collection{public static void main(String args[]){Scanner sc = new Scanner(System.in);int T = sc.nextInt();while(T>0){ArrayList<integer> A = new ArrayList<integer>();int q = sc.nextInt();
while(q>0){char c = sc.next().charAt(0);GfG g = new GfG();if(c == 'a'){int x = sc.nextInt();g.add_to_ArrayList(A,x);}
if(c == 'b')g.sort_ArrayList_Asc(A);
if(c == 'c')g.reverse_ArrayList(A);
if(c == 'd')System.out.print(g.size_Of_ArrayList(A)+" ");
if(c == 'e')g.print_ArrayList(A);
if(c == 'f')g.sort_ArrayList_Desc(A);
q--;}T--;System.out.println();}}}// } Driver Code Ends
/*You are required to complete below methods*/
class GfG{    /*inserts an element x at     the back of the ArrayList A */    void add_to_ArrayList(ArrayList<integer> A, int x)    {        A.add(x);// Your code here    }
    /*sort the ArrayList A in ascending order*/    void sort_ArrayList_Asc(ArrayList<integer> A)    {        Collections.sort(A);// Your code here    }
    /*reverses the ArrayList A*/    void reverse_ArrayList(ArrayList<integer> A)    {        Collections.reverse(A);// Your code here    }
    /*returns the size of the ArrayList A */    int size_Of_ArrayList(ArrayList<integer> A)    {        return A.size();// Your code here    }
    /*sorts the ArrayList A in descending order*/    void sort_ArrayList_Desc(ArrayList<integer> A)    {        Collections.sort(A,Collections.reverseOrder());// Your code here    }
    /*prints space separated     elements of ArrayList A*/    void print_ArrayList(ArrayList<integer> A)    {         for(int i=0;i<a.size();i++) {="" system.out.println(a.get(i)+"="" ");="" }="" your="" code="" here="" }="" }="">
0
Vikram Shekhawat2 years ago
Vikram Shekhawat
/*inserts an element x at     the back of the ArrayList A */    void add_to_ArrayList(ArrayList<integer> A, int x)    {A.add(x);    }
    /*sort the ArrayList A in ascending order*/    void sort_ArrayList_Asc(ArrayList<integer> A)    {Collections.sort(A);    }
    /*reverses the ArrayList A*/    void reverse_ArrayList(ArrayList<integer> A)    {Collections.reverse(A);    }
    /*returns the size of the ArrayList A */    int size_Of_ArrayList(ArrayList<integer> A)    {return A.size();    }
    /*sorts the ArrayList A in descending order*/    void sort_ArrayList_Desc(ArrayList<integer> A)    {Collections.sort(A, Collections.reverseOrder());    }
    /*prints space separated     elements of ArrayList A*/    void print_ArrayList(ArrayList<integer> A)    {       for (int i = 0; i < A.size();i++)       {                 System.out.print(A.get(i)+" ");       }       }}
0
kaushar fatima3 years ago
kaushar fatima
https://ide.geeksforgeeks.o... only first element is being displayed
0
rishi pandey3 years ago
rishi pandey
https://ide.geeksforgeeks.o... tell me what is wrong
0
LAMIAZOHRA MIHOUBI4 years ago
LAMIAZOHRA MIHOUBI
hi guys my code (here: https://practice.geeksforge... is failing at a certain input but I just don't understand why, can anybody help please?here's the input:138a 50 a 277 c c a 404 f c b a 61 c a 740 e e a 95 f d e c e f d f c d a 653 c b c c b d f e f e d d band here's the expected output:61 404 277 50 740 61 404 277 50 740 6 740 404 277 95 61 50 50 61 95 277 404 740 6 6 7 740 653 404 277 95 61 50 740 653 404 277 95 61 50 7 7and here's my output:61 277 404 50 740 61 277 404 50 740 6 740 404 277 95 61 50 50 740 277 95 61 404 6 6 7 740 653 404 277 95 61 50 740 653 404 277 95 61 50 7 7
0
Chaitanya Stndt5 years ago
Chaitanya Stndt
working perfect in eclipse ide:but i having this problemRun Time ErrorException in thread "main" java.util.NoSuchElementExceptionat java.util.Scanner.throwFor(Scanner.java:862)at java.util.Scanner.next(Scanner.java:1485)at java.util.Scanner.nextInt(Scanner.java:2117)at java.util.Scanner.nextInt(Scanner.java:2076)at GfG.<init>(GfG.java:90)at ArrayList_Collection.main(GfG.java:18)
and my code is/*You are required to complete below methods*/
public class GfG {ArrayList<integer> ar = new ArrayList<>();Scanner s = new Scanner(System.in);void add_to_ArrayList(ArrayList<integer> A, int x)    {ar.add(x);    }
void sort_ArrayList_Asc(ArrayList<integer> A)    {Collections.sort(ar);    }
    /*reverses the ArrayList A*/    void reverse_ArrayList(ArrayList<integer> A)    {Collections.reverse(ar);    }
    /*returns the size of the ArrayList A */    int size_Of_ArrayList(ArrayList<integer> A)    {return ar.size();    }
    /*sorts the ArrayList A in descending order*/    void sort_ArrayList_Desc(ArrayList<integer> A)    {    Collections.sort(ar, Collections.reverseOrder());    }
    /*prints space separated     elements of ArrayList A*/    void print_ArrayList(ArrayList<integer> A)    {    for (Integer pro : ar) {pro.toString();}    }
int t =s.nextInt();{//System.out.println("enter queries");for(int i=0; i<t;i++){ int="" q="s.nextInt();" for="" (int="" j="0;" j="" <="" q;="" j++)="" {="" system.out.println("enter="" string");="" string="" choice="s.next();" if(choice.equals("a")){="" system.out.println("enter="" integer");="" int="" n="s.nextInt();" add_to_arraylist(ar,="" n);="" }="" else="" if="" (choice.equals("b"))="" {="" sort_arraylist_asc(ar);="" }="" else="" if="" (choice.equals("c"))="" {="" reverse_arraylist(ar);="" }="" else="" if="" (choice.equals("d"))="" {="" size_of_arraylist(ar);="" }="" else="" if="" (choice.equals("e"))="" {="" print_arraylist(ar);="" }="" else="" if="" (choice.equals("f"))="" {="" sort_arraylist_desc(ar);="" }="" }="" }="" }="" }="">
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.