简体   繁体   中英

Java comparison of array data regardless of size or order

I'm writing a basic program to compare the data in two arrays. The order doesn't matter and neither do repeated numbers. The output is currently saying the array data is the same. Which is not what I want to happen as the array data is different. This is the code I have right now:

public class Algorithm1{
public static void main (String[] args){

    int[] array1 = {1, 2, 3};
    int[] array2 = {1, 2, 3, 4, 5, 6};
    int matchCount = 0;
    boolean match = false;

for(int j = 0; j < array1.length;j++)   {
    for(int i = 0; i < array2.length;i++){
if(array1[j] == array2[i])
    matchCount++;


    }
}
if(matchCount >= array1.length && matchCount >= array2.length)
    match = true;

if(match = true){
    System.out.println("The data in the arrays is the same.");
                }
else if(match = false){
    System.out.println("The data in the arrays is different.");
                }
}
}

Simply replace if(match = true) by if(match == true) or, even simpler, if(match) , and you should be all right ;)

You wrote:

if(match = true) {
    System.out.println("The data in the arrays is the same.");
}

That if(match = true) actually:

  • sets match to true
  • tests if the previous expression was true ... and enters the if :)

As a side note, I would like to point out that you don't need to specify if(match==false) in your else clause... because if we enter that else , it already means that match != true (ie: match == false ).

Cheers!

This is a fairly simple error that a lot of begginning programmers make. When comparing two objects, you use == and when setting, you use =

Instead of:

if(match = true){
    System.out.println("The data in the arrays is the same.");   
}
else if(match = false){
    System.out.println("The data in the arrays is different.");
}

Should be:

if(match == true){
    System.out.println("The data in the arrays is the same.");   
}
else if(match == false){
    System.out.println("The data in the arrays is different.");
}

Well, in reality, it is very redundant. Saying if(match == true) is the same thing as just saying if(match) and no matter what, in the else part, if match is not true, then it must be false, so saying else if(match == false) is unnessesary. You only need to say else .

Here is what it should be with the revisions I have made:

if(match){
    System.out.println("The data in the arrays is the same.");   
}
else{
    System.out.println("The data in the arrays is different.");
}

There is an approach you can utilize on the library. Collections.indexOfSubList(List source, List target

public int findArray(Integer[] arg1, Integer[] arg2)
{
    return Collections.indexOfSubList(Arrays.asList(arg1), Arrays.asList(arg2));
}

Try this code and let me know if you need more explanation if this is what you need

O(nlogn) runtime if your are interested :) if you use a hash or set.. it will be O(n) but higher space complexity

import java.io.*;
import java.util.Arrays;

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        int[] array1 = {1, 2, 3, 6, 5, 4, 3};
        int[] array2 = {1, 2, 3, 4, 5, 6, 4, 4,6};
        java.util.Arrays.sort(array1);
        java.util.Arrays.sort(array2);
        boolean match = true;
        int ptr1 = 0;
        int ptr2 = 0;
        while(ptr1<array1.length && ptr2<array2.length && array1[ptr1]==array2[ptr2])
        {

            ptr1++;ptr2++;
            while(ptr1<array1.length && array1[ptr1]==array1[ptr1-1])
            {
                ptr1++;
            }
            while(ptr2<array2.length && array2[ptr2]==array2[ptr2-1])
            {
                ptr2++;
            }
        }
        if((ptr1 == array1.length) && (ptr2 == array2.length))
            System.out.println("The data in the arrays is the same");
        else
            System.out.println("The data in the arrays is the different");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM