In this method i will explain some methods of ArrayList which are used frequently.
1)ensureCapacity():This method is used to increase the size of the ArrayList,if necessary to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
list.ensureCapacity(30);
By above statement the minimum capacity of the list will be 30.
2)get(index):This method is used to get the element at the specified index.If index<0 or index>sizeOfList then IndexOutOfBoundsException will occur.
list.get(2);
This will return element present at index 2.Again if we will put any index which is not in list then exception will occur:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 4
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at examples.ArrayListMethods.main(ArrayListMethods.java:23)
3)hashCode():This method returns the hash code of the given list.
list.hashCode();
output:1351734790
4)indexOf(object):This method searches for the specified object and returns the index of first occurence of that object.If object is not present in the list then it returns -1.
list.indexOf("Brown");
This will return 3.
5)isEmpty():This method is used to test whether the list is empty or not.If the list is empty it will return true.
list.isEmpty();
Program Code:
package examples;
import java.util.ArrayList;
public class ArrayListMethods {
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");
list.add("Orange");
list.add("Red");
list.add("Brown");
System.out.println("After adding elements list is "+list );
list.ensureCapacity(30);
String str=list.get(3);
System.out.println("Element at index 3 is: "+str);
System.out.println(list.hashCode());
int loc=list.indexOf("Brown");
if(loc>=0){
System.out.println("The element Brown is present at location "+loc);
}
else{
System.out.println("The element Brown is not present in the list");
}
System.out.println("List is Empty: "+list.isEmpty());
}
}
Output:
This is an example of add method in ArrayList
After adding elements list is [First List, Orange, Red, Brown]
Element at index 3 is: Brown
1351734790
The element Brown is present at location 3
List is Empty: false
So above is all about some methods of ArrayList which can be used for convenience.If there is any query ask me via comment or email.
1)ensureCapacity():This method is used to increase the size of the ArrayList,if necessary to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
list.ensureCapacity(30);
By above statement the minimum capacity of the list will be 30.
2)get(index):This method is used to get the element at the specified index.If index<0 or index>sizeOfList then IndexOutOfBoundsException will occur.
list.get(2);
This will return element present at index 2.Again if we will put any index which is not in list then exception will occur:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 4
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at examples.ArrayListMethods.main(ArrayListMethods.java:23)
3)hashCode():This method returns the hash code of the given list.
list.hashCode();
output:1351734790
4)indexOf(object):This method searches for the specified object and returns the index of first occurence of that object.If object is not present in the list then it returns -1.
list.indexOf("Brown");
This will return 3.
5)isEmpty():This method is used to test whether the list is empty or not.If the list is empty it will return true.
list.isEmpty();
Program Code:
package examples;
import java.util.ArrayList;
public class ArrayListMethods {
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");
list.add("Orange");
list.add("Red");
list.add("Brown");
System.out.println("After adding elements list is "+list );
list.ensureCapacity(30);
String str=list.get(3);
System.out.println("Element at index 3 is: "+str);
System.out.println(list.hashCode());
int loc=list.indexOf("Brown");
if(loc>=0){
System.out.println("The element Brown is present at location "+loc);
}
else{
System.out.println("The element Brown is not present in the list");
}
System.out.println("List is Empty: "+list.isEmpty());
}
}
Output:
This is an example of add method in ArrayList
After adding elements list is [First List, Orange, Red, Brown]
Element at index 3 is: Brown
1351734790
The element Brown is present at location 3
List is Empty: false
0 comments:
Post a Comment