- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9321 Articles for Object Oriented Programming
2K+ Views
The size of a vector represents the number of components in the vector. The capacity of a vector represents the maximum number of elements the vector can hold.Example:import java.util.*; public class VectorDemo { public static void main(String args[]) { Vector v = new Vector(3, 2); System.out.println("Initial size: " + v.size()); System.out.println("Initial capacity: " + v.capacity()); v.addElement(new Integer(1)); v.addElement(new Integer(2)); v.addElement(new Integer(3)); v.addElement(new Integer(4)); System.out.println("Capacity after four additions: " + v.capacity()); v.addElement(new ... Read More
506 Views
Both Vector and ArrayList implement the List interface, and each of them uses (dynamically resizable) arrays for their internal data structure, similar to using an ordinary array. However, there are many differences between ArrayList and Vector classes hence by reading this article, you will learn what ArrayList and Vector class are and their major difference that helps you to choose which one to opt for. Understanding ArrayList and Vector Class In addition to the Arrays class, Java provides an ArrayList class that can be used to create containers that stores lists of objects. ArrayList is considered to be a growable ... Read More
114 Views
The ensureCapacity(int minCapacity) method of the class java.util.ArrayList increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.Exampleimport java.util.ArrayList; public class ArrayListDemo { public static void main(String args[]) { ArrayList arrlist = new ArrayList(5); arrlist.add(10); arrlist.add(50); arrlist.add(30); arrlist.ensureCapacity(15); for (Integer number : arrlist) { System.out.println("Number = " + number); } } }OutputNumber = 10 Number = 50 Number = 30
57 Views
The lastIndexOf(Object) method of the class java.util.ArrayList returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.Exampleimport java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { ArrayList arrlist = new ArrayList(5); arrlist.add("G"); arrlist.add("E"); arrlist.add("F"); arrlist.add("M"); arrlist.add("E"); System.out.println("Size of list: " + arrlist.size()); for (String value : arrlist) { System.out.println("Value = " + value); ... Read More
2K+ Views
ArrayList doesn't have length() method, the size() method of ArrayList provides the number of objects available in the collection.Array has length property which provides the length or capacity of the Array. It is the total space allocated during the initialization of the array.
99 Views
The indexOf(Object) method of the java.util.ArrayList class returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.Exampleimport java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { ArrayList arrlist = new ArrayList(5); arrlist.add("G"); arrlist.add("E"); arrlist.add("F"); arrlist.add("M"); System.out.println("Size of list: " + arrlist.size()); for (String value : arrlist) { System.out.println("Value = " + value); } ... Read More
289 Views
The search(Object o) method is used to return the 1-based position where an object is on this stack.Exampleimport java.util.*; public class StackDemo { public static void main(String args[]) { Stack st = new Stack(); st.push("Java"); st.push("Source"); st.push("code"); System.out.println("Searching 'code' in stack: "+st.search("code")); } }OutputSearching 'code' in stack:
393 Views
The get(int index) method of the java.util.ArrayList class returns the element at the specified position in this list.Exampleimport java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { ArrayList arrlist = new ArrayList(5); arrlist.add(15); arrlist.add(22); arrlist.add(30); arrlist.add(40); for (Integer number : arrlist) { System.out.println("Number = " + number); } int retval=arrlist.get(3); System.out.println("Retrieved element is = " + retval); } }OutputNumber = 15 Number = 22 Number = 30 Number = 40 Retrieved element is = 40
100 Views
The empty() method is used to test if this stack is or not.Exampleimport java.util.*; public class StackDemo { public static void main(String args[]) { Stack st = new Stack(); st.push("Java"); st.push("Source"); st.push("code"); System.out.println("Is stack empty: "+st.empty()); } }OutputIs stack empty: false