[英]The method (double[]) in the type main is not applicable for the arguments (int[])
I am trying to find a median of an array in Java. 我试图找到Java中数组的中位数。
I am getting the exception: 我得到了例外:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 线程“主”中的异常java.lang.Error:未解决的编译问题:
The method median(double[]) in the type tez3 is not applicable for the arguments (int[]) tez3类型的中位数(double [])方法不适用于参数(int [])
at tez3.main(tez3.java:33) 在tez3.main(tez3.java:33)
My code is below. 我的代码如下。 What is the problem? 问题是什么? How can i print the double function ? 如何打印双重功能?
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
public class tez3 {
public static void main(String args[]) throws java.io.IOException {
Scanner s = new Scanner(new File("C:\\tny\\Deneme1.txt"));
int[] numberList = new int[10];
int i = 0;
int count = 0;
int result = 0;
while (s.hasNextInt()) {
numberList[i++] = s.nextInt();
}
for (i = 0; i < numberList.length; i++) {
System.out.println(+(i + 1) + ".Value: " + numberList[i]);
}
for (i = 0; i < numberList.length; i++) {
count++;
}
for (i = 0; i < numberList.length; i++) {
result += numberList[i];
}
System.out.println("Average of the Values is: " + result / count);
System.out.println("Mode of the Values is: " + mode(numberList));
System.out.println("Median of the Values is: " + median(numberList));
}
public static int mode(int numberList[]) {
int maxValue = 0, maxCount = 0;
for (int i = 0; i < numberList.length; ++i) {
int count = 0;
for (int j = 0; j < numberList.length; ++j) {
if (numberList[j] == numberList[i]) {
++count;
}
}
if (count > maxCount) {
maxCount = count;
maxValue = numberList[i];
}
}
return maxValue;
}
public double median(double[] numberList) {
int factor = numberList.length - 1;
double[] first = new double[(double) factor / 2];
double[] last = new double[first.length];
double[] middleNumbers = new double[1];
for (int i = 0; i < first.length; i++) {
first[i] = numbersList[i];
}
for (int i = numberList.length; i > last.length; i--) {
last[i] = numbersList[i];
}
for (int i = 0; i <= numberList.length; i++) {
if (numberList[i] != first[i] || numberList[i] != last[i]) {
middleNumbers[i] = numberList[i];
}
}
if (numberList.length % 2 == 0) {
double total = middleNumbers[0] + middleNumbers[1];
return total / 2;
} else {
return middleNumbers[0];
}
}
}
The problem is exactly what the error message says. 问题恰恰是错误消息中所说的。 Your method median() takes a double[] as parameter. 您的方法mean()采用double []作为参数。
But when you're trying to call it you give it numberList as a parameter. 但是,当您尝试调用它时,可以给它numberList作为参数。 But numberList is an int[], that is to say it is not a double[]. 但是numberList是一个int [],也就是说它不是double []。
The easiest fix is to modify median() so that the parameter it takes is int[] rather than double[]. 最简单的解决方法是修改mid(),使它采用的参数是int []而不是double []。
The problem is that you want to cast all elements in you int[] list to a double. 问题是您要将int []列表中的所有元素都强制转换为双精度。 If you don't want to change the parameters of median to int[] then my preferred way to deal with this is to overload the method and convert the arguments 如果您不想将中位数的参数更改为int [],那么我处理此问题的首选方法是重载方法并转换参数
So, it would be something like: 因此,它将类似于:
public double median(int[] numberList) {
double[] doubleList = new double[numberList.size()];
for(int i=0; i<doubleList.size(); i++){
doubleList[i] = (double)numberList[i];
}
return median(doubleList);
}
If you did change the parameter of your original 'median' method, then you would just cast the numberList element each time you called it, ie 如果确实更改了原始“ median”方法的参数,则每次调用它时都将强制转换numberList元素,即
first[i] = (double) numbersList[i];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.