Found 9326 Articles for Object Oriented Programming

How to convert/store a string of numbers in to an array in java?

Swarali Sree
Updated on 30-Jul-2019 22:30:21

2K+ Views

You can convert a String to an integer using the parseInt() method of the Integer class. To convert a string array to an integer array, convert each element of it to integer and populate the integer array with them.Example Live Demoimport java.util.Arrays; public class StringToIntegerArray {    public static void main(String args[]) {       String [] str = {"123", "345", "437", "894"};       int size = str.length;       int [] arr = new int [size];       for(int i=0; i

How to convert string to array of integers in java?

Samual Sam
Updated on 06-Sep-2023 12:59:19

39K+ Views

You can convert a String to integer using the parseInt() method of the Integer class. To convert a string array to an integer array, convert each element of it to integer and populate the integer array with them.Example Live Demoimport java.util.Arrays; public class StringToIntegerArray { public static void main(String args[]) { String [] str = {"123", "345", "437", "894"}; int size = str.length; int [] arr = new int [size]; for(int i=0; i

How to convert an Array to a Set in Java?

Sai Subramanyam
Updated on 30-Jul-2019 22:30:20

2K+ Views

One solution to add an array to set is to use the addAll() method of the Collections class. This method accepts a collection and an element and, it adds the given element to the specified Collection. Example Live Demo import java.util.Collections; import java.util.HashSet; import java.util.Set; public class ArrayToSet { public static void main(String args[]) { Integer[] myArray = {23, 93, 56, 92, 39}; Set set = new HashSet(); Collections.addAll(set, myArray); System.out.println(set); } } Output [23, 39, 56, 92, 93]

What is length in Java Arrays?

Monica Mona
Updated on 19-Feb-2020 10:41:06

432 Views

Length is a filed in java, it gives the total number of the elements in a Java array. The length of an array is defined after the array was created.Integer[] myArray = {23, 93, 56, 92, 39}; System.out.println(myArray.length);

What's the simplest way to print a Java array?

karthikeya Boyini
Updated on 16-Jun-2020 11:16:55

98 Views

Generally, to print the contents of an array you need to use for loops, in addition to that you can directly print an array using the toString() method of this class. This method accepts an array as a parameter and returns it in String format.ExampleLive Demoimport java.util.Arrays; public class PrintingArrays {    public static void main(String args[]) {       int[] myArray = {23, 93, 30, 56, 92, 39};       System.out.println(Arrays.toString(myArray));    } }Output[23, 93, 30, 56, 92, 39]

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

Advertisements