Found 4338 Articles for Java 8

How to convert integer set to int array using Java?

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

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

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

Write a Java program to find the first array element whose value is repeated an integer array?

Maruthi Krishna
Updated on 02-Aug-2019 12:56:21

260 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 non-zero element 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, ... Read More

Can you assign an Array of 100 elements to an array of 10 elements in Java?

Maruthi Krishna
Updated on 02-Jul-2020 12:14:32

3K+ Views

In general, arrays are the containers that store multiple variables of the same datatype. These are of fixed size and the size is determined at the time of creation. Each element in an array is positioned by a number starting from 0.You can access the elements of an array using name and position as −System.out.println(myArray[3]); //Which is 1457Creating an array in JavaIn Java, arrays are treated as referenced types you can create an array using the new keyword similar to objects and populate it using the indices as −int myArray[] = new int[7];While creating array in this way, you must specify ... Read More

Can you pass the negative number as an Array size in Java?

Maruthi Krishna
Updated on 02-Jul-2020 12:18:45

2K+ Views

In general, arrays are the containers that store multiple variables of the same datatype. These are of fixed size and the size is determined at the time of creation. Each element in an array is positioned by a number starting from 0.You can access the elements of an array using name and position as −System.out.println(myArray[3]); //Which is 1457Creating an array in JavaIn Java, arrays are treated as referenced types you can create an array using the new keyword similar to objects and populate it using the indices as −int myArray[] = new int[7];While creating array in this way, you must ... Read More

What happens if try to access an element with an index greater than the size of the array in Java?

Maruthi Krishna
Updated on 02-Jul-2020 12:20:04

3K+ Views

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

Write program to reverse a String without using reverse() method in Java?

Maruthi Krishna
Updated on 02-Aug-2019 12:27:11

4K+ Views

You can reverse a String in several ways, without using the reverse() function.Using recursion − Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function. You can reverse a string using recursive function as shown in the following program.Exampleimport java.util.Scanner; public class StringReverse {    public static String reverseString(String str){       if(str.isEmpty()){          return str;       }else{          return reverseString(str.substring(1))+str.charAt(0);       } ... Read More

What are the different ways to iterate over an array in Java?

Maruthi Krishna
Updated on 02-Jul-2020 12:21:33

17K+ Views

In general, arrays are the containers that store multiple variables of the same datatype. These are of fixed size and the size is determined at the time of creation. Each element in an array is positioned by a number starting from 0.You can access the elements of an array using name and position as −System.out.println(myArray[3]); //Which is 1457Creating an array in JavaIn Java, arrays are treated as referenced types you can create an array using the new keyword similar to objects and populate it using the indices as −int myArray[] = new int[7]; myArray[0] = 1254; myArray[1] = 1458; myArray[2] = ... Read More

Advertisements