Found 10102 Articles for Object Oriented Programming

What does the method removeAllElements() do in java?

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

56 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

37 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

What does the method lastElement() do in java?

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

67 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 firstElement() do in java?

Paul Richard
Updated on 25-Feb-2020 10:09:47

58 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

What does the method elementAt(int index) do in java?

Vikyath Ram
Updated on 30-Jul-2019 22:30:20

120 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

What does the method addElement(E obj) do in java?

Rishi Raj
Updated on 26-Feb-2020 06:10:20

214 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

How does the Java “foreach” loop work?

Rahul Sharma
Updated on 30-Jul-2019 22:30:21

117 Views

JDK 1.5 introduced a new for loop known as foreach loop or enhanced for loop, which enables you to traverse the complete array sequentially without using an index variable. Examplepublic class ArrayUsingForEach {    public static void main(String[] args) {       double[] myList = {1.9, 2.9, 3.4, 3.5};       // Print all the array elements       for (double element: myList) {          System.out.println(element);       }    } }Output1.9 2.9 3.4 3.5

What is a switch case statement in Java and how to use it?

Johar Ali
Updated on 25-Feb-2020 09:34:41

229 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

What is a continue statement in Java and how to use it?

Debarpito Sarkar
Updated on 05-Sep-2022 09:08:05

163 Views

This article will help you understand what the continue statement in Java Programming language is. Continue Statement The statement continue, is just the opposite of Break statement. As soon as the continue statement is executed in a loop, the control skips rest of the statements for that value and resumes for the next iteration. Syntax while (condition){ Statement 1; _________________; _________________; if (another condition is true){ continue; } Statement 2; _________________; ... Read More

What is a break statement in Java and how to use it?

Debarpito Sarkar
Updated on 05-Sep-2022 08:03:16

86 Views

This article will help you understand what the break statement in Java Programming language is. Break Statement Sometimes, it is needed to stop a look suddenly when a condition is satisfied. This can be done with the help of break statement. A break statement is used for unusual termination of a block. As soon as you apply a break statement, the control exits from the block (A set of statements under curly braces). Syntax for (condition){ execution continues if (another condition is true){ break; } ... Read More

Advertisements