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 PrintArrayElementsExample { public static void main(String[] args) { int[] arr1 = {1, 7, 9, 5, 2, 8, 3}; String[] arr2 = {“Megan”, “Tom”, “Melissa”, “John”, “Steve”}; // print elements of the arr1 System.out.println(Arrays.toString(arr1)); // print elements of the arr2 System.out.println(Arrays.toString(arr2)); } } Output: [1, 7, 9, 5, 2, 8, 3] [Megan, Tom, Melissa, John,…
Read More Print Array Elements in Javapublic class SplitString { public static void main(String[] args) { String names = “Tom,Steve,John,Megan,Melissa”; // split string String[] arr = names.split(“,”); // print the size of the array System.out.println(arr.length); // print the elements Stream.of(arr).forEach(System.out::println); } } Output: 5 Tom Steve John Megan Melissa If you need a list, instead of an array, use the…
Read More Split a comma-separated String in Javaclass BreakOutOfLoop { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; // iterate over array for (int i = 0; i < arr.length; i++) { if (arr[i] == 3) { // break out of the loop if the current value is equals to 3 System.out.println(“Breaking out of the loop!”);…
Read More How to break out of the loop 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 JavaOutput: Some String Here, we put the creation of the Scanner object into a try-with-resources because we want to close the Scanner after it reads all the data. We passed the “\\A” to tell the Scanner to read the entire input stream. import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; class ConvertInputStreamToString { /** *…
Read More Convert InputStream to a String 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 Javaclass EscapeDoubleQuotesInString { public static void main(String[] args) { String str = “Hello, I’m \”Steve\” and I am \”Java\” developer”; System.out.println(str); } } Output: Hello, I’m “Steve” and I am “Java” developer Escape double quotes in a JSON object in Java There are many cases where you might need to escape double quotes. In this…
Read More Escape Double Quotes in Java String<dependency> <groupid>org.apache.poi</groupid> <artifactid>poi-ooxml</artifactid> <version>5.0.0</version> </dependency> If you are not familiar with Maven, you can check out this tutorial: Create Java Project with Maven. import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class Product { private int id; private String color; private String size; private int cost; private String description; } import org.apache.poi.ss.usermodel.Cell;…
Read More Read and write Excel files in JavaWant to learn how to merge two ArrayLists in Java? In this post, we will cover the following ways: Using the List.addAll() method Using the Java 8 Streams Using the ListUtils.union() method Merge two ArrayLists in Java using the List.addAll() method There is addAll() method from the List interface that appends all of the elements in…
Read More How to merge two ArrayLists in Java?In this post, you will learn how to Serialize and Deserialize an ArrayList in Java using the FileOutputStream and FileInputStream classes. How to Serialize an ArrayList in Java? Serialization is the process of changing the state of an object into a byte stream. For example, we use it to write some Java objects into a file. In…
Read More Serialize and Deserialize an ArrayList in JavaNeed to remove a character from String in Java? You can do that by converting the String to a StringBuilder class and using the deleteCharAt() method that removes the character at a specified position. Example class Test { public static void main(String[] args) { String str = “Learn Java”; str = new StringBuilder(str).deleteCharAt(5).toString(); System.out.println(str); } }…
Read More Remove a Character from String in JavaIn this post, you will learn to create a Singleton class in Java. Let’s first see what Singleton class is. What is a Singleton class in Java? A Singleton class in Java is a class that can have only one object at a time. Singleton’s purpose is to control object creation. We can ensure that only one…
Read More Create a Singleton class in JavaIn one of the previous examples, we covered how to sort an ArrayList in ascending order. In this post, you will learn how to sort ArrayList in descending order in Java. We will explore the following ways: Using the sort() method Using the Collections.sort() and Collections.reverse() methods Sort ArrayList in descending order in Java using the…
Read More Sort ArrayList in descending order in JavaWant to learn how to count character occurrences in Java String? Below is the program that uses a Map to store chars as keys and the number of occurrences as values. class Test { public static void main(String[] args) { String str = “moiujjmhrqdoasdsooqavvae”; Map<Character, Integer> map = new HashMap<>(); for (int i = 0;…
Read More Count Character occurrences in Java String