在JTable模型上计算模式

时间:2017-03-18 02:02:38

标签: java swing jtable

我没有处理JTables的经验,我怎样才能让这段代码计算出我的某个专栏的模式?

public static int mode(int[] array) {
    int mode = array[0];
    int maxCount = 0;
    for (int i = 0; i < array.length; i++) {
        int value = array[i];
        int count = 0;
        for (int j = 0; j < array.length; j++) {
            if (array[j] == value) count++;
            if (count > maxCount) {
                mode = value;
                maxCount = count;
            }
        }
    }
    if (maxCount > 1) {
        return mode;
    }
    return 0;
}

1 个答案:

答案 0 :(得分:1)

考虑到代码的逻辑,我想,你应该将public static int mode(int[] array) { int mode = array[0]; int maxCount = 0; for (int i = 0; i < array.length; i++) { int value = array[i]; int count = 0; for (int j = 0; j < array.length; j++) { if (array[j] == value) count++; } if (count > maxCount) { mode = value; maxCount = count; } } if (maxCount > 1) { return mode; } return 0; } 条件置于内部for循环之外,如下所示。

SELECT e.ename, SUM(md.prices) AS TotalSales
FROM Employee2 e inner join MakesD2 md on md.eid=e.eid
GROUP BY e.ename
UNION ALL
SELECT e.ename, 0 As  TotalSales
FROM Employee2 e left join MakesD2 md on md.eid=e.eid
WHERE md.eid is NULL;

虽然我不确定你究竟在寻找什么!