java collection

Java Collection: ArrayList

ArrayList is one java collection class. We can store any type of object data in ArrayList. In this blog, we will see how to store Integer data in ArrayList Class, display ArrayList and use of ArrayList object data.

 

How to declare ArrayList Class?

ArrayList<Generics> object = new ArrayList<Generics>();

Example: ArrayList<Integer> list1 = new ArrayList<Integer>();

 

How to add data in ArrayList Class?

object.add(new Generics(value));

Example: list1.add(new Integer(67));

 

Let’s see how to use ArrayList class in java collection programming.

 

import java.util.*;// import util package

class collection1

{

                public static void main(String args[])

                {

                                // Dec of ArrayList Class

ArrayList<Integer> list1 = new ArrayList<Integer>();       

 

                                // How to Add data in ArrayList

                                list1.add(new Integer(56));

                                list1.add(90);

                                list1.add(new Integer(99));

 

                                // How to display ArrayList ?

                                System.out.println(list1); // [56,90,99]

 

                                // How to use ArrayList data in programs???

                                int sum=0;

                                for(Integer obj : list1)     // Foreach list1 data store in obj object

                                {

                                                sum = sum + obj;

                                }

                                System.out.println(“Sum = ” + sum);

                }

}

 

Output

[56,90,99]

Sum = 245

 

 

Video on ArrayList


For more videos on Java  visit our YouTube Channel


Earn Money Online

532total visits,2visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Comments are closed.