RnewGrab Deal : Flat 23% off on live classes + 2 free self-paced courses as a bonus! - SCHEDULE CALL Rnew

- Java Blogs -

Java Print ArrayList with Examples

Array List in Java are the important data structures that can be extended to accommodate a greater number of elements and shrink back by removing items from the array when not needed. This is highly important if you wanted to handle elements within array dynamically. Here is an example of how to print the whole array list in Java –

System.out.println(“Whole List=”+ ListTest);

What is Array List in Java?

Java Array List is the resizable array or the implementation of List interface that grows automatically as requirement grow, so performance is always better in case of the single threaded environment.

Wondering how are Array List in Java different from the Arrays and Lists in Java? Look at the following example for a better understanding. See the given picture where one man stretching the rubber band and it can be extended to higher length than the actual one to accommodate a number of objects within rubber band.

However, the actual size of the rubber band is much smaller if it is not stretched. At the same time, take one rope as another example that cannot be stretched and whose size will remain fixed always. Here, the rubber band is the Array list where elements can be added when required and shrink back if not required and the rope is an Array.

Java Print ArrayList with Examples

Read: What is Java Exponent? How to Do Exponents in Java?

The important facts related to an Array List include here –

  • They do contain duplicate elements and maintain the insertion order. They can also be defined as null as well.
  • Array List cannot be synchronized and allows random access when array works at the index basis.
  • In the case of Array List, manipulation is sometimes slower because it takes a lot of time in removing any particular element from the list.
  • They are not safe in a threaded environment so extra care needs to perform when executed in a multithreaded environment.
  • If you wanted to add objected then they are always maintained in a specific order. To retrieve the first object from an array list, call the index with object 0.
  • The default capacity of an array is taken as 10 in Java. However, the value can be revised anytime by calling the “ensureCapacity (int minCapacity)” method.
  • Once iterators are modified, they can be called by iterator add or remove methods only otherwise it may throw exceptions.
  • The random access is possible in the case of Java Array List as it is working on the basis of the index and you can access elements anytime when needed.
  • Further, array list in java does support Generics and this is the best way of creating an array list in Java.

The basic syntax for java array list look like this as given below –

Java Print ArrayList with Examples The diagram given above shows the hierarchy of an array list in Java where Array List extends the Abstract class and the abstract class implements List first then collection and the Iterables, in the end, Java collection frameworks were non-generic before JDK 1.5 and after this version, they went to generic. In the generic collection, you could define only one type of elements or objects in the program. Today, it has gone safe too so there is no need for the typecasting. How array list was created in the older version (Non-Generic) – Java Print ArrayList with Examples How array lists are created in the current version (Generic) – Now data type in modern array lists is given in angular braces. They are forced to store a particular type of objects only. If you will try to add any other data type then it will show the compile-time error.

Constructors in Java Array List

It contains three constructors majorly – ArrayList (), ArrayList (Collection<? Extends E> c), and ArrayList (int initialCapacity).

  • ArrayList () – This is a most frequently used Java constructor that will return an empty array with a capacity of 10.
  • ArrayList (Collection<? Extends E> c) – Here, the constructor will return a list of specific elements in the same order as they are returned by the iterator. It will throw an exception If the collection argument is NULL.
  • ArrayList (int initialCapacity) – This constructor is useful if you wanted to declare an array with a large capacity to store a huge amount of data.

Methods in Java Array List

There are a plenty of methods that can be used along one array list. Here, we have listed a few only that are used frequently by programmers and necessary to learn by aspirants.

  1. Array List add () – This method is used to add elements to an array. If an array already has few elements and you wanted to add more then you can use this method and keep adding the elements unless the syntax is specified.
  2. Syntax: add (Object o);

  3. Array List Remove () – To delete a specific element from the list, this specific method is used to reduce the size of an array accordingly. You can also delete an element based on its index.
  4. Syntax: remove (Object o);

  5. Java array size () – This method specifies the total number of elements for the array list. Like other, the first element will be started with index ().
  6. Syntax: int size ();

    Read: Java Access Modifiers - Public, Private, Protected & Default
  7. Array List Contains – Here, the output will be true if the list contains any specified element.

Syntax: boolean contains (Object o);

Array and Array List Compared

The discussion is incomplete if you don’t know the differences among array and array list in Java and which one to choose among two. Here, both are compared on eight major points like performance, resizable, primitives, traversal, length, type safety, add elements, and multi-dimensional.

Resizable –

  • Arrays are the fixed data structure and static in nature. One cannot change the size of an array once it is defined.
  • At the same time, array lists are dynamic in nature associated with an object whose size can be changed as needed. When you start adding elements into an array list, its size will grow automatically.

Performance –

  • The performance of an array and array list depends on the operations you are performing. For example, when the size of an array list is resized, it slows the performance automatically.
  • The performance for adding or removing elements into an array or array lists is almost the same in both the cases.

Primitives –

  • Array List does not contain any primitive data type but it contains only objects to store data.
  • At the same time, arrays have primitive data types that include char, int, byte, short, long, double, float etc. Users have a misconception that array list could store primitive data types too but this is not true.

Iteration – To iterate through the array list, an iterator is required. We can use either for loop or each loop to iterate through array elements.

Read: A comprehensive guide for Java Developers Roles and Responsibilities | Updated!

Type Safety – In Java, safety is embedded through Generics. At the same time, array lists are the homogeneous data structure thus it contains values of a particular data type only. If you will try to store any different value then it will show the compile-time error in that case.

Length – To give the size of an array list, you may use the size () method. Each array has a variable-sized object that will return the fixed length in the end.

Integer arrayobject[] = new integer[3];

Arraylength = arrayobject.length; //uses arrayobject length variable

Arraylist arraylistobject = new arraylist();

Arraylistobject.add(12);

Arraylistobject.size();

Add the Elements – To insert elements in the array list, add () method is used and it will insert new elements in the list with the assignment operator.

Multi-dimensional – The array could be multi-dimensional but array lists are always one dimensional only.

Here are some similarities between the array and array list -

  • Performance for getting and adding elements in the array and array lists are the same. Both run constantly at the executions time.
  • Both are containing duplicate elements and they can store NULL values too.
  • The index is used to refer elements in an array and the array list. There is no guarantee of ordered elements in both the cases.

With this discussion, we come to an end of this blog. Now you had enough information about Array List in Java and how to print the list, add or create elements to an existing array based on requirement. To know more, you should join Java certification training program for practical experience and be an established Java programmer too.

Read: Node.JS Interview Questions and Answers for Fresher, Experienced


fbicons FaceBook twitterTwitter google+Google+ lingedinLinkedIn pinterest Pinterest emailEmail

     Logo

    Janbask Training

    A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.


  • fb-15
  • twitter-15
  • linkedin-15

Comments

Trending Courses

AWS Course

AWS

  • AWS & Fundamentals of Linux
  • Amazon Simple Storage Service
  • Elastic Compute Cloud
  • Databases Overview & Amazon Route 53
AWS Course

Upcoming Class

6 days 31 Mar 2023

DevOps Course

DevOps

  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing
DevOps Course

Upcoming Class

5 days 30 Mar 2023

Data Science Course

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning
Data Science Course

Upcoming Class

-0 day 25 Mar 2023

Hadoop Course

Hadoop

  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation
Hadoop Course

Upcoming Class

6 days 31 Mar 2023

Salesforce Course

Salesforce

  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL
Salesforce Course

Upcoming Class

-0 day 25 Mar 2023

QA Course

QA

  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing
QA Course

Upcoming Class

6 days 31 Mar 2023

Business Analyst  Course

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum
Business Analyst  Course

Upcoming Class

-0 day 25 Mar 2023

MS SQL Server Course

MS SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design
MS SQL Server Course

Upcoming Class

-0 day 25 Mar 2023

Python Course

Python

  • Features of Python
  • Python Editors and IDEs
  • Data types and Variables
  • Python File Operation
Python Course

Upcoming Class

7 days 01 Apr 2023

Artificial Intelligence  Course

Artificial Intelligence

  • Components of AI
  • Categories of Machine Learning
  • Recurrent Neural Networks
  • Recurrent Neural Networks
Artificial Intelligence  Course

Upcoming Class

-0 day 25 Mar 2023

Machine Learning Course

Machine Learning

  • Introduction to Machine Learning & Python
  • Machine Learning: Supervised Learning
  • Machine Learning: Unsupervised Learning
Machine Learning Course

Upcoming Class

13 days 07 Apr 2023

Tableau Course

Tableau

  • Introduction to Tableau Desktop
  • Data Transformation Methods
  • Configuring tableau server
  • Integration with R & Hadoop
Tableau Course

Upcoming Class

14 days 08 Apr 2023

Search Posts

Reset

Receive Latest Materials and Offers on Java Course

Interviews