Java Articles

Page 348 of 450

How to define an array size in java without hardcoding?

Sravani S
Sravani S
Updated on 19-Feb-2020 690 Views

To avoid hard coding you can read the size of the array from the user using command line arguments of the reader classes like Scanner. Then using this value create an array:Exampleimport java.util.Arrays; import java.util.Scanner; public class PopulatingAnArray { public static void main(String args[]) { System.out.println("Enter the required size of the array :: "); Scanner s = new Scanner(System.in); int size = s.nextInt(); int myArray[] = new int [size]; System.out.println("Enter the elements of the array one by one "); for(int i=0; i

Read More

How to populate an array one value at a time by taking input from user in Java?

Priya Pallavi
Priya Pallavi
Updated on 19-Feb-2020 7K+ Views

To read data from user create a scanner class. Read the size of the array to be created from the user using nextInt() method. Create an array with the specified size. In the loop read the values from the user and store in the array created above.Exampleimport java.util.Arrays; import java.util.Scanner; public class PopulatingAnArray { public static void main(String args[]) { System.out.println("Enter the required size of the array :: "); Scanner s = new Scanner(System.in); int size = s.nextInt(); ...

Read More

How to divide an array into half in java?

Abhinaya
Abhinaya
Updated on 19-Feb-2020 16K+ Views

Using the copyOfRange() method you can copy an array within a range. This method accepts three parameters, an array that you want to copy, start and end indexes of the range.You split an array using this method by copying the array ranging from 0 to length/2 to one array and length/2 to length to other.Exampleimport java.util.Arrays; import java.util.Scanner; public class SplittingAnArray {    public static void main(String args[]) {       Scanner s =new Scanner(System.in);       System.out.println("Enter the required size of the array ::");       int size = s.nextInt();       int [] myArray ...

Read More

How to read a 2d array from a file in java?

Govinda Sai
Govinda Sai
Updated on 19-Feb-2020 11K+ Views

A 2d array is an array of one dimensional arrays to read the contents of a file to a 2d array –Instantiate Scanner or other relevant class to read data from a file. Create an array to store the contents.To copy contents, you need two loops one nested within the other. the outer loop is to traverse through the array of one dimensional arrays and, the inner loop is to traverse through the elements of a particular one dimensional array.Create an outer loop starting from 0 up to the length of the array. Within this loop read each line trim and ...

Read More

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

Ramu Prasad
Ramu Prasad
Updated on 19-Feb-2020 370 Views

Apache Commons provides a library named org.apache.commons.lang3 and, following is the maven dependency to add the library to your project.           org.apache.commons       commons-lang3       3.0     This package provides a class named ArrayUtils. Using the toPrimitive() method of this class you can convert An object array to an array of primitive types:Exampleimport java.util.Arrays; import org.apache.commons.lang3.ArrayUtils; public class ArraysToPrimitives {    public static void main(String args[]) {       Integer[] myArray = {234, 76, 890, 27, 10, 63};       int[] primitiveArray = ArrayUtils.toPrimitive(myArray);       System.out.println(Arrays.toString(primitiveArray));    } }Output[234, 76, 890, 27, 10, 63]

Read More

How to convert an object to byte array in java?

Srinivas Gorla
Srinivas Gorla
Updated on 19-Feb-2020 16K+ Views

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 contents to the stream using the flush() method.Finally, convert the contents of the ByteArrayOutputStream to a byte array using the toByteArray() method.Exampleimport java.io.ByteArrayOutputStream; import java.io.ObjectOutputStream; import java.io.Serializable; class Sample implements Serializable { public void display() { System.out.println("This is a sample ...

Read More

What is length in Java Arrays?

Monica Mona
Monica Mona
Updated on 19-Feb-2020 765 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);

Read More

How to add/insert additional property to JSON string using Gson in Java?

raja
raja
Updated on 19-Feb-2020 9K+ Views

The com.google.gson.JSonElement class represents an element of Json. We can use the toJsonTree() method of Gson class to serialize an object's representation as a tree of JsonElements. We can add/ insert an additional property to JSON string by using the getAsJsonObject() method of JSonElement. This method returns to get the element as JsonObject.Syntaxpublic JsonObject getAsJsonObject()Exampleimport com.google.gson.*; public class AddPropertyGsonTest { public static void main(String[] args) { Gson gson = new GsonBuilder().setPrettyPrinting().create(); // pretty print JSON Student student = new Student("Adithya"); ...

Read More

How to convert an array to JSON Array using JSON-lib API in Java?

raja
raja
Updated on 19-Feb-2020 590 Views

A Java array is an object which stores multiple variables of the same type, it can hold primitive types and object references whereas JSONArray is an ordered sequence of values. Its external text form is a string wrapped in square brackets with commas separating the values, an internal form is an object having get() and opt() methods for accessing the values by index and element() method for adding or replacing values. In the first step, we can create an Object[] array and pass this parameter as an argument to the toJSON() of JSONSerializer class and typecasting it to get the ...

Read More

How to create an object from class in Java?

Prabhas
Prabhas
Updated on 19-Feb-2020 2K+ Views

An object is created from a class using the new keyword. There are three steps when creating an object from a class −Declaration − A variable declaration with a variable name with an object type.Instantiation − The 'new' keyword is used to create the object.Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object.Examplepublic class Sample {    public static void main(String args[]){       Sample s = new Sample();    } }

Read More
Showing 3471–3480 of 4,498 articles
« Prev 1 346 347 348 349 350 450 Next »
Advertisements