Found 9150 Articles for Object Oriented Programming

How do you find continuous sub array whose sum is equal to a given number in Java?

Maruthi Krishna
Updated on 02-Aug-2019 14:12:53

2K+ Views

To find continuous sub array whose sum is equal to a given number −Iterate through the array.At each element add the next n elements one by one, when the sum equals to the required value print the sub array.Exampleimport java.util.Arrays; import java.util.Scanner; public class sub_arrays {    public static void main(String args[]){       //Reading the array from the user       Scanner sc = new Scanner(System.in);       System.out.println("Enter the size of the array that is to be created: ");       int size = sc.nextInt();       int[] myArray = new int[size];   ... Read More

How do you separate zeros from non-zeros in an integer array using Java?

Maruthi Krishna
Updated on 02-Aug-2019 14:09:27

2K+ Views

To separate zeros from non-zeros in an integer array, and push them to the end, you need to rearrange it array by assigning all the nonzero elements to its positions, sequentially, starting from zero. Then, from last position of the array to its end populate it with zeros.ExampleFollowing Java program pushes all the zeros in an array to its end.import java.util.Arrays; import java.util.Scanner; public class ZerosFromNonZeros {    public static void main(String args[]){       //Reading the array from the user       Scanner sc = new Scanner(System.in);       System.out.println("Enter the size of the array that ... Read More

How to find all pairs of elements in Java array whose sum is equal to a given number?

Rama Giri
Updated on 02-Aug-2019 14:05:01

6K+ Views

To find all pairs of elements in Java array whose sum is equal to a given number −Add each element in the array to all the remaining elements (except itself).Verify if the sum is equal to the required number.If true, print their indices.Exampleimport java.util.Arrays; import java.util.Scanner; public class sample {    public static void main(String args[]){       //Reading the array from the user       Scanner sc = new Scanner(System.in);       System.out.println("Enter the size of the array that is to be created: ");       int size = sc.nextInt();       int[] myArray ... Read More

Are the values of static variables stored when an object is serialized in Java?

Maruthi Krishna
Updated on 02-Aug-2019 13:58:39

1K+ Views

In Java, serialization is a concept using which we can write the state of an object into a byte stream so that we can transfer it over the network (using technologies like JPA and RMI).To serialize an object −Make sure the class implements the Serializable interface.Create a FileOutputStream object representing the file (abstract path) of the file to which the object is to be stored.Create a ObjectOutputStream object by passing the above created FileOutputStream object.Write the object to the file using the writeObject() method.To de-serialize an objectCreate a FileInputStream object representing the file that contains the serialized object.Read the object ... Read More

Using a JavaScript object inside a static() method?

vineeth.mariserla
Updated on 02-Jul-2020 12:46:34

100 Views

Actually, the result will be in vain when we try to use an object inside a static method. But when the object is sent as a parameter, we can access the object. let's discuss it in a nutshell.Example-1In the following example, we tried to use the object "myComp" directly rather than sending it as a parameter, therefore, we get no result. If we open the browser console we will get an error that is "myComp.comp() is not a function". To get the actual result we have to send the object as a parameter as shown in Example-2    class ... Read More

Are the constructors in an object invoked when de-serialized in Java?

Maruthi Krishna
Updated on 02-Jul-2020 12:31:58

1K+ Views

In Java, serialization is a concept using which we can write the state of an object into a byte stream so that we can transfer it over the network (using technologies like JPA and RMI).To serialize an object −Make sure the class implements the Serializable interface.Create a FileOutputStream object representing the file (abstract path) of the file to which the object is to be stored.Create a ObjectOutputStream object by passing the above created FileOutputStream object.Write the object to the file using the writeObject() method.To de-serialize an objectCreate a FileInputStream object representing the file that contains the serialized object.Read the object ... Read More

How to convert integer set to int array using Java?

Maruthi Krishna
Updated on 02-Jul-2020 12:34:00

11K+ Views

A collection object in Java is the one which stores references of other objects in it. The java.util package provides the classes and interfaces for collections. There are four main collection interfaces namely Set Lists, Queues, Maps.Set − The set object is a collection which stores group of elements, it grows dynamically and it does not allow duplicate elements.HashSet and LinkedHashSet are the classes that implements Set interface. You can create a Set object by implementing either of these classes.Exampleimport java.util.HashSet; public class SetExample {    public static void main(String args[]) {       //Instantiating the HashSet       ... Read More

What is an anonymous array and explain with an example in Java?

Maruthi Krishna
Updated on 02-Jul-2020 12:35:23

2K+ Views

An array is a data structure/container/objectthat 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 is stored at the index 0 and, the second element is at the index 1 and so on.Each element in an array is accessed using an expression which contains the name of the array followed by the index of the required element in square brackets.For example, if an array of 6 elements ... Read More

How to find the missing number in a given Array from number 1 to n in Java?

Maruthi Krishna
Updated on 02-Aug-2019 13:38:27

17K+ Views

If a single number is missing in an integer array that contains a sequence of numbers values, you can find it basing of the sum of numbers or, basing on the xor of the numbers.Based on the sum of the numbers −The sum of n sequential numbers will be [n*(n+1)]/2. Using this get the sum of the numbers the n numbers.Add all the elements in the array.Subtract the sum of the numbers in the array from the sum of the n numbers.Exampleimport java.util.Scanner; public class MissingNumber {    public static void main(String[] args) {       Scanner sc = ... Read More

Write a program to find the first non-repeating number in an integer array using Java?

Maruthi Krishna
Updated on 02-Aug-2019 13:34:47

2K+ Views

To find the first non-repeating number in an array −Construct count array to store count of each element in the given array with same length and with initial value 0 for all elements.Compare each element in the array with all other elements, except itself.If match occurs increment its value in the count array.Get the index of the first 0 in the count array and print the element in the input array at this index.Exampleimport java.util.Arrays; public class NonRpeatingArray {    public static void main(String args[]) {       int array[] = {114, 225, 669, 996, 336, 6547, 669, 225, ... Read More

Advertisements