Array in Java (part 2)

In Java programming language, there are many useful methods, utilities for working with an array. All of those things are defined in Arrays class which could be imported via library java.util.Arrays.

In this part, I will give some examples of the popular static methods that can be used for integer, float, byte, char, double, short, boolean.

Printing an array

Different from other data types, when we want to print out the data of an array, we have to use method toString(). For more detail, the toString() method will convert an Array object to a String object including “[]” and “,”.

In the case of without using toString() method, although the program still prints out the data of our array, just for machine’s understand.

import java.util.Arrays;

public class testArray{
    int[] numbers = {1,2,3,4,5,6};
    System.out.println(numbers);
    }
}
[I@3b22cdd0 

If we use toString() method to convert the data to String which can be readable by human, the result could be more understandable.

import java.util.Arrays;
public class testArray{
    int[] numbers = {1,2,3,4,5,6};
    System.out.println(Arrays.toString(numbers));
    }
}
[1, 2, 3, 4, 5, 6]

Comparison two arrays

There are some ways to compare two arrays; for example, we can use the For loop to compare each index of those arrays. However, the easiest way is using the built-in method equals(). Similar with toString, the syntax is Arrays.equals(array1, array2).

NOTE: equals() method only compare the same data type.

import java.util.Arrays;

public class Comparison{
    public static void main(String[] args){
        int[] a = {1,2,3,4};
        int[] b = {2,3,4,5};
        System.out.println(Arrays.equals(a,b));
    }
}

The output will be true or false

false

Copying the array

During the coding process, we might want to use the similar arrays from the original; Or we only want to use some of the values from an array, instead of assigning the values again, we can use the copy method for those value.

import java.util.Arrays;

public class Cpy{
         int [] b = {1,0,3,4,5,6};
//creating a new array with a specify length
         int [] c = Arrays.copyOf(b,3);
//creating a new array with the specify values
         int[] d = Arrays.copyOfRange(b,2,5);
         System.out.println(Arrays.toString(c));
         System.out.println(Arrays.toString(d));
}
[1, 0, 3]
[3, 4, 5]

Replacing all by one

In the case we want to assign or replace all indexes of an array by one value, we can use the fill() method. For example:

import java.util.Arrays;
public class FillingValue{
    public static void main(String[] args){
         int[] numbers = {1,2,3,4,5};
         int[] newArr = Arrays.copyOf(numbers,3);
         int[] d = new int[4];
        System.out.println("before: "+ Arrays.toString(newArr));
        System.out.println("before: " + Arrays.toString(d));
         Arrays.fill(newArr,3);
         Arrays.fill(d,2);
        System.out.println("after filling: "+ Arrays.toString(newArr));
        System.out.println("after filling: " + Arrays.toString(d));
    }
}
 before: [1, 2, 3]
 before: [0, 0, 0, 0]
 after filling: [3, 3, 3]
 after filling: [2, 2, 2, 2] 

Conclusion

In this article, I mentioned some popular useful methods and how to use it in the real cases. I hope it helped you some how. There are many other methods that you can read more from this document.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: