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

Thursday, 12 March 2015

ArrayList Methods in Java

 Earthcare Foundation NGO     10:14     Collection, Java     No comments   

In this pots i will explain some methods of ArrayList.
1)clone:This method is used to make a exact copy of list.This method returns object so we need to add casting to it.
Suppose list is an ArrayList and we have added some elements to it.

ArrayList<String> list=new ArrayList<String>();

list.add("First List");

list.add(1, "I");

list.add(0, "Abhishek");

So apply clone method on it
ArrayList<String> list1=new ArrayList<String>();
list1=(ArrayList<String>) list.clone();//to get the exact copy

2)clear():This method is used to clear the list means all the data present in the list will be lost and list will become empty.
list.clear();

3)contains():This method accepts object as argument to chech whether it is present in the list or not.It returns true if element is present else returns false.
boolean exist=list.contains("Abhishek");

4)containsAll():This method accepts collection as its argument and checks all the elements with the current collection to check whether every element in both of these collections are same or not.If every element is present then it returns true else returns false.
exist=list.containsAll(list1);
Note:The presence of elements of passed list is searched.
for example in above statement if list contains all the elements which are present in list1 then it will return true.It will not check the presence of all elements of list in list1.
The whole concept explained above is explained in the following program:

package examples;

import java.util.ArrayList;

public class ArrayListOther {

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 );

ArrayList<String> list1=new ArrayList<String>();
list1=(ArrayList<String>) list.clone();
System.out.println("The cloned list of list is "+list1);

boolean exist=list.contains("Abhishek");
System.out.println("List contains element Abhishek: "+exist);
list.add("hello");
exist=list.containsAll(list1);
System.out.println("List contains all elements of other list : "+exist);

list.clear();
System.out.println("After removing all the elements using clear method list is"+list);

}

}

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]
The cloned list of list is [Abhishek, First List, I]
List contains element Abhishek: true
List contains all elements of other list : true
After removing all the elements using clear method list is[]

In the above example you can see that after cloning list and list1 both contains same elements and after that i have added another element in list and after that i have called list.containsAll(list1) and it is returning true since elements if list1 will be checked in list.
So till now i have explained some methods of ArrayList class.Hope you are able to understand.If there is any problem then you can ask me by comment 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