How to Declare an Array in Java?
dataType[] arr; Where: int[] intArray = new int[5]; String[] arr = new String[3]; arr[0] = “value 1”; arr[1] = “value 2”; arr[2] = “value 3”;
Read More How to Declare an Array in Java?dataType[] arr; Where: int[] intArray = new int[5]; String[] arr = new String[3]; arr[0] = “value 1”; arr[1] = “value 2”; arr[2] = “value 3”;
Read More How to Declare an Array in Java?class CheckIfArrayContainsValue { public static void main(String[] args) { String[] programmingLanguages = {“Python”, “Kotlin”, “Ruby”, “JavaScript”, “C#”, “Java”, “Flutter”}; for (String lang : programmingLanguages) { if (lang.equals(“Java”)) { System.out.println(“It does contain!”); break; // value found, exit the loop } } } } Output: It does contain! Here, we are iterating over the array, and…
Read More Check if Array Contains a Value in Javaclass FindDuplicateElements { public static void main(String[] args) { int[] array = new int[]{2, 4, 7, 2, 11, 5, 7, 14, 22, 11, 49, 58, 14, 101, 1, 3, 205, 49, 101, 12}; for (int i = 0; i < array.length; i++) { // outer loop for (int j = i + 1; j <…
Read More Find duplicate elements in an Array in JavaThere are multiple ways to convert a String to an array in Java. We can convert it to a char array or String array. Convert a String to a char Array The String class, a fundamental data type in Java, has the useful toCharArray() method that enables the conversion of a String to a char array.…
Read More Convert a String to an Array in JavaIn this post, you will learn how to convert ArrayList to an Array in Java using two methods: With a for loop Using toArray() method Convert ArrayList to Array in Java with a for loop In this example, we create an empty array, then iterate over the list and add each element to the array. class Test {…
Read More Convert ArrayList to Array in JavaThere are many ways to convert an array to a list in Java. In this tutorial, we will cover the following: Using the Arrays.asList() method Using Java 8 Streams API Using the List.of() method Convert array to list in Java using the Arrays.asList() method We can use the asList() method from the Arrays class, which belongs to…
Read More Convert Array to List in JavaThere are many ways to copy an array in Java. Here, we will explore the following: Using the clone() method Using System.arraycopy() Using the Arrays.copyOf() Copy an array in Java using the clone() method This is the most used way of cloning objects. The clone() method belongs to Object class, and we can use it to…
Read More Copy an Array in Javapublic class Test { public static void main(String[] args) { Integer[] array = new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // reverse an array Integer[] reversedArray = reverse(array); // print the elements Arrays.stream(reversedArray).forEach(item -> System.out.print(item + ” “)); } public static Integer[] reverse(Integer[] array) { List arrayList = Arrays.asList(array); Collections.reverse(arrayList); return…
Read More Reverse an Array in Javapublic class Test { public static void main(String[] args) { int[] array = new int[]{7, 1, 9, 2, 5}; Arrays.sort(array); Arrays.stream(array).forEach(System.out::print); } } Output: 1 2 5 7 9 Note: values of type int will be automatically converted to the corresponding Wrapper class. And since the Integer Wrapper class implements Comparable, we can sort…
Read More Sort an Array in Javaimport java.util.Arrays; public class Test { public static void main(String[] args) { int[] array = new int[]{17, 28, 9, 4, 22, 7, 1, 3, 19, 42, 58, 47}; // first sort the array Arrays.sort(array); //print the minimum value (first number in an array) System.out.println(“Minimum value: ” + array[0]); //print the maximum value (last number in…
Read More Find Max and Min Values in a Java Arrayimport java.util.Arrays; class Test { public static void main(String[] args) { String[] names = {“John”, “Steve”, “Melissa”, “Maria”}; System.out.println(Arrays.toString(names)); } } Output: [John, Steve, Melissa, Maria] copyOf(type [] a, int n) Returns a new copy of the array, which consists of the first n of its elements. import java.util.Arrays; class Test { public static…
Read More Arrays class in JavaDeclaring a multidimensional array in Java class Test { int[][] array1; int[][] array2; int array3[][]; } class Test { int[][] array1 = new int[3][3]; // 3 rows and 3 columns int[][] array2 = {{1, 2}, {3, 4}}; // 2 rows and 2 columns } class Test { public static void main(String[] args) { int[][] array…
Read More Multidimensional Arrays in Javaclass Test { int[] a; int b[]; int []c; } class Test { int[] a = new int[100]; int b[] = new int[]{1, 2, 3}; int[] c = {1, 2, 3}; } class Test { public static void main(String[] args) { // initialize the array int[] a = new int[3]; // add elements to array…
Read More Java Arrays