What Does the Method lastElement Do in Java

Arushi
Updated on 25-Feb-2020 10:08:06

157 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

What Does the Method removeAllElements Do in Java

Rishi Raj
Updated on 25-Feb-2020 10:07:20

172 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

What Does the Method int Capacity Do in Java

Vikyath Ram
Updated on 25-Feb-2020 10:06:15

401 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

What Does the Method elements() Do in Java

Paul Richard
Updated on 25-Feb-2020 10:04:48

198 Views

The elements() method is used to return an enumeration of the components of this vector. The returned Enumeration object will generate all items in this vector at the similar index location.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);       Enumeration e=vec.elements();       System.out.println("Numbers in the enumeration are :- ");       while (e.hasMoreElements()) {          System.out.println("Number = " + e.nextElement());       }    } }OutputNumbers in the enumeration are :- Number = 4 Number = 3 Number = 2 Number = 1

What Does the Method push(Object item) Do in Java

Arushi
Updated on 25-Feb-2020 10:04:00

594 Views

The push(Object item) method is used to Pushes an item onto the top of 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("Elements in the stack: "+st);    } }OutputElements in the stack: [Java, Source, code]

What Does the Method Peek Do in Java

Rishi Raj
Updated on 25-Feb-2020 10:01:14

340 Views

The peek() method is used to look at the object at the top of this stack without removing it from the 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("Top object is: "+st.peek());    } }OutputTop object is: code

What Does the Method Empty Do in Java

Vikyath Ram
Updated on 25-Feb-2020 10:00:29

180 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

What Does the Method search(Object o) Do in Java

Paul Richard
Updated on 25-Feb-2020 09:59:50

465 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: 

Copy or Clone a Java ArrayList

Abhinanda Shri
Updated on 25-Feb-2020 09:50:14

2K+ Views

The clone() method of the java.util.ArrayList class returns a shallow copy of this ArrayList instance (i.e the elements themselves are not copied). Using this method, you can copy the contents of one array list to other.Exampleimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String args[]) {       ArrayList arrlist1 = new ArrayList();       arrlist1.add(new StringBuilder("Learning-"));       ArrayList arrlist2 = (ArrayList) arrlist1.clone();       StringBuilder strbuilder = arrlist1.get(0);       strbuilder.append("list1, list2-both pointing to the same StringBuilder");       System.out.println("The 1st list prints: ");       for (int i = ... Read More

Use do-while Loop in Java

Rahul Sharma
Updated on 25-Feb-2020 09:49:15

238 Views

A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.SyntaxFollowing is the syntax of a do...while loop −do {    // Statements }while(Boolean_expression);Notice that the Boolean expression appears at the end of the loop, so the statements in the loop execute once before the Boolean is tested.If the Boolean expression is true, the control jumps back up to do statement, and the statements in the loop execute again. This process repeats until the Boolean expression is false.Examplepublic class Test {    public static void main(String args[]) {   ... Read More

Advertisements