Object Oriented Programming Articles

Page 141 of 588

How to reverse the elements of an array using stack in java?

Ramu Prasad
Ramu Prasad
Updated on 11-Mar-2026 4K+ Views

Stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example – a deck of cards or a pile of plates, etc.A stack is first in first out, it has two main operations push and pop. Push inserts data in to it and pop retrieves data from it.To reverse an array using stack initially push all elements in to the stack using the push() method then, retrieve them back using the pop() method into another array.Exampleimport java.util.Arrays; import java.util.Stack; public class ReversinArrayUsingStack {   ...

Read More

If a number is read as a String, how to Count the no of times the highest number repeated in the string?\\n a. Ex: 2 4 3 4 2 4 0 -> (3)

Govinda Sai
Govinda Sai
Updated on 11-Mar-2026 164 Views

To do so, Trim the given string and split it using the split() method (Removing the empty spaces in-between).Create an integer array and in loop convert each element of the string array to an integer using Integer.parseInt() method and assign it to the respective element of the integer array.Sort the obtained integer array, since this method sorts the elements of the array in ascending order the last element will be the maximum of the array. Create an integer variable count with initial value 0. Compare each element of the array with the maximum value, each time a match occurs increment the count. The final value ...

Read More

How to store the contents of arrays in a file using Java?

Ramu Prasad
Ramu Prasad
Updated on 11-Mar-2026 3K+ Views

You can use write data into a file using the Writer classes. In the example given below, we are writing the contents of the array using the BufferedWriter.Exampleimport java.io.BufferedWriter; import java.io.FileWriter; public class WritingStringArrayToFile {    public static void main(String args[]) throws Exception {       String[] myArray = {"JavaFX", "HBase", "OpenCV", "Java", "Hadoop", "Neo4j"};       BufferedWriter writer = new BufferedWriter(new FileWriter("myFile.txt", false));       for(int i = 0; i < myArray.length; i++) {          writer.write(myArray[i].toString());          writer.newLine();       }       writer.flush();       ...

Read More

How to convert Java Array to Iterable?

V Jyothi
V Jyothi
Updated on 11-Mar-2026 9K+ Views

To make an array iterable either you need to convert it to a stream or as a list using the asList() or stream() methods respectively. Then you can get an iterator for these objects using the iterator() method.Exampleimport java.util.Arrays; import java.util.Iterator; public class ArrayToIterable {    public static void main(String args[]){       Integer[] myArray = {897, 56, 78, 90, 12, 123, 75};       Iterator iterator = Arrays.stream(myArray).iterator();       while(iterator.hasNext()) {          System.out.println(iterator.next());       }    } }Output897 56 78 90 12 123 75As mentioned above, you can also convert an ...

Read More

What is the difference between method overloading and method hiding in Java?

varun
varun
Updated on 11-Mar-2026 1K+ Views

method hiding − When super class and the sub class contains same methods including parameters, and if they are static and, when called, the super class method is hidden by the method of the sub class this is known as method hiding. Example class Demo{ public static void demoMethod() { System.out.println("method of super class"); } } public class Sample extends Demo{ public static void demoMethod() { System.out.println("method of sub class"); } public ...

Read More

What is the difference between compile time polymorphism and runtime polymorphism in java?

usharani
usharani
Updated on 11-Mar-2026 1K+ Views

If we perform (achieve) method overriding and method overloading using instance methods, it is run time (dynamic) polymorphism. In dynamic polymorphism the binding between the method call an the method body happens at the time of execution and, this binding is known as dynamic binding or late binding. Example class SuperClass{ public static void sample(){ System.out.println("Method of the super class"); } } public class RuntimePolymorphism extends SuperClass { public static void sample(){ System.out.println("Method of the sub class"); ...

Read More

What is Is-a relationship in Java?

varma
varma
Updated on 11-Mar-2026 1K+ Views

IS-A is a way of saying: This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance.Examplepublic class Animal { } public class Mammal extends Animal { } public class Reptile extends Animal { } public class Dog extends Mammal { }Now, based on the above example, in Object-Oriented terms, the following are true −Animal is the superclass of Mammal class.Animal is the superclass of Reptile class.Mammal and Reptile are subclasses of Animal class.Dog is the subclass of both Mammal and Animal classes.Exampleclass Animal { } class Mammal extends Animal { ...

Read More

How to return an array from a method in Java?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 11-Mar-2026 6K+ Views

We can return an array in Java from a method in Java. Here we have a method createArray() from which we create an array dynamically by taking values from the user and return the created array.Exampleimport java.util.Arrays; import java.util.Scanner; public class ReturningAnArray {    public int[] createArray() {       Scanner sc = new Scanner(System.in);       System.out.println("Enter the size of the array that is to be created:: ");       int size = sc.nextInt();       int[] myArray = new int[size];       System.out.println("Enter the elements of the array ::");       for(int i=0; i

Read More

How to pull distinct values from an array in java?

Sharon Christine
Sharon Christine
Updated on 11-Mar-2026 1K+ Views

To pull distinct values in an array you need to compare each element of the array to all the remaining elements, in case of a match you got your duplicate element. One solution to do so you need to use two loops (nested) where the inner loop starts with i+1 (where i is the variable of outer loop) to avoid repetitions in comparison.Exampleimport java.util.Arrays; import java.util.Scanner; public class DetectDuplcate {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter the size of the array that is to be created::"); ...

Read More

How to switch data from an array to array list in java?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 332 Views

The Arrays class of the java.util package provides you a method named asList(). This method accepts an array (of objects) and converts them into a list and returns it.Exampleimport java.util.Arrays; import java.util.List; public class ArrayToList {    public static void main(String args[]) {       Integer[] myArray = {23, 93, 56, 92, 39};       List list = Arrays.asList(myArray);       System.out.println(list);    } }Output[23, 93, 56, 92, 39]

Read More
Showing 1401–1410 of 5,878 articles
« Prev 1 139 140 141 142 143 588 Next »
Advertisements