DEV Community

Deepikandas
Deepikandas

Posted on

#34 Known is a Drop! Array List Qs

1.How to create a arraylist object in java ?
Answer:ArrayList list= new ArrayList();

2.Which package should import for using arraylist class?
Answer:import java.util.ArrayList;

3.What inferface implemented in arraylist class ?
Answer :List Interface

4.Can you stored a value as a primitive data in arraylist ?
Answer :No

5.What are the methods you know in arraylist ?
Answer: add(),addAll(),removeAll(),remove(),clear(),contains(),get(),getFirst(),toArray()

6.Can you stored a value as a object in arraylist ?
Answer :yes
7.Can you please add 5 student name in arraylist and write code ?

//Answer :using add() method
public static void main(String[] args) {
        ArrayListDemo ad = new ArrayListDemo();
        String stu[] = { "Suresh", "John", "Thomas", "Mike", "Ara" };
        ArrayList student = new ArrayList();
        for (Object student1 : stu) {
            student.add(student1);
        }

Enter fullscreen mode Exit fullscreen mode

8.How to remove the student in arraylist ? write code ?
Answer :remove(index value) - remove element from particular index
removeAll(collection) - removes all matching elements from another collection
remove(object) - removes first matching element
clear() - removes everything

package arraylist;
import java.util.ArrayList;
public class ArrayListDemo {
        public static void main(String[] args) {
        ArrayListDemo ad = new ArrayListDemo();
        String stu[] = { "Suresh", "John", "Thomas", "Mike", "Ara","Andi" };
        ArrayList student = new ArrayList();
        ArrayList student2 = new ArrayList();
        for (String student1 : stu) {
            student.add(student1);
        }
        System.out.println("Student array list "+student);
        ArrayList student3=student;
        System.out.println("\"Student3 array list "+student3);
        student2.addAll(student);
        System.out.println("Student 2array list  "+student2);
        student.remove(1); //remove(index)removes one element by position
        System.out.println(student);
        student2.removeAll(student);//removes all matching elements from another collection
        student.remove(student3);
        student2.clear();//Remove everything
        System.out.println(student);
        student3.remove("Ara");//removes first matching element
        System.out.println(student3);
    }
}

Enter fullscreen mode Exit fullscreen mode

9.ArrayList can have duplicate value ?
Answer :Yes.It allows.

10.Is ArrayList maintain the insertion order ?
Answer :The order is exactly the same as insertion.

11.How to print the value from arraylist using for loop ?
Answer :If traditional for loop is used,get(index)is used.
for non generic in enhanced for loop- Raw type iteration-Object type is used
for Generic version of for-each loop - Instead of Object,use the type declared for array list

package arraylist;
import java.util.ArrayList;
public class ArrayListDemo {
    public static void main(String[] args) {
        ArrayList student = new ArrayList();
        student.add("Life");
        student.add("Time");
        student.add("Gratitude");
        student.add("Acceptance");
        System.out.println("_________Using Enhanced For");
        // for non generic ArrayList
        for (Object name : student) {
            System.out.println(name);
        }
        System.out.println("_______Using traditional for loop");
        for (int i = 0; i < student.size(); i++) {
            System.out.println(student.get(i));
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

12 How to sort a arraylist in java ?
Answer :
using Collections.sort() for ascending order
package arraylist;

import java.util.ArrayList;
import java.util.Collections;
public class ArrayListDemo {
    public static void main(String[] args) {
        ArrayList student = new ArrayList();
        student.add(90);
        student.add(89);
        student.add(54);
        student.add(9);
        Collections.sort(student);
        System.out.println(student);
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)