Found 9128 Articles for Object Oriented Programming

How to sort a random number array in java?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:20

2K+ Views

To sort an array in Java, you need to compare each element of the array to all the remaining elements and verify whether it is greater if so swap them.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 ArrayInOrder {    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 convert a Double array to a String array in java?

Sharon Christine
Updated on 19-Feb-2020 10:44:05

3K+ Views

You can convert a double array to a string using the toString() method. To convert a double array to a string array, convert each element of it to string and populate the String array with them.ExampleLive Demoimport java.util.Arrays; public class DoubleArrayToString {    public static void main(String args[]) {       Double[] arr = {12.4, 35.2, 25.6, 98.7, 56.4};       int size = arr.length;       String[] str = new String[size];           for(int i=0; i

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

44K+ 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

4K+ 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

632 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

161 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?

Revathi Satya Kondra
Updated on 26-Dec-2024 20:44:18

2K+ 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. To convert an InputStream to a byte array, We can use the ByteArrayOutputStream class to read data from the InputStream and write it into a byte array. Comparison Table Methods to Convert InputStream to Byte Array Let's compare the methods to convert InputStream to Byte Array in the below table − ... Read More

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

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

277 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

Advertisements