Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 基于数组值的计算_Java_Arrays - Fatal编程技术网

Java 基于数组值的计算

Java 基于数组值的计算,java,arrays,Java,Arrays,我有两个数组 int[] count = {7, 17, 704, 17, 704, 7, 1} int[] number = {2, 8, 8, 600, 600, 1200, 30056} 其中代表这些数据: +------+-----------+ | Item | Frequency | +------+-----------+ | 2 | 7 | | 8 | 17 | | 8 | 704

我有两个数组

int[] count = {7,   17, 704,    17, 704,    7,  1}
int[] number = {2,  8,  8,  600,    600,    1200,   30056}
其中代表这些数据:

+------+-----------+
| Item | Frequency |
+------+-----------+
|   2  |     7     |
|   8  |     17    |
|   8  |    704    |
|  600 |     17    |
|  600 |    704    |
| 1200 |     7     |
| 30056|     1     |
+------+-----------+
总数为

1457
如何得到这样的东西

Percentage of 2 is 0.5% /* 7/1457 */
Percentage of 8 is 49.5% /*(17+704)/1457*/
Etc ...
试用

for (int i = 0; i < count.size(); i++) {
                // calculate freq percentage
                double percent =count.get(i)*100.0/total;
                String temp = df.format(percent);

            }
试图搜索一些,但似乎无法找到正确的关键字来搜索所需的答案。

public double-get(int-match){
public double get(int match){
    int numSum = 0; // numerators
    int denSum = 0; // denominators
    assert count.length == number.length; // assert that they are of the same size. Strange things may occur if this assumption is false.
    for(int i = 0; i < count.length; i++){
        denSum += count[i]; // add the count to denominator
        if(number[i] == match){
            numSum += count[i]; // add to numerator if relative `number` matches
        }
    }
    return numSum * 100d / denSum; // convert to percentage
}
int numSum=0;//分子 int denSum=0;//分母 assert count.length==number.length;//断言它们的大小相同。如果这个假设是错误的,可能会发生奇怪的事情。 for(int i=0;i
HashMap map=newhashmap();
int totalCnt=0;
对于(int j=0;j
导入java.text.DecimalFormat;
导入java.text.NumberFormat;
公开课考试{
静态int[]计数={7,17,704,17,704,7,1};
静态int[]number={2,8,8,600600120030056};
静态整数和=0;
公共静态void main(字符串[]args){
NumberFormat格式化程序=新的十进制格式(“0.00%”);
for(int i:计数){
sum=sum+i;
}
int TEMPNAME=数字[0];
int tempCount=0;

对于(int j=0;j2的百分比为什么是7/1547?这只是一个统计公式。它意味着数字2总共显示了7次1457times@Pemapmodder1547似乎是第一个数组中的和,7-项对应于2(都在索引0处)。第二个数组-值,第一个数组-出现计数。或者获取要查找的元素的索引(例如,值8为1和2)然后使用它们查找另一个数组中的值,或者更好地创建包含count和number的对象并对其进行运算。然后,您可以查看下一个值,如果相同,则将其相加。整数数学不会给您一个浮点数,这意味着在百分比计算中没有小数。此外,为什么是浮点数而不是双精度?哎呀,忘记了整数除法的事情。只是浮点数在double:P之前出现在我的脑海里,没有太大的区别,对吗?这与更新的问题中特别指出的问题是一样的:数字
8
将被列出两次(分开),而不是合并(并集).我看到了这个问题并更新了我的解决方案。使用Hashmap可以让您对所有数字进行联合计数
public double get(int match){
    int numSum = 0; // numerators
    int denSum = 0; // denominators
    assert count.length == number.length; // assert that they are of the same size. Strange things may occur if this assumption is false.
    for(int i = 0; i < count.length; i++){
        denSum += count[i]; // add the count to denominator
        if(number[i] == match){
            numSum += count[i]; // add to numerator if relative `number` matches
        }
    }
    return numSum * 100d / denSum; // convert to percentage
}
HashMap<Integer, Integer> map = new HashMap<>();
    int totalCnt = 0;
    for (int j = 0; j < number.length; j++) {
        totalCnt += count[j];
        if(map.containsKey(number[j]))
            map.put(number[j], map.get(number[j]) + count[j]);
        else 
            map.put(number[j], count[j]);
    }

    for (Entry<Integer, Integer> i : map.entrySet()) {
        double fraction = (double)i.getValue()/totalCnt;
        System.out.println("Percentage of " + i.getKey() + " is " + fraction*100);
    }
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Test {

   static int[] count = {7,   17, 704,    17, 704,    7,  1};
   static int[] number = {2,  8,  8,  600,    600,    1200,   30056};
   static int  sum = 0;

   public static void main(String[]args){
      NumberFormat formatter = new DecimalFormat("#0.00%"); 
      for(int i : count){
        sum = sum+i;
      }
      int tempNumber = number[0];
      int tempCount = 0;

      for (int j=0;j<count.length;j++){

        if(number[j]!=tempNumber){
            System.out.println("Percentage of "+tempNumber+ " is " + formatter.format((double)tempCount/sum ));             
            tempCount = count[j];
            tempNumber = number[j];

            if(j==count.length-1){
               System.out.println("Percentage of "+tempNumber+ " is " + formatter.format((double)tempCount/sum ));  
            }             
        }
        else{              
          tempCount +=count[j];              
        }

    }
}

}