Learning to Code
ArrayList.
Question -
Implementing all methods of ArrayList
Create an ArrayList Num with size 12. add 6 numbers 12,14,65,56,80,100
adjust the size automatically. Show the each item of arraylist iterating.
Remove 100 and find last index of 14.
Check whether 99 belong to ArrayList or not. How to find arraylist is not empty.
Set element 12 to 873 ..use clone method and clear
check if element at 4 index is 98 or not Convert Num to array.
Solution-
import java.util.*;
public class DsArrayList {
public static void main (String Args[]){
ArrayList al= new ArrayList();
ArrayList<Integer> Num = new ArrayList<>(12);
//add method
Num.add(12);
Num.add(14);
Num.add(65);
Num.add(56);
Num.add(80);
Num.add(100);
// Trim method
Num.trimToSize();
// Iterator() method
Iterator <Integer>ir= Num.iterator();
while(ir.hasNext()) {
System.out.println(ir.next()+" ");
}
// Remove Method()
Num.remove(5);
// Index method
Num.indexOf(100);
Num.lastIndexOf(14);
// Contain and empty method
Num.isEmpty();
Num.contains(99);
//Set Method
Num.set(0, 873);
// Clone method
al=(ArrayList)Num.clone();
// clear method
al.clear();
//get method
if(Num.get(4)==98) {
System.out.println("Yeah");
}
// to array and size method
Integer IntegerArray[]= new Integer[Num.size()] ;
IntegerArray=Num.toArray(IntegerArray);
}}
Comments
Post a Comment