gpt4 book ai didi

java - 如何将公共(public)元素组合到一个数组中?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:21:52 26 4
gpt4 key购买 nike

我正在尝试将三个数组合并为一个数组。只保留共同的元素。这不是一个重复的问题。我知道网上还有其他示例,但那是使用 int [],我不知道如何使用 Comparable。

我需要什么帮助:

  1. 如何将单个组合/更新数组添加到二维数组。

  2. 如何计算元素每次比较的迭代次数。

  3. 如果我想要如何将我现在拥有的数组更改为列表? - 我在想也许添加起来会更容易。

我是编程新手,非常感谢您的帮助。我正在尝试通过阅读书籍和在线搜索来学习 java。

这就是我目前所拥有的。

public class Common{

Comparable [] col_1 = {1, 1, 2};
Comparable [] col_2 = {1, 1, 2,3};
Comparable [] col_3= {1, 1, 2,3,4,};
Comparable [][] collections = {col_1, col_2, col_3};
int comparisonCount = 0


public Comparable[] findCommon(Comparable [][] collections){

int i, j, k, x, y;

for(i = 0; i< col_1.length; i++){
for(j = 0; j < col_2.length; j++){
for(k = 0; k < col_3.length; k++){

comparisonCount++;
// This should be counting but is not...

if(col_1[i].compareTo(col_2[j]) == 0 && col_1[i].compareTo(col_3[k]) ==0){

//keep searching until last element & allow duplicates & add to collections or a temp[]


}
}
}
}

// Here I'm not sure how to add the elements to the collection


for (x = 0; x < collections.length; x++){
for(y = 0; y< collections[x].length; y++){
collections [x][y] = ?????? // not sure how to add results here
}
}
}


public void setComparisons(int count){
count = comparisonCount;
}



public int getComparisons(){

return comparisonCount;
}



public class Sorting {

public static void main(String[] args) {

Common m = new Common();
//I want to test it from here but I don't know how to initialize each array.

for(int x=0; x < m.collections.length; x++){
for(int y= 0; y< m.collections[x].length; y++){
System.out.println(m.collections[x][y]);
}
// what I should be getting is only (1, 1, 2) - the order is not important really. I just want to learn.

}
System.out.println(m.getComparisons());

}

最佳答案

要仅保留可比较集中的公共(public)元素,您可以使用 TreeSet,它使用传递的比较器比较元素。

此外,使用自定义比较器,您可以计算元素相互比较的次数:

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;

public class Main {
public static Comparable[] findCommon(Comparable[][] collections, Comparator comparator) {
TreeSet<Comparable> set = new TreeSet<Comparable>(comparator);
Collections.addAll(set, collections[0]);

for (int i = 1; i < collections.length; i++)
set.retainAll(Arrays.asList(collections[i]));

return set.toArray(new Comparable[set.size()]);
}

public static void main(String[] args) {
Comparable[] col_1 = {1, 1, 2};
Comparable[] col_2 = {1, 1, 2, 3};
Comparable[] col_3 = {1, 1, 2, 3, 4};

Comparable[][] collections = {col_1, col_2, col_3};
final int comparisonCount = 0;

CountingComparator comparator = new CountingComparator();
System.out.println(Arrays.toString(findCommon(collections, comparator)));
System.out.println(comparator.getComparisonCount());
}

private static class CountingComparator implements Comparator<Comparable> {
private int comparisonCount;

public int getComparisonCount() {
return comparisonCount;
}

@Override
public int compare(Comparable o1, Comparable o2) {
comparisonCount++;
return o1.compareTo(o2);
}
}
}

关于java - 如何将公共(public)元素组合到一个数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32815013/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com