What is java.util.Arrays$ArrayList?

The java.util.Arrays$ArrayList is a nested class inside the Arrays class. It is a fixed size or immutable list backed by an array. Arrays.java public static <T> List<T> asList(T… a) { return new ArrayList<>(a); } /** * @serial include */ private static class ArrayList<E> extends AbstractList<E> implements RandomAccess, java.io.Serializable { private static final long serialVersionUID = …

Read more

Java – Convert Array to ArrayList

In Java, we can use new ArrayList(Arrays.asList(array)) to convert an Array into an ArrayList ArrayExample1.java package com.mkyong; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ArrayExample1 { public static void main(String[] args) { String[] str = {"A", "B", "C"}; List<String> list = new ArrayList<>(Arrays.asList(str)); list.add("D"); list.forEach(x -> System.out.println(x)); } } Output A B C D …

Read more

Java two-dimensional array example

A Java 2d array example. ArrayExample.java package com.mkyong; public class ArrayExample { public static void main(String[] args) { int[][] num2d = new int[2][3]; num2d[0][0] = 1; num2d[0][1] = 2; num2d[0][2] = 3; num2d[1][0] = 10; num2d[1][1] = 20; num2d[1][2] = 30; //or init 2d array like this : int[][] num2dInit = { {1, 2, 3}, …

Read more

Java 8 – Convert a Stream to Array

In Java 8, we can use .toArray() to convert a Stream into an Array. 1. Stream -> String[] StreamString.java package com.mkyong; import java.util.Arrays; public class StreamString { public static void main(String[] args) { String lines = "I Love Java 8 Stream!"; // split by space, uppercase, and convert to Array String[] result = Arrays.stream(lines.split("\\s+")) .map(String::toUpperCase) …

Read more

Java 8 – How to convert IntStream to Integer[]

The key is boxed() the IntStream into a Stream<Integer>, then only convert to an Array. StreamExample.java package com.mkyong; import java.util.Arrays; import java.util.stream.IntStream; import java.util.stream.Stream; public class StreamExample { public static void main(String[] args) { //int[] -> IntStream -> Stream<Integer> -> Integer[] int[] num = {3, 4, 5}; //1. int[] -> IntStream IntStream stream = Arrays.stream(num); …

Read more

Java – How to declare and initialize an Array

Few Java examples to declare, initialize and manipulate Array in Java 1. Declares Array 1.1 For primitive types. ArrayExample1.java package com.mkyong; import java.util.Arrays; public class ArrayExample1 { public static void main(String[] args) { //declares an array of integers int[] num1 = new int[5]; int[] num2 = {1, 2, 3, 4, 5}; int[] num3 = new …

Read more

Java ArrayIndexOutOfBoundsException example

This java.lang.ArrayIndexOutOfBoundsException is thrown when we are accessing an array with an index which is greater than the size of an array. P.S Array index starts with 0 ArrayExample.java package com.mkyong; public class ArrayExample { public static void main(String[] args) { // array of 3 int[] num = new int[3]; num[0] = 1; num[1] = …

Read more

How to copy an Array in Java

The methods described below are only applicable to one dimensional arrays. Before we talk about the different ways to copy an array in Java we will show you how NOT to copy an Array. How NOT to copy an Array in Java Arrays in Java are Objects. If you try to treat them as variables… …

Read more

Java – Check if Array contains a certain value?

Java examples to check if an Array (String or Primitive type) contains a certain values, updated with Java 8 stream APIs. 1. String Arrays 1.1 Check if a String Array contains a certain value “A”. StringArrayExample1.java package com.mkyong.core; import java.util.Arrays; import java.util.List; public class StringArrayExample1 { public static void main(String[] args) { String[] alphabet = …

Read more

Java – How to convert Array to Stream

In Java 8, you can either use Arrays.stream or Stream.of to convert an Array into a Stream. 1. Object Arrays For object arrays, both Arrays.stream and Stream.of returns the same output. TestJava8.java package com.mkyong.java8; import java.util.Arrays; import java.util.stream.Stream; public class TestJava8 { public static void main(String[] args) { String[] array = {"a", "b", "c", "d", …

Read more

Java – How to join Arrays

In this article, we will show you a few ways to join a Java Array. Apache Commons Lang – ArrayUtils Java API Java 8 Stream 1. Apache Commons Lang – ArrayUtils The simplest way is add the Apache Commons Lang library, and use ArrayUtils. addAll to join arrays. This method supports both primitive and object …

Read more

How to print an Array in Java

In this article, we will show you a few ways to print a Java Array. Table of contents. 1. JDK 1.5 Arrays.toString 2. Java 8 Stream APIs 3. Java 8 Stream APIs – Collectors.joining 4. Jackson APIs 5. References 1. JDK 1.5 Arrays.toString We can use Arrays.toString to print a simple array and Arrays.deepToString for …

Read more

MongoDB – Remove a field from array

Prior to MongoDB 2.6, there is no official function to $unset a field from array document. Note $unset is not working with arrays $pull is used to remove the entire record, not a particular field. 1. Arrays Documents In this article, we will show you how to remove the field “countryName” from below array. > …

Read more

How to sort an Array in java

Java example to show the use of Arrays.sort() to sort an Array. The code should be self-explanatory. import java.util.Arrays; import java.util.Collections; public class ArraySorting{ public static void main(String args[]){ String[] unsortStringArray = new String[] {"c", "b", "a", "3", "2", "1"}; int[] unsortIntArray = new int[] {7,5,4,6,1,2,3}; System.out.println("Before sort"); System.out.println("— unsortStringArray —"); for(String temp: unsortStringArray){ System.out.println(temp); …

Read more

How to convert Array to List in Java

Java example to show you how to convert a Array to a List ArrayToList.java package com.mkyong; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class ArrayToList { public static void main(String[] argv) { String sArray[] = new String[] { "A", "B", "C" }; // convert array to list #1 List<String> list = Arrays.asList(sArray); System.out.println(list); …

Read more

Java – Check if array contains duplicated value

This article shows a few ways to detect if an array contains a duplicated value. Java 8 Stream, compare the array size. TreeSet, compare the array size. Two for loops, classic algorithm, 0(n^2). Set, 0(n). Bitmap, boolean[] 0(n). All the above solutions are tested with the below program and generate the same output. (except 5. …

Read more