How do I search specific value in an array?

package org.kodejava.util;

import java.util.Arrays;

public class ArraySearchExample {
    public static void main(String[] args) {
        // We create an array of ints where the search will be done.
        int[] items = {9, 5, 14, 6, 11, 28, 9, 16, 37, 3, 2};

        // The Arrays.binarySearch() require us to sort the array
        // items before we call the method. We can utilize the
        // Arrays.sort() method to do this. If we did not sort the
        // array the result will be undefined, the search process
        // will return a negative result.
        Arrays.sort(items);

        // To search we use Arrays.binarySearch() methods which accept
        // parameters of any array type. To search we passed the array
        // to be searched and the key to be searched for.
        //
        // When the search item exist more than one in the array, this
        // method gives no guarantee which one will be returned.
        int needle = 9;
        int index = Arrays.binarySearch(items, needle);

        // Print out where the 9 number is located in the array.
        System.out.println("Items: " + Arrays.toString(items));
        System.out.println("Item " + needle + " is at index " + index);
    }
}

There result of the code snippet:

Items: [2, 3, 5, 6, 9, 9, 11, 14, 16, 28, 37]
Item 9 is at index 5

How do I sort elements of an array?

package org.kodejava.util;

import java.util.Arrays;

public class ArraySortExample {
    public static void main(String[] args) {
        // An array of random numbers
        int[] numbers = {3, 1, 8, 34, 1, 2, 13, 89, 5, 21, 55};
        System.out.println("Before: " + Arrays.toString(numbers));

        // We need to sort these array elements into a correct order
        // from the smallest to the greatest. We will use the Arrays
        // class on java.utils package to do the sort. The sort
        // method of this class are overloaded, so they can take
        // other type of array as well such as byte[], long[],
        // float[], Object[].
        Arrays.sort(numbers);
        System.out.println("After : " + Arrays.toString(numbers));

        // We can also do the sort only for the specified range of
        // array elements.
        float[] money = {1.05f, 99.8f, 3f, 4.55f, 7.23f, 6.50f};
        Arrays.sort(money, 3, money.length);

        // Here we display the sort result, the first and the second
        // element of the array is not included in the sort process.
        System.out.println("Money : " + Arrays.toString(money));
    }
}

And here are the results:

Before: [3, 1, 8, 34, 1, 2, 13, 89, 5, 21, 55]
After : [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Money : [1.05, 99.8, 3.0, 4.55, 6.5, 7.23]

How do I convert an array into a collection object?

To convert array based data into List / Collection based we can use java.util.Arrays class. This class provides a static method asList(T... a) that converts array into List / Collection.

package org.kodejava.util;

import java.util.Arrays;
import java.util.List;

public class ArrayAsListExample {
    public static void main(String[] args) {
        String[] words = {"Happy", "New", "Year", "2021"};
        List<String> list = Arrays.asList(words);

        for (String word : list) {
            System.out.println(word);
        }
    }
}

The results of our code are:

Happy
New
Year
2021

How do I compare if two arrays are equal?

Using Arrays.equals() methods we can compare if two arrays are equal. Two arrays are considered to be equal if their length, each element in both arrays are equal and in the same order to one another.

package org.kodejava.util;

import java.util.Arrays;

public class CompareArrayExample {
    public static void main(String[] args) {
        String[] abc = {"Kode", "Java", "Dot", "Org"};
        String[] xyz = {"Kode", "Java", "Dot", "Org"};
        String[] java = {"Java", "Dot", "Com"};

        System.out.println(Arrays.equals(abc, xyz));
        System.out.println(Arrays.equals(abc, java));
    }
}

The Arrays.equals() can be used to compare array of any primitive data type and array of Object. If you run this example you will have a result as follows:

true
false

How do I create a repeated sequence of character?

This example shows you how to create a repeated sequence of characters. To do this, we use the Arrays.fill() method. This method fills an array of char with a character.

package org.kodejava.util;

import java.util.Arrays;

public class RepeatCharacterExample {
    public static void main(String[] args) {
        char c = '*';
        int length = 10;

        // creates a char array with 10 elements
        char[] chars = new char[length];

        // fill each element of the char array with '*'
        Arrays.fill(chars, c);

        // print out the repeated '*'
        System.out.println(String.valueOf(chars));
    }
}

As the result you get the x character repeated 10 times.

**********

For one-liner code, you can use the following code snippet, which will give you the same result.

public class Test {
    public static void main(String[] args) {
        String str = new String(new char[10]).replace('\u0000', '*');
    }
}

Or

import java.util.Collections;

public class Test {
    public static void main(String[] args) {
        String str = String.join("", Collections.nCopies(10, "*"));
    }
}