Found 7442 Articles for Java

What does the method pop() do in java?

George John
Updated on 30-Jul-2019 22:30:21

559 Views

The pop() method is used to remove the object at the top of this stack and returns that object as the value of this function. Example import 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("Removed object is: "+st.pop()); System.out.println("Elements after remove: "+st); } } Output Removed object is: code Elements after remove: [Java, Source]

What does the method toArray() do in java?

Priya Pallavi
Updated on 25-Feb-2020 08:20:57

179 Views

The toArray() method of the java.util.ArrayList class returns an array containing all of the elements in this list in proper sequence (from first to the last element).This acts as a bridge between array-based and collection-based APIs.Exampleimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String[] args) {       ArrayList arrlist = new ArrayList(5);       arrlist.add(20);       arrlist.add(40);       arrlist.add(10);       arrlist.add(15);       arrlist.add(25);       for (Integer number : arrlist) {          System.out.println("Number = " + number);       }       Object[] ... Read More

What does the method push(Object item) do in java?

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

557 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 elements() do in java?

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

171 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 contains(obj o) do in java?

Nikitha N
Updated on 25-Feb-2020 08:20:14

130 Views

The contains(Object) method of the java.util.ArrayList class returns true if this list contains the specified element.Exampleimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String[] args) {       ArrayList arrlist = new ArrayList

What does the method isEmpty() do in java?

Srinivas Gorla
Updated on 20-Feb-2020 12:17:27

190 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

What does the method int capacity() do in java?

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

335 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 size() do in java?

Abhinanda Shri
Updated on 20-Feb-2020 12:16:41

832 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

What does the method removeAllElements() do in java?

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

143 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 removeElementAt(int index) do in java?

George John
Updated on 30-Jul-2019 22:30:21

113 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

Advertisements