ArrayList-in-Java-768x497

What is an ArrayList?

ArrayList is a resizable array in Java. It is implementation of the List interface.
Array is fixed in size, whereas ArrayList can grow or shrink dynamically as needed to accommodate adding and removing elements.

ArrayList in Java

ArrayList is random access list. It supports fast and generally constant access time for the elements. You can add duplicate elements to the list.

Array List is not thread safe. So, if multiple threads access concurrently, and when threads attempt to modify the list, it must be synchronized externally. For example, thread operation that adds or deletes one or more elements in the list.

The list order is maintained while adding and removing elements to the list. For example, when an element is inserted at specified position in the list, the other elements are shifted to the right.

Sample Program

Simple program to add elements to the list is shown below:

import java.util.ArrayList;
import java.util.Scanner;

public class ArrayListExample {
    public static void main(String[] args) {
        Scanner input = null;
        try{
            input = new Scanner(System.in);
            ArrayList<Integer> arrList =new ArrayList<Integer>();
            System.out.println("Add elements to list");
            while(input.hasNextInt()){
                arrList.add(Integer.parseInt(input.nextLine()));
            }

            System.out.println("Input ArrayList is:");
            for (Integer a : arrList) {
                System.out.print("[" + a.intValue() + "]");
            }
        }
        catch(Exception e){
            e.printStackTrace();
            System.out.println("Unexpected Error...!");
        }
        finally
        {
            if (input != null) {
                input.close();
            }
        }
    }
}

 

Run output of the program

ArrayList sample program

 

56
99
65
2
45
56
1
stop
Input ArrayList is:
[56][99][65][2][45][56][1]

Related Posts

Google-Auto-Suggestion-Box-1024x555

Automation

Write a java program to capture auto suggestions list

Tomcat-deployment

Automation

How to deploy a .WAR file in Tomcat using Eclipse?

Servlet-Running-on-Tomcat

Automation

How to create a Sample Servlet using Eclipse?

String-literal-common-pool-1024x586

Automation

What is the difference between String Literal and String Object?

2FA-GitHub-account-1024x363

Automation

How to enable 2FA in GitHub?

Simple-JavaFX-Application-1024x524

Automation

What is JavaFX API?

Navigation

  • Home
  • Selenium
  • JBehave
  • TestNG

Random Questions

  • Triangle with known sides a and bCalculate Third side of Triangle puzzle?
  • Write a simple java program to traverse a single linked list?
  • ssh communicationWhat is JSch?
  • Setters_and_Getters_JavaBeanHow to generate setter and getter automatically for a Bean?
  • Google-Auto-Suggestion-Box-1024x555Write a java program to capture auto suggestions list
  • What is GitHub Desktop?
  • Linear Search Flow ChartWrite a java program for linear search an unsorted array?
  • What is the difference between Web Service and Micro service?What is the difference between Web Service and Micro service?
  • Use Mathematical Induction to prove the statement
  • How to create a repository on GitHub?
  • What are Unstaged and Staged changes in Git?
  • How to commit changes to Git Repository?
  • StringTokenizer Class HeirarchyWhat is StringTokenizer?
  • Write a program to convert date format from one Timezone to another?Write a program to convert date format from one Timezone to another?
  • Selection sortWrite a java program for selection sort?
TestingDocs.com
  • Home
  • Selenium
  • JBehave
  • TestNG
© 2016 - 2019 TestingDocs.com All Rights Reserved
TestingDocs.com

TestingDocs.com

Links

  • Home
  • Selenium
  • JBehave
  • TestNG

Search