Hub Of Geekz

  • Home
  • About Me
  • Contact Us
  • Home
  • Languages
    • C++
    • Java
    • Perl
    • Prolog
    • Bootstrap
  • Database
    • SQL
    • PL/SQL
  • Study
    • Java
      • Java Collection
      • Java Concurrency
      • Java Interview Questions
      • Java Networking
    • Number System
  • Kavita
  • Entertainment
    • Hinglish Cafe
    • Videos
    • Jokes
  • Windows Tricks
  • How To

Tuesday, 10 March 2015

ArrayList in Java

 Earthcare Foundation NGO     10:14     Collection, Java     No comments   

Java API provides several predefined data structure which are also known as collections in Java.ArrayList is a collection which we can use conveniently in place of array because when we are using array there is a problem that array size cannot be changed automatically at runtime i.e. if we are using an array and it became full then it will throw an exception instead of increasing its size.This problem can be solved by using ArrayList.There are also other benefits as there are lot of inbuilt methods which are convenient to use for adding element,deleting an element,searching an element etc.List can also contain null and duplicate elements.So lets get some knowledge of methods of ArrayList.
1)add(element):This method is used to add an element at the last position of ArrayList.This is an efficient method to add an element in ArrayList.
2)add(index,element):This method is used to add an element at the specified position in the ArrayList.This method is not so much efficient as the elements after that position have to shift one position.
for ex list=2,3,6,7
list.add(2,56); //add 56 at 2nd position
 then list=2,3,56,6,7
So whenever possible use add(element) method for efficeincy.
3)addAll(Collection<?extends E>):This method is used to append all the elements of other collection at the end of the list.
4)addAll(index,Collection<?extends E>):This method is used to append all elements of other collection at specific position in the list.

Program Code:
package examples;

import java.util.ArrayList;

public class ArrayListExample {

public static void main(String[] args) {

ArrayList<String> list=new ArrayList<String>();
System.out.println("This is an example of add method in ArrayList");
list.add("First List");
System.out.println("After adding elements list is "+list );
list.add(1, "I");
System.out.println("After adding elements list is "+list );
list.add(0, "Abhishek");
System.out.println("After adding elements list is "+list );
System.out.println("List can also contain duplicate elements");
list.add("I");
System.out.println("After adding elements list is "+list );

ArrayList<String> list1=new ArrayList<String>();
list1.add("Second List");
System.out.println("After adding elements list is "+list1 );
list1.addAll(list);
System.out.println("We can append other list to this list");
System.out.println("After appending other list, elements of list are "+list1 );

ArrayList<String> list2=new ArrayList<String>();
list2.add("Third List");
list2.add("Hello");
System.out.println("We can also insert other list at specific location");
list2.addAll(1, list1);
System.out.println("After appending other list, elements of list are "+list2 );

}

}

Output:
This is an example of add method in ArrayList
After adding elements list is [First List]
After adding elements list is [First List, I]
After adding elements list is [Abhishek, First List, I]
List can also contain duplicate elements
After adding elements list is [Abhishek, First List, I, I]
After adding elements list is [Second List]
We can append other list to this list
After appending other list, elements of list are [Second List, Abhishek, First List, I, I]
We can also insert other list at specific location
After appending other list, elements of list are [Third List, Second List, Abhishek, First List, I, I, Hello]
So above are some methods to add elements in the ArrayList.
Hope everything is clear,if not then ask me via comments or email
Thanks.
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Ad


Jobsmag.inIndian Education BlogThingsGuide

Subscribe

Do you want fresh and hot updates about us? Then subscribe to our Feeds.

Total Pageviews

Popular Posts

  • Write a program in PL/SQL to print the factorial of a number.
    In this post I will explain how to get the factorial of any given number. For that first you need to know what is the procedure to find ...
  • To find the GCD of two numbers in PROLOG.
    gcd(X,Y):-X=Y,write('GCD of two numbers is '),write(X); X=0,write('GCD of two numbers is '),write(Y); Y=0,write('G...
  • Write a PL/SQL code to get the Fibonacci Sequence
    First, I will explain what is Fibonacci Sequence and how to get this series. So, Fibonacci Sequence is a series of numbers 0,1,1,2,3,5,8,1...

Label

All Articles Best Resources Blogging Boost Traffic Bootstrap C Plus Plus Collection Comedy Comedy Posts Comedy Videos Concurrency creative commons website Education Employee Entertainment Fibonacci Sequence free images GirlFriend Hinglish Cafe How To Image Websites Inspirational Java Java Interview Questions Java Networking Kavita Sangrah Life Lock Sreen Love Number System Patterns Perl Picture PL/SQL Plastic Engineering Programming Prolog public domain SEO Servlet Short Story Shortcut Keys Social Media Social Services SQL SuVichar Thread Traffic True Events Ultimate Guide Windows Tricks Windows8.1 WordPress

Blog Archive

  • ►  2020 (43)
    • ►  September (41)
    • ►  August (2)
  • ►  2019 (1)
    • ►  July (1)
  • ►  2018 (9)
    • ►  September (7)
    • ►  July (1)
    • ►  May (1)
  • ►  2017 (8)
    • ►  June (3)
    • ►  May (3)
    • ►  March (1)
    • ►  January (1)
  • ►  2016 (2)
    • ►  September (1)
    • ►  January (1)
  • ▼  2015 (91)
    • ►  December (1)
    • ►  November (1)
    • ►  October (6)
    • ►  May (10)
    • ▼  March (20)
      • ArrayList Methods in Java
      • ArrayList Methods in Java
      • ArrayList in Java
      • Arrays Class in Java
      • Java Interview Question-Variable length arguments ...
      • Array in Java
      • Data Hierarchy in Operating Systems
      • Synchronization in Java
      • Typecasting in Java
      • Concurrency in Java
      • Runnable in Java
      • HashMap in Java
      • Reverse of a given number using Java
      • Star Pattern printing in Java
      • Difference between fail-fast and fail-safe iterators
      • Factorial of a number in Java
      • Null in Java
      • Final in Java
      • Immutable Objects in Java
      • Thread concept in java
    • ►  February (50)
    • ►  January (3)
  • ►  2014 (339)
    • ►  December (1)
    • ►  October (55)
    • ►  September (58)
    • ►  August (94)
    • ►  July (64)
    • ►  June (67)
  • ►  2013 (34)
    • ►  August (5)
    • ►  April (29)
  • ►  2012 (20)
    • ►  November (1)
    • ►  October (15)
    • ►  September (4)

Author

  • Earthcare Foundation NGO
  • Kavita house
  • Unknown
  • Unknown

Info

Copyright © Hub Of Geekz | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates