Searching...
Saturday, 2 November 2013

How to compare Arrays contents in Java ?

00:58

Arrays.equals(array1, array2) compares the contents of the arrays. See one simple example.

Example

package com.pretech;
import java.util.Arrays;
public class ArrayEqualsExample {
	public static void main(String[] args) {
		String[] array1 = { "Sunday", "Monday", "Tuesday" };
                  String[] array2 = { "January", "February", "March" };
		String[] array3 = { "Sunday", "Monday", "Tuesday" };
        System.out.println("Compare between array1 & array2: " + Arrays.equals(array1, array2));
        System.out.println("Compare between array1 & array3: " + Arrays.equals(array1, array3));
	}
}

Output



Compare between array1 & array2: false
Compare between array1 & array3: true


0 comments: