- 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 4338 Articles for Java 8
120 Views
The isEmpty() method of the class java.util.ArrayList returns true if this list contains no elements.Exampleimport java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { ArrayList arrlist = new ArrayList(5); arrlist.add(25); arrlist.add(10); arrlist.add(20); arrlist.add(35); boolean retval = arrlist.isEmpty(); if (retval == true) { System.out.println("list is empty"); } else { System.out.println("list is not empty"); } for (Integer number : arrlist) { System.out.println("Number = " + number); } } }Outputlist is not empty Number = 25 Number = 10 Number = 20 Number = 35
217 Views
The capacity() method is used to return the current capacity of a vector. Capacity is the length of the array kept in the vector.Exampleimport java.util.Vector; public class VectorDemo { public static void main(String[] args) { Vector vec = new Vector(); vec.add(14); vec.add(13); vec.add(12); vec.add(11); vec.add(4); vec.add(3); vec.add(2); vec.add(1); System.out.println("Capacity of the vector is :"+vec.capacity()); } }OutputCapacity of the vector is :10
651 Views
The size() method of the class java.util.ArrayList returns the number of elements in this list i.e. the size of the list.Exampleimport java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { ArrayList arrlist = new ArrayList(5); arrlist.add(15); arrlist.add(20); arrlist.add(25); arrlist.add(22); for (Integer number : arrlist) { System.out.println("Number = " + number); } int retval = arrlist.size(); System.out.println("Size of list = " + retval); } }OutputNumber = 15 Number = 20 Number = 25 Number = 22 Size of list = 4
89 Views
The removeAllElements() method is used to remove all components from this vector and sets its size to zero. This method is identical in functionality to the clear method.Exampleimport java.util.Vector; public class VectorDemo { public static void main(String[] args) { Vector vec = new Vector(4); vec.add(4); vec.add(3); vec.add(2); vec.add(1); System.out.println("Added numbers are :- "); for (Integer number : vec) { System.out.println("Number = " + number); } System.out.println("Size of ... Read More
59 Views
The removeElementAt(int index) method is used to delete the component at the specified index. Each component in this vector with an index greater or equal to the specified index is shifted downward to have an index one smaller than the value it had previously and the size of this vector is decreased by 1. Example public class VectorDemo { public static void main(String[] args) { Vector vec = new Vector(4); vec.add(4); vec.add(3); ... Read More
98 Views
The lastElement() method is used to return the last component of the vector.Exampleimport java.util.Vector; public class VectorDemo { public static void main(String[] args) { Vector vec = new Vector(4); vec.add(4); vec.add(3); vec.add(2); vec.add(1); System.out.println("Last element: "+vec.lastElement()); } }OutputLast element: 1
93 Views
The firstElement() method is used to return the first component (the item at index 0) of this vector.Exampleimport java.util.Vector; public class VectorDemo { public static void main(String[] args) { Vector vec = new Vector(4); vec.add(4); vec.add(3); vec.add(2); vec.add(1); System.out.println("First element is :"+vec.firstElement()); } }OutputFirst element is :4
171 Views
The elementAt(int index) method is used to get the component at the specified index/location of the vector.Exampleimport java.util.Vector; public class VectorDemo { public static void main(String[] args) { Vector vec = new Vector(4); vec.add(4); vec.add(3); vec.add(2); vec.add(1); System.out.println("Element at 1st position :- "+vec.elementAt(1)); } }OutputElement at 1st position :- 3
286 Views
The addElement(E obj) method is used to add the specified component to the end of this vector and increasing its size by one. The capacity of this vector is increased if its size becomes greater than its capacity. This addElement() method is identical in functionality to the add(Object) method. The add() method returns true/false but the addElement() method does not return any value.Exampleimport java.util.Vector; public class VectorDemo { public static void main(String[] args) { Vector vec = new Vector(4); vec.add(4); vec.add(3); vec.add(2); vec.add(1); System.out.println("Initial ... Read More
317 Views
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.Syntaxswitch(expression) { case value : // Statements break; case value : // Statements break; // You can have any number of case statements. default : // Statements }The following rules apply to a switch statement −The variable used in a switch statement can only be integers, convertible integers (byte, short, char), ... Read More