How to find the Odd and Even numbers in an Array in java?

Govinda Sai
Published on 03-Jan-2018 11:23:04
In the loop check, the result of i%2 operation on each element if 0 the element is even else the element is odd.ExampleLive Demopublic class OddNumbersInAnArray {    public static void main(String args[]) {       int[] myArray = {23, 93, 56, 92, 39};       System.out.println("Even numbers ... Read More

How to print data of specific element from an array in java?

Monica Mona
Published on 02-Jan-2018 11:48:22
An array is a data structure/container/object that stores a fixed-size sequential collection of elements of the same type. The size/length of the array is determined at the time of creation. The position of the elements in the array is called as index or subscript. The first element of the array ... Read More

How to convert an object to byte array in java?

Srinivas Gorla
Published on 02-Jan-2018 13:40:03
To convert an object to byte arrayMake the required object serializable by implementing the Serializable interface.Create a ByteArrayOutputStream object.Create an ObjectOutputStream object by passing the ByteArrayOutputStream object created in the previous step.Write the contents of the object to the output stream using the writeObject() method of the ObjectOutputStream class.Flush the ... Read More

How to move an array element from one array position to another in Java?

karthikeya Boyini
Published on 02-Jan-2018 11:46:29
To move an element from one position to other (swap) you need to –Create a temp variable and assign the value of the original position to it.Now, assign the value in the new position to original position.Finally, assign the value in the temp to the new position.ExampleLive Demoimport java.util.Arrays; ... Read More

How to convert an array to string in java?

Nikitha N
Published on 02-Jan-2018 13:42:32
The Arrays class of the java.util package provides toString() methods for all primitive data types and object. These methods accept an array and return its string representation.Therefore, to convert an array to string pass the required array to this method.ExampleLive Demoimport java.util.Arrays; public class ArrayToString {    public static ... Read More

How to convert Image to Byte Array in java?

Priya Pallavi
Published on 02-Jan-2018 13:45:49
Java provides ImageIO class for reading and writing an image. To convert an image to a byte array –Read the image using the read() method of the ImageIO class.Create a ByteArrayOutputStream object.Write the image to the ByteArrayOutputStream object created above using the write() method of the ImageIO class.Finally convert the ... Read More

How to move an element of an array to a specific position (swap)?

Sharon Christine
Published on 02-Jan-2018 11:36:06
To move an element from one position to other (swap) you need to –Create a temp variable and assign the value of the original position to it.Now, assign the value in the new position to original position.Finally, assign the value in the temp to the new position.ExampleLive Demoimport java.util.Arrays; public ... Read More

How to convert Byte Array to Image in java?

V Jyothi
Published on 03-Jan-2018 05:05:50
Java provides ImageIO class for reading and writing an image. To convert a byte array to an image.Create a ByteArrayInputStream object by passing the byte array (that is to be converted) to its constructor.Read the image using the read() method of the ImageIO class (by passing the ByteArrayInputStream objects to ... Read More

How to convert a PDF to byte array in Java?

Sravani S
Published on 03-Jan-2018 05:09:44
You can read data from a PDF file using the read() method of the FileInputStream class this method requires a byte array as a parameter.Exampleimport java.io.File; import java.io.FileInputStream; import java.io.ByteArrayOutputStream; public class PdfToByteArray {    public static void main(String args[]) throws Exception {       File file = ... Read More

How to convert an array of objects to an array of their primitive types in java?

Ramu Prasad
Published on 03-Jan-2018 06:08:54
Apache Commons provides a library named org.apache.commons.lang3 and, following is the maven dependency to add the library to your project.<dependencies>    <dependency>       <groupId>org.apache.commons</groupId>       <artifactId>commons-lang3</artifactId>       <version>3.0</version>    </dependency> </dependencies>This package provides a class named ArrayUtils. Using the toPrimitive() method of this class ... Read More
Advertisements