Programming Articles - Page 2845 of 2896

How to return an array from a method in Java?

Lakshmi Srinivas
Updated on 30-Jul-2019 22:30:20

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.Example Live Demoimport 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

What is Is-a relationship in Java?

varma
Updated on 30-Jul-2019 22:30:20

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.Example Live Democlass Animal { } class Mammal extends Animal ... Read More

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

usharani
Updated on 30-Jul-2019 22:30:20

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 Live Demo 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 ... Read More

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

varun
Updated on 30-Jul-2019 22:30:20

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 Live Demo 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"); } ... Read More

Mobile

How to find Min/Max numbers in a java array?

Samual Sam
Updated on 07-Oct-2023 02:52:33

32K+ Views

You can find the minimum and maximum values of an array using for loops −ExampleLive Demopublic class MinAndMax {    public int max(int [] array) {       int max = 0;             for(int i=0; imax) {             max = array[i];          }       }       return max;    }    public int min(int [] array) {       int min = array[0];             for(int i=0; i

Is final method inherited in Java?

seetha
Updated on 30-Jul-2019 22:30:20

3K+ Views

No, we cannot override a final method in Java. The final modifier for finalizing the implementations of classes, methods, and variables. We can declare a method as final, once you declare a method final it cannot be overridden. So, you cannot modify a final method from a sub class. The main intention of making a method final would be that the content of the method should not be changed by any outsider. Example class Demo{ public final void demoMethod(){ System.out.println("Hello"); } } public class Sample extends Demo{ ... Read More

How to Convert a Java 8 Stream to an Array?

karthikeya Boyini
Updated on 19-Dec-2019 08:54:20

504 Views

To convert a stream to an Array in Java -Collect the stream to a list using the Collect interface and the Collectors class.Now convert the list to an array using the toArray() method.ExampleLive Demoimport java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class J8StreamToArray {    public static void main(String args[]) {       String[] myArray = { "JavaFX", "OpenCV", "WebGL", "HBase" };       Stream stream = Stream.of(myArray);       List list = stream.collect(Collectors.toList());       String[] str = list.toArray(new String[0]);       System.out.println(Arrays.toString(str));    } }Output[JavaFX, OpenCV, WebGL, HBase]

How to read data from scanner to an array in java?

Revathi Satya Kondra
Updated on 17-Dec-2024 23:10:17

13K+ Views

The Scanner class of the java.util package gives you methods like nextInt(), nextByte(), nextFloat(), etc., to read data from the keyboard. To read an element of an array, use these methods in a 'for' loop. Let us have a brief explanation of the methods of nextInt(), nextByte(), and nextFloat() from the Scanner class in Java. Reading Integer Data Using nextInt() Method The nextInt() method is used to read the next token from the input as an integer. It is commonly used for reading integer values from the keyboard. Example In the following example, we use the nextInt() method of the ... Read More

How to convert Java Array to Iterable?

V Jyothi
Updated on 16-Jun-2020 08:52:32

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.ExampleLive Demoimport 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 ... Read More

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

Ramu Prasad
Updated on 16-Jun-2020 08:56:30

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.ExampleLive Demoimport 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

Advertisements