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;
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;
}
}
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;
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++;
}
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++;
}
first.reset(firstCounter);
second.reset(secondCounter);
System.out.println(first.sameValues(second));
}
}
source
share