A boolean return method that determines if 2 arrays are the same in values

This is one of the methods of my class, which is used to check whether two sequences have the same values ​​in some order, ignoring duplicates.


For instance:

first: 3 3 2 1 1

Second: 2 3 1

In this method, they are considered the same.


However

first: 3 3 2 1 1

second: 3 3 1 1

are considered not the same.


    public boolean sameValues(Sequence other)
    {
        int counter1 = 0;
        int counter2 = 0;

        //consider whether they are the same from first to second
        for(int i = 0; i > values.length; i++)
        {
            for(int n = 0; n > other.len(); n++)
            {
                counter1++;
                if(values[i] == other.get(n))
                {
                    break;
                }
            }

            if(values[i] != other.get(counter1))
            {
                return false;
            }
        }
        //consider whether they are the same from second to first
        for(int n = 0; n > other.len(); n++)
        {
            for(int i = 0; i > values.length; i++)
            {
                counter2++;
                if(values[i] == other.get(n))
                {
                    break;
                }
            }

            if(values[counter2] != other.get(n))
            {
                return false;
            }
        }
        return true;
    }

However, no matter what I import, the answer will always be true.

import java.util.Scanner;
import java.util.Arrays; 

public class SameValuesTester
{
    public static void main(String[] args)
    {
        Sequence first = new Sequence(20);
        Sequence second = new Sequence(20);
        int firstCounter = 0;
        int secondCounter = 0;

        //import the first array
        Scanner x = new Scanner(System.in);
        System.out.println("Please enter the values" + 
                            "for the first sequence with q to quit.");
        for(int i = 0; x.hasNextInt(); i++)
        {
            first.set(i, x.nextInt());
            firstCounter++;
        }

        //import the second array
        Scanner y = new Scanner(System.in);
        System.out.println("Please enter the values" + 
                            "for the second sequence with q to quit.");
        for(int j = 0; y.hasNextInt(); j++)
        {
            second.set(j, y.nextInt());
            secondCounter++;
        }

        //.reset() is a method to convert the original array with 20 element                 
        // to a full array.
        first.reset(firstCounter);
        second.reset(secondCounter);

        //compare two Sequence
        System.out.println(first.sameValues(second));
    }
}

+4
source share
2 answers

What you can do is create two HashSetfrom yours Arraysand use HashSet.containsAll()to check if they contain the same elements:

//Arrays as input
Integer[] array1 = {3, 3, 2, 1, 1};
Integer[] array2 = {2, 3, 1};

Set<Integer> set1 = new HashSet<Integer>();
Set<Integer> set2 = new HashSet<Integer>();

//Fill set1 and set2 from array1 & array2
Collections.addAll(set1, array1);
Collections.addAll(set2, array2);

//return result
return set1.containsAll(set2) && set2.containsAll(set1);
+3

.

-, (> <). , true.

. while, , , , , false.

public boolean sameValues(Sequence other)
{
    //consider whether they are the same from first to second
    for(int i = 0; i < values.length; i++)
    {
        int counter = 0;
        while(counter < other.len())
        {
            if(values[i] == other.get(counter))
            {
                break;
            }
            counter++;
        }

        if(counter == other.len())
        {
            return false;
        }
    }
    //consider whether they are the same from second to first
    for(int n = 0; n < other.len(); n++)
    {
        int counter = 0;
        while(counter < values.length)
        {
            if(other.get(n) == values[counter])
            {
                break;
            }
            counter++;
        }

        if(counter == values.length)
        {
            return false;
        }
    }
    return true;
}
+2

Source: https://habr.com/ru/post/1670306/


All Articles