You can convert your arrays to a List and then use the containsAll method to see if a particular list contains all elements described in another list.

You would get better performance out of it if they were Sets instead.

Example:

List<String> firstList = Arrays.asList("chery", "chery", "unid", ...);
List<String> secondList = Arrays.asList("chery", "unid", ...);

System.out.println(secondList.containsAll(firstList));

If the performance of this method in particular is getting a bit dodgy, then consider converting the lists into Sets instead:

Set<String> firstSet = new HashSet<>(Arrays.asList("chery", "chery", "unid", ...));
Answer from Makoto on Stack Overflow
🌐
Coderanch
coderanch.com › t › 655592 › java › compare-ArrayList-size
how to compare two ArrayList with different size (Beginning Java forum at Coderanch)
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums · this forum made possible by our volunteer staff, including ... ... So I have 2 ArrayList but they are have different size i try to do while loop but i'm not sure I do it right pseudocode ArrayList<String> listA ArrayList<String> listB int i = 0; if(listA.size > listB.size) // to compare with size is larger { while(listA > 0) { if ListA equals to ListB remove listA; } i++; } Any suggest for this thank you.
🌐
Medium
medium.com › @AlexanderObregon › javas-arrays-equals-method-explained-271f9420177d
Java’s Arrays.equals() Method Explained | Medium
September 11, 2024 - The first thing Arrays.equals() does is check whether the two arrays being compared have the same length. If the arrays don’t match in length, they are immediately considered unequal, and the method returns false. This is a highly efficient first check, as arrays of different sizes can never be equal, regardless of their contents.
Top answer
1 of 3
1

Judging by the picture, you are not placing the values in the correct index in the second array, or you are not matching the correct ones.

If you want it to look exactly like in the picture, the second array should be declared:

double[] newArray = new double[array.length - 2];

And the loop to fill it should be changed to:

    for(j = 2; j < array.length - 2; j++)
    {
        for(int k = -2; k <= 2; k++)
        {
            newArray[j] += array[j+k] / 5;  
        }

    }

This will put the averages in the third, fourth, fifth... elements in newArray. And now you can compare them directly:

    for(int i = 2; i < array.length - 2; i++)
    {
        if(array[i] > (newArray[i] + 0.999))
        {
            count++;
            tempVal = count;
        }

        System.out.println(tempVal);
    }

If you want to save the two unused spaces, as you originally did, rather than responding exactly to the picture, then you should calculate the values as you originally did. But remember to compare each element to the one two places before it and stop 2 places before the end.

Instead of

for(int i = 2; i < array.length; i++)

use

for(int i = 2; i < array.length - 2; i++)

To print the location, your construct with the count and tempVal is unnecessary. You just need to print i+1. Also note that you have a ; after your if. This means it's an empty if, and the block after it is always performed. Never have a ; after an if, for, while etc.

2 of 3
0

Not clear with what you are asking for in your question but without questioning what's the logic, by just looking at your code:

    for(int i = 2; i < array.length; i++)
    {
        if(array[i] > (newArray[i-2] + 0.999));
        {
            count++;
            tempVal = count;
        }

         System.out.println(tempVal);
    }   
    }

if you relocate the system.out line as follows, I think you will get what you expect as follows:

    for(int i = 2; i < array.length - 2; i++)
    {
        if(array[i] > (newArray[i-2] + 0.999));
        {
            System.out.println(tempVal);
            // count++;
            // tempVal = count;
        }


    }   
    }

PS: Please note that I have also changed the boundary for the loop to stop iteration on 13th member of the array, instead of 15.

🌐
DataCamp
datacamp.com › doc › java › compare
Java Array compare()
The Arrays.compare() method compares two arrays lexicographically. It returns 0 if the arrays are equal, a negative integer if the first array is lexicographically less, and a positive integer if the first array is lexicographically greater. This method can be used for arrays of various types ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › compare-two-arrays-java
Compare Two Arrays in Java - GeeksforGeeks
July 23, 2025 - Java provides a direct method Arrays.equals() to compare two arrays. There is a list of equals() methods in the Arrays class for different primitive types (int, char etc.) and one for Object type (which is the base of all classes in Java).
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-arrays-compare-method-with-examples
Java Arrays compare() Method with Examples - GeeksforGeeks
import java.util.Arrays; public ... elements int[] arr1 = { 1, 2, 3, 4 }; int[] arr2 = { 1, 2, 3, 4 }; // compare arr1 and arr2 using compare() method // and print the result System.out.println( "" + Arrays.compare(arr1, arr2)); ...
Published: April 14, 2026
🌐
Baeldung
baeldung.com › home › java › java array › comparing arrays in java
Comparing Arrays in Java | Baeldung
January 8, 2024 - Having sorted the arrays in the same order first, we allow the deepEquals method to find if these two arrays are equal. Java 9 introduced a brand new method in the Arrays utility class called compare().
Find elsewhere
🌐
Javatpoint
javatpoint.com › how-to-compare-two-arrays-in-java
How to Compare Two Arrays in Java - Javatpoint
How to Compare Two Arrays in Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
LabEx
labex.io › tutorials › java-how-to-compare-two-long-arrays-in-java-413956
How to compare two long arrays in Java | LabEx
By understanding and applying these ... have learned how to efficiently compare two long arrays, including using the Arrays.equals() method and implementing custom comparison logic....
🌐
YouTube
youtube.com › watch
How to Compare Arrays in Java - Comparing 2 Arrays by Example - Learn Java Programming - Appficial - YouTube
When comparing two arrays is Java, you cannot use the == operator to compare arrays because iIt will only tell you if the two arrays point to the same object...
Published: March 1, 2018
🌐
CodeJava
codejava.net › java-core › collections › comparing-arrays-in-java-examples
Comparing Arrays in Java Examples
April 11, 2024 - String[][] data1 = { {"Name", "City", "Country"}, {"Bill", "Hawaii", "USA"}, {"David", "Madrid", "Spain"}, }; String[][] data2 = { {"Name", "City", "Country"}, {"Bill", "Hawaii", "USA"}, {"David", "Madrid", "Spain"}, }; String[][] data3 = { {"Name", "City", "Country"}, {"David", "Madrid", "Spain"}, {"Bill", "Hawaii", "USA"}, }; boolean equal = Arrays.deepEquals(data1, data2); System.out.println("data1 == data2 ? " + equal); equal = Arrays.deepEquals(data1, data3); System.out.println("data1 == data3 ? " + equal);Output: data1 == data2 ? true data1 == data3 ? false Note that, two array references are considered equal if both are null.
🌐
Medium
medium.com › @codeforhunger › how-to-compare-arrays-in-java-with-examples-93e96ed08ca8
How to Compare Arrays in Java with Examples | by CodeForHunger | Medium
September 3, 2024 - import java.util.Arrays; public class ComparingArrays { public static void main(String[] args) { int arr1[] = {1,2,3,4,5}; int arr2[] = {1,2,3,4,5}; //a1[] and a2[] contains only one elements Object a1[] = {arr1}; Object a2[] = {arr2}; //deeply comparing array contents if(Arrays.deepEquals(a1,a2)) System.out.println("Given two arrays are the same"); else System.out.println("Given arrays are not the same"); } }
🌐
TutorialsPoint
tutorialspoint.com › how-to-compare-two-arrays-in-java
How to compare two arrays in Java
Following example uses equals method to check whether two arrays are or not. import java.util.Arrays; public class Main { public static void main(String[] args) throws Exception { int[] ary = {1,2,3,4,5,6}; int[] ary1 = {1,2,3,4,5,6}; int[] ...
🌐
YouTube
youtube.com › watch
How to compare two Arrays in Java #shorts #javainterviewquestions #javaprogramming - YouTube
How to compare two Arrays in Java #shorts #javainterviewquestions #javaprogramming
Published: March 19, 2023
🌐
NavTutorial
navtutorial.com › home › code snippets › compare two arrays for equality in java (arrays.equals() & beyond)
Compare Two Arrays in Java: Arrays.equals() Guide
July 13, 2026 - In most Java problems, both phrases refer to comparing the array contents rather than their memory references. Yes, provided the custom objects correctly override the equals() method. Arrays.equals() immediately returns false because arrays ...
🌐
Medium
medium.com › @AlexanderObregon › javas-arrays-compare-method-explained-7989daf18608
Java’s Arrays.compare() Method Explained | Medium
February 6, 2025 - The compare() method in the java.util.Arrays class provides a way to compare two arrays lexicographically. This means it determines which array is greater, smaller, or if they are equal by checking the elements in order.