Java 在两个数组中查找相同的重数
我需要有关SameeElements()方法的帮助。我需要取两个随机排列的用户输入数组,看看它们是否具有相同的多重性 我知道如果它们没有相同的长度,那么它们就不会有相同的元素。但我只有这些。是否可以为此修改sameSet()方法中的代码?还是我需要做一些完全不同的事情Java 在两个数组中查找相同的重数,java,arrays,Java,Arrays,我需要有关SameeElements()方法的帮助。我需要取两个随机排列的用户输入数组,看看它们是否具有相同的多重性 我知道如果它们没有相同的长度,那么它们就不会有相同的元素。但我只有这些。是否可以为此修改sameSet()方法中的代码?还是我需要做一些完全不同的事情 ` /** * checks whether two arrays have the same elements in some order, with * the same multiplicities. For exam
` /**
* checks whether two arrays have the same elements in some order, with
* the same multiplicities. For example, 1 4 9 16 9 7 4 9 11 and 11 1 4 9 16
* 9 7 4 9 would be considered identical
*
*/
public static boolean sameElements(Integer a[], int sizeA, Integer b[], int sizeB) {
if (a.length != b.length) {
return false;
}
// need help on this, length portion is all I have.
}
/**
* check whether two arrays have the same elements in some order, ignoring
* duplicates. For example, the two arrays 1 4 9 16 9 7 4 9 11 and 11 11 7 9
* 16 4 1 would be considered identical.
*/
public static boolean sameSet(Integer[] a, int sizeA, Integer[] b, int sizeB) {
HashSet<Integer> set1 = new HashSet<Integer>(Arrays.asList(a)); // Put arrays a & b into hashset */
HashSet<Integer> set2 = new HashSet<Integer>(Arrays.asList(b));
//Compares the sets to see if they have the same elements
if (set1.equals(set2)) {
System.out.println("The elements of the arrays A and B form the same set.");
} else {
System.out.println("The elements of the arrays A and B do not1 form the same set.");
}
return set1.equals(set2);
}
public static void main() {
final int LENGTH = 30;
int sizeA = 0;
int sizeB = 0;
Integer a[] = new Integer[LENGTH];
Integer b[] = new Integer[LENGTH];
Scanner input = new Scanner(System.in);
Scanner in = new Scanner(System.in);
System.out.println("Please fill up values for array a, Q to quit: "); //Gather user input for array A
while (input.hasNextInt() && sizeA < a.length) {
a[sizeA] = input.nextInt();
sizeA++;
}
System.out.println("# of values in array A:" + sizeA + "\n");
System.out.println("Please fill up values for array b, type in Q to quit: "); //Gather user input for array B
while (in.hasNextInt() && sizeB < b.length) {
b[sizeB] = in.nextInt();
sizeB++;
}
System.out.println("# of values in array B:" + sizeB);
//Take the user input and test it through each method
equals(a, sizeA, b, sizeB);
sameSet(a, sizeA, b, sizeB);
sameElements(a, sizeA, b, sizeB);
}
}
`/**
*检查两个数组是否以某种顺序具有相同的元素,并使用
*同样的多重性。例如,1 4 9 16 9 7 4 9 11和11 1 4 9 16
*9 7 4 9将被视为相同
*
*/
公共静态布尔sameements(整数a[],整数sizeA,整数b[],整数sizeB){
如果(a.长度!=b.长度){
返回false;
}
//在这方面需要帮助,长度部分是我所有的。
}
/**
*检查两个数组是否以某种顺序具有相同的元素,忽略
*复制品。例如,两个阵列1 4 9 16 9 7 4 9 11和11 11 7 9
*16 4 1将被视为相同。
*/
公共静态布尔sameSet(整数[]a,整数大小a,整数[]b,整数大小b){
HashSet set1=新的HashSet(Arrays.asList(a));//将数组a和b放入HashSet*/
HashSet set2=新的HashSet(Arrays.asList(b));
//比较这些集合以查看它们是否具有相同的元素
if(set1.等于(set2)){
System.out.println(“数组A和B的元素构成同一个集合”);
}否则{
System.out.println(“数组A和B的元素不构成同一个集合”);
}
返回set1.equals(set2);
}
公共静态void main(){
最终整数长度=30;
int-sizeA=0;
int-sizeB=0;
整数a[]=新整数[长度];
整数b[]=新整数[长度];
扫描仪输入=新扫描仪(System.in);
扫描仪输入=新扫描仪(系统输入);
System.out.println(“请填写数组a的值,Q退出:”;//收集数组a的用户输入
while(input.hasNextInt()&&sizeA
对不起。我没有很好地理解这个问题。要执行所需操作,可以按最大值和最小值对每个数组中的值进行排序,然后检查它们是否相等
这里的代码在数组中循环,并用另一个数组的元素检查每个元素
boolean equal = true;//If true after for then it is equal if false then it is not equal.
for(int i = 0; i < a.length; a++){
if(!a[i].equals(b[i])){
equal = false;
break;
}
}
boolean equal=true//如果在for之后为true,则该值相等;如果为false,则该值不相等。
for(int i=0;i
您基本上想知道数组是否是彼此的字谜。该问题的一般解决方案是对它们进行排序(
array.sort
),然后检查它们是否相等
请参见使用集合的containsAll方法可以轻松实现。我已经写了下面的程序。请检查它是否对您有帮助
Set a = new HashSet<Integer>();
Set b = new HashSet<Integer>();
a.add(1);
a.add(2);
a.add(3);
b.add(3);
b.add(2);
b.add(1);
if(a.containsAll(b) && b.containsAll(a))
{
System.out.println("Same elements in both");
}
else
{
System.out.println("Not same elements in both");
}
Set a=newhashset();
Set b=新的HashSet();
a、 增加(1);
a、 增加(2);
a、 增加(3);
b、 增加(3);
b、 增加(2);
b、 增加(1);
如果(a.containsAll(b)和&b.containsAll(a))
{
System.out.println(“两者中的元素相同”);
}
其他的
{
System.out.println(“两者中的元素不相同”);
}
array.sort如果所有数组值都未填充,会引发异常,不是吗?@Maxwell1222我怀疑它会,但即使它填充了,您也可以创建自己的异常,@Maxwell1222是的。如果可能的话,您应该使用int[]而不是Integer[]。因为您知道如何将数组转换为集合,所以我在代码中保留了这一部分