[英]How to compare arrays with duplicates as sets?
我有兩個可能有重復的數組。 我需要將它們作為集合進行比較。
例如, {1, 4, 9, 16, 9, 7, 4, 9, 11}
等同於{11, 11, 7, 9, 16, 4, 1}
。 我已經嘗試了很多方法,但我一直得到錯誤或錯誤的答案。 這是我現在的代碼:
import java.util.Scanner;
public class sameElement{
public static void main(String[] args){
int[] value1 = {11, 7, 9, 16, 4, 1};
int[] value2 = {11, 11, 7, 9, 16, 4, 1};
sort(value1);
sort(value2);
System.out.println(sameSet(value1, value2));
}
public static boolean sameSet(int[] a, int[] b){
int j = 0;
int counter2 = 0;
for(int i = 0; i < b.length; i++){
if(a[j] == b[i]){j++;}
else{counter2++;};}
}
public static int[] sort (int[] a){
for (int i = 0; i < a.length; i++) {
for (int i2 = i + 1; i2 < a.length; i2++){
if (a[i] > a[i2]){
int temp = a[i2];
a[i2] = a[i];
a[i] = temp;}
}
}
return a;
}
}
TreeSet是一個有序的集合,因此它將免費進行排序和重復刪除。 因此,您所要做的就是將數組加載到其中,然后使用.equals()。
Integer[] value1 = { 11, 7, 9, 16, 4, 1 };
Integer[] value2 = { 11, 11, 7, 9, 16, 4, 1 };
Set<Integer> tSet1 = new TreeSet<Integer>(Arrays.asList(value1));
Set<Integer> tSet2 = new TreeSet<Integer>(Arrays.asList(value2));
System.out.println(tSet1);
System.out.println(tSet2);
System.out.println(tSet1.equals(tSet2));
產量
[1, 4, 7, 9, 11, 16]
[1, 4, 7, 9, 11, 16]
true
使用Arrays.sort
對數組進行排序。
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.HashSet;
import java.util.Set;
public class Test {
public static class SameElement {
/**
* Constructor.
*/
private SameElement()
{
// avoid instatiation
}
/**
* Check if the provided 2 arrays have the same elements ignoring order and duplicates
*
* @param val1 1st array
* @param val2 2nd array
* @return true if so.
*/
public static boolean sameSet(int[] val1, int[] val2)
{
return toSet(val1).equals(toSet(val2));
}
/**
* Transform provided array of int into a {@link Set} of {@link Integer}.
*
* @param vals Array of int to use
* @return a {@link Set} of {@link Integer} (empty if vals is null)
*/
private static Set<Integer> toSet(int[] vals)
{
final Set<Integer> set = new HashSet<Integer>();
if (vals != null) {
for (final int i : vals) {
set.add(i);
}
}
return set;
}
}
@org.junit.Test
public void testSameSet()
{
int[] value1 = { 11, 7, 9, 16, 4, 1 };
int[] value2 = { 11, 11, 7, 9, 16, 4, 1 };
int[] value3 = { 8, 11, 11, 7, 9, 16, 4, 1 };
assertTrue(SameElement.sameSet(value1, value2));
assertFalse(SameElement.sameSet(value3, value1));
assertFalse(SameElement.sameSet(value3, value2));
assertFalse(SameElement.sameSet(null, value2));
assertFalse(SameElement.sameSet(value1, null));
assertTrue(SameElement.sameSet(null, null)); // check against your requirements
}
}
int[] value1 = {11, 7, 9, 16, 4, 1};
int[] value2 = {11, 11, 7, 9, 16, 4, 1};
HashSet<Integer> set = new HashSet<Integer>();
HashSet<Integer> set2 = new HashSet<Integer>();
for(int i=0; i<value1.length;i++) {
set.add(value1[i]);
}
for(int j=0; j<value2.length; j++) {
set2.add(value2[j]);
}
now do the sorting and compare both the sets
我已經修改了你的sameSet
方法,它不需要對數組進行排序,並且在不刪除它們的情況下處理重復項。
public static boolean sameSet(int[] a, int[] b)
{
for (int i = 0; i < a.length; i++)
{
boolean match = false;
for (int j = 0; j < b.length; j++)
{
if (a[i] == b[j])
{
match = true;
break;
}
}
if (!match)
return false;
}
return true;
}
我有很大的優化空間,但目前服務的目的。
以下是我附帶的代碼。
這是代碼。
public static void main(String[] args) {
int[] value1 = { 11, 7, 9, 16, 4, 1 };
int[] value2 = { 11, 11, 7, 9, 16, 4, 1 };
int[] firstArray = null;
int[] secondArray = null;
//Max Length Array should be used for outer loop
if(value2.length >value1.length)
{
firstArray = value2;
secondArray = value1;
}
else
{
firstArray = value1;
secondArray = value2;
}
boolean equal = true;
for (int i = 0; i < firstArray.length; i++) {
boolean found = false;//each iteration initialise found to false
for (int j = 0; j < secondArray.length; j++) {
if (firstArray[i] == secondArray[j]) {
found = true;
break;// as there is no point running the loop
}
}
if (!found) {
equal = false;//check after each iteration if found is false if false arrays are not equal and break
break;
}
}
System.out.println(equal);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.