Found 9321 Articles for Object Oriented Programming

How to convert an input stream to byte array in java?

Syed Javed
Updated on 06-Mar-2020 05:07:10

1K+ Views

The InputStream class in Java provides read() method. This method accepts a byte array and it reads the contents of the input stream to the given byte array.ExampleLive Demoimport java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; public class StreamToByteArray {     public static void main(String args[]) throws IOException{            InputStream is = new BufferedInputStream(System.in);        byte [] byteArray = new byte[1024];        System.out.println("Enter some data");        is.read(byteArray);              String s = new String(byteArray);        System.out.println("Contents of the byte stream are :: "+ s);   ... Read More

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

Samual Sam
Updated on 16-Jun-2020 11:14:27

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

How to remove duplicate elements of an array in java?

Monica Mona
Updated on 30-Jul-2019 22:30:20

3K+ Views

To detect the duplicate 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 the outer loop) to avoid repetitions.Apache Commons provides a library named org.apache.commons.lang3 and, following is the maven dependency to add a library to your project. org.apache.commons commons-lang3 ... Read More

How to detect duplicate values in primitive Java array?

karthikeya Boyini
Updated on 19-Dec-2019 10:20:51

1K+ Views

To detect the duplicate 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 ... Read More

How do I invoke a Java method when given the method name as a string?

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

3K+ Views

The java.lang.reflect.Method class provides information about, and access to, a single method on a class or interface. The reflected method may be a class method or an instance method (including an abstract method). A Method permits widening conversions to occur when matching the actual parameters to invoke with the underlying method's formal parameters, but it throws an IllegalArgumentException if a narrowing conversion would occur. You can invoke the method using the class named method of the package java.lang.reflect. The constructor of this class accepts the method name in the form of a string. And you can invoke this method using ... Read More

How to pull distinct values from an array in java?

Sharon Christine
Updated on 30-Jul-2019 22:30:20

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

How multiple inheritance is implemented using interfaces in Java?

mkotla
Updated on 25-Feb-2020 12:33:24

1K+ Views

Java does not support multiple inheritance. This means that a class cannot extend more than one class. Therefore, following is illegal −Examplepublic class extends Animal, Mammal{}However, a class can implement one or more interfaces, which has helped Java get rid of the impossibility of multiple inheritance.The extends keyword is used once, and the parent interfaces are declared in a comma-separated list.For example, if the Hockey interface extended both Sports and Event, it would be declared as −Examplepublic interface Hockey extends Sports, Event

How to move specific item in array list to the first item in Java?

Sharon Christine
Updated on 19-Dec-2019 10:14:46

7K+ Views

To move an item from an ArrayList and add it to the first position you need to -Get the position (index) of the item using the indexOf() method of the ArrayList class.Remove it using the remove() method of the ArrayList class.Finally, add it to the index 0 using the add() method of the ArrayList class.ExampleLive Demoimport java.util.ArrayList; public class ArrayListSample {    public static void main(String args[]) {       ArrayList al = new ArrayList();       al.add("JavaFX");       al.add("HBase");       al.add("WebGL");       al.add("OpenCV");       System.out.println(al);       String item = "WebGL";   ... Read More

How to pass Arrays to Methods in Java?

Samual Sam
Updated on 02-Sep-2023 13:52:53

56K+ Views

You can pass arrays to a method just like normal variables. When we pass an array to a method as an argument, actually the address of the array in the memory is passed (reference). Therefore, any changes to this array in the method will affect the array.Suppose we have two methods min() and max() which accepts an array and these methods calculates the minimum and maximum values of the given array respectively:Example Live Demoimport java.util.Scanner; public class ArraysToMethod {    public int max(int [] array) {       int max = 0;       for(int i=0; imax) { ... Read More

How to create array of strings in Java?

Sai Subramanyam
Updated on 19-Dec-2019 10:13:26

2K+ Views

In Java, you can create an array just like an object using the new keyword. The syntax of creating an array in Java using new keyword −type[] reference = new type[10];Where, type is the data type of the elements of the array.reference is the reference that holds the array.And, if you want to populate the array by assigning values to all the elements one by one using the index −reference [0] = value1; reference [1] = value2;You can declare an array of Strings using the new keyword as &mius;String[] str = new String[5]; And then, you can populate the string ... Read More

Advertisements