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

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

443 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

235 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

What is a Switch Case Statement in Java and How to Use It

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

560 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 Does the Method sort(obj a) Do in Java

Sharon Christine
Updated on 25-Feb-2020 09:28:44

124 Views

The sort(Object[]) method of the java.util.Arrays class sorts the specified array of Objects into ascending order according to the natural ordering of its elements.Exampleimport java.util.Arrays; public class ArrayDemo {    public static void main(String[] args) {       Object ob[] = {27, 11, 44};       for (Object number : ob) {          System.out.println("Number = " + number);       }       Arrays.sort(ob);       System.out.println("The sorted Object array is:");       for (Object number : ob) {          System.out.println("Number = " + number);       }    } }OutputNumber = 27 Number = 11 Number = 44 The sorted Object array is: Number = 11 Number = 27 Number = 44

What Does the fill Method Do in Java?

Swarali Sree
Updated on 25-Feb-2020 09:27:53

204 Views

The fill(int[] a, int fromIndex, int toIndex, int val) method of the java.util.Arrays class assigns the specified int value to each element of the specified range of the specified array of integers. The range to be filled extends from index fromIndex, inclusive, to index toIndex, exclusive.(If fromIndex==toIndex, the range to be filled is empty.)Exampleimport java.util.Arrays; public class ArrayDemo {    public static void main(String[] args) {       int arr[] = new int[] {1, 6, 3, 2, 9};       System.out.println("Actual values: ");       for (int value : arr) {          System.out.println("Value = ... Read More

Convert Class to Another Class Type in C++

Ayush Gupta
Updated on 25-Feb-2020 09:15:13

5K+ Views

In this tutorial, we will be discussing a program to understand how to convert a class to another class type in C/C++.Class conversion can be done with the help of operator overloading. This allows data of one class type to be assigned to the object of another class type.Example Live Demo#include using namespace std; //type to which it will be converted class Class_type_one {    string a = "TutorialsPoint";    public:       string get_string(){          return (a);    }    void display(){       cout

Find Sum of Elements of a Vector Using STL in C++

Ayush Gupta
Updated on 25-Feb-2020 09:12:10

823 Views

In this tutorial, we will be discussing a program to understand how to find the sum of elements of a vector using STL in C++.To find the sum of elements of a given vector, we would be using the accumulate() method from the STL library.Example Live Demo#include using namespace std; int main(){    //defining the vector    vector a = { 1, 45, 54, 71, 76, 12 };    cout

Fill Array with a Value in Java

karthikeya Boyini
Updated on 25-Feb-2020 08:21:52

160 Views

The fill(object[] a, int fromIndex, int toIndex, object val) method of the class java.util.Arrays assign the specified Object reference to each element of the specified range of the specified array of objects. The range to be filled extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be filled is empty)Exampleimport java.util.Arrays; public class ArrayDemo {    public static void main(String[] args) {       Object arr[] = new Object[] {1.2, 5.6, 3.4, 2.9, 9.7};       System.out.println("Actual values: ");       for (Object value : arr) {          System.out.println("Value ... Read More

What Does the Method toArray Do in Java

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

182 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

Advertisements