WebDriver Tutorial : ArrayList Class In Java

In THIS POST, I have described about one and two dimensional array In Java software development. Then question Is what Is arraylist In java and why we need It? Interviewer can ask you this question then you can give him answer like : arraylist Is a class that Implements list Interface In java software development language which supports dynamic array means size of array can grow as needed. As you know, We can create only fixed size array to store values Inside It and that Is the limitation/drawback of array because when you don't know how many values you needs to store In array then how can you provide/define size of array? ArrayList helps us to overcome this limitation of array because It can re-size automatically by Itself.

So finally we can say that when you don't know how many values you have to store In array, You can use ArrayList at place of simple array in java software program. We have to use arraylist many times In our selenium webdriver software automation  test case development. Example : Store all links from page, store all buttons Id from page etc.. In all such case, You will not aware about how many values you have to store.

Arraylist hierarchy in java

VIEW NEXT POST to learn about hashtable In Java software development language for selenium webdriver

Bellow given example will show you how to create and work with arraylist class In java. Same thing we will use In selenium WebDriver test case development to store values.

//Import ArrayList class header file
import java.util.ArrayList;

public class ArrayList_Example {

 public static void main(String[] args) {
  //Create object of ArrayList class. It will store only string values.
  ArrayList<String> Sample = new ArrayList<String>();
  //Now you can store any number of values In this arraylist as bellow. Size constrain will comes never.
  Sample.add("button1"); //Putting an Item In arraylist at Index = 0.
  Sample.add("button2"); //Putting an Item In arraylist at Index = 1.
  Sample.add("button3"); //Putting an Item In arraylist at Index = 2.
  Sample.add("button4"); //Putting an Item In arraylist at Index = 3.
  
  for(int i=0; i<Sample.size();i++){//loop will execute till size of arraylist.
   System.out.println(Sample.get(i)); //print arraylist values one by one.
  }
  
  System.out.println("*************************");
  //To get the Index of an Item from arraylist.
  int ItemIndex = Sample.indexOf("button3"); 
  System.out.println("Index Of button3 Item = "+ItemIndex); 
  System.out.println("*************************");
    
  Sample.remove(1);//To remove an Item from arraylist.  
  for(int i=0; i<Sample.size();i++){
   System.out.println(Sample.get(i)); 
  }
  System.out.println("*************************");
  
  Sample.set(2, "Button8");//To reset value of an arraylist item.  
  for(int i=0; i<Sample.size();i++){
   System.out.println(Sample.get(i)); 
  }
 }
}

In above example, I have described how to created arraylist object, how to add values In arraylist, How to get Index of arraylist Item, How to remove arraylist Item, How to reset arraylist Item. You can do many more actions on arraylist object in java software development language.

4 comments:

  1. Thanks!!! It helped a lot...

    ReplyDelete
  2. can you help me to write the difference between two arrays created by following the above way

    ReplyDelete
  3. array list is not interface ...............

    ReplyDelete
  4. Hi,

    Your tutorials are really great and appreciate your effort on putting this all java concepts together.
    Your comments within the code really helps to understand the code. Why don't you change the font color (light green) of comments given inside the code. It's not that effective and hard to read at times.

    Thanks,
    Sathiya

    ReplyDelete