我在代码中遇到了错误。
这是我得到的错误:
线程“main”中的异常java.lang.Error:未解决的编译问题:
recur(Integer[])
类型中的方法D_and_con
不适用于参数(int[]
)at edu.uqu.algorithms.inversions.D_and_con.recur(D_and_con.java:27)
在edu.uqu.algorithms.inversions.MainTest.main(MainTest.java:27)
代码计算反转次数。
是:
package edu.uqu.algorithms.inversions;
import java.io.FileNotFoundException;
import java.util.Arrays;
import edu.uqu.algorithms.inversions.util.IOUtil;
public class MainTest {
public static void main(String[] args) {
try {
/*//Inversions using BRUTE FORCE
Integer[] tokens1 = IOUtil.loadFileIntoArray("IntegerArray.txt");
long startTime1 = System.currentTimeMillis();
System.out.println("Started Computing Total nb of invertions BRUTE FORCE..........................");
System.out.println("Total nb of invertions BRUTE FORCE: " + Inversions.countInvertionsBruteForce(tokens1));
long runningTime1 = (System.currentTimeMillis() - startTime1);
System.out.println("BRUTE FORCE Running time: " + runningTime1);
System.out.println("\n");*/
//Inversions using DIVIDE & CONQUER
Integer[] tokens2 = IOUtil.loadFileIntoArray("IntegerArray.txt");
long startTime2 = System.currentTimeMillis();
System.out.println("Started Computing Total nb of invertions DIVIDE & CONQUER..........................");
System.out.println("MMMMM" + D_and_con.recur(tokens2) );
long runningTime2 = (System.currentTimeMillis() - startTime2);
System.out.println("DIVIDE & CONQUER Running time: " + runningTime2);
System.out.println("----------------------- FINISHED -------------------------");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
----------------------------------------
/************************************
*
* The aim of this code is to count the number of inversions in
* an array of integers. Two ways of counting are used, a Brute Force algorithm and
* a recursive Divide and Conquer algorithm.
*
***********************************/
package edu.uqu.algorithms.inversions;
/**
public class Inversions{
/**
* Brute force inversions counting method.
*/
public static long countInvertionsBruteForce(Integer[] entries_p)
{
long result = 0;
for(int i = 0; i < entries_p.length; i++){
for(int j = i+1; j < entries_p.length; j++){
if(entries_p[i] > entries_p[j]) result++;
}
//System.out.println("BRUTE FORCE intermediate result for i = " + i + " IS: " + result);
}
return result;
}
}
--------------------------
Divide and conquer
/************************************
*
* The aim of this code is to count the number of inversions in
* an array of integers. Two ways of counting are used, a Brute Force algorithm and
* a recursive Divide and Conquer algorithm.
*
***********************************/
package edu.uqu.algorithms.inversions;
import java.math.BigDecimal;
public class D_and_con{
private static BigDecimal totalcount = new BigDecimal(0);
public static Integer[] recur(Integer[] entries_p)
{
int n = entries_p.length;
if(n == 1)
{
return entries_p;
}
int middel = n/2;
int[] Larray = new int[middel];
int[] Rarray = new int[n - middel];
System.arraycopy(entries_p , 0 , Larray, 0 , Larray.length );
System.arraycopy(entries_p , Larray.length , Rarray , 0 , Rarray.length);
recur(Larray);\\ERROR APPEAR HERE
recur(Rarray);\\ERROR APPEAR HERE
comb(Larray , Rarray , entries_p );
return entries_p;
}
private static void comb(int[] Larray, int[] Rarray, Integer[] newarray)
{
int LarrayL = Larray.length;
int RarrayL = Rarray.length;
int i=0 , j=0 , k=0 ;
while(i< LarrayL && i<RarrayL)
{
if(Larray[i] < Rarray[i] )
{
newarray[k] = Larray[i];
i++;
}
else
{
newarray[k] = Rarray[j];
i++;
totalcount = totalcount.add (new BigDecimal(Larray.length - 1));
}
k++;
}
while(i < LarrayL) {
newarray[k] = Larray[i];
i++;
k++;
}
while(j < RarrayL) {
newarray[k] = Rarray[j];
j++;
k++;
}
}
}
答案 0 :(得分:1)
编译器抱怨您使用recur
类型的某些内容提供int[]
方法,但您声明它接受Integer[]
。因此,请查看方法签名
// entries_p is Integer[], not int[]
public static Integer[] recur(Integer[] entries_p)
但您可以使用此处显示的int[]
提供方法
int[] Larray = new int[middel];
int[] Rarray = new int[n - middel];
...
recur(Larray);
recur(Rarray);
您需要自行转换类型。请注意,与 以下是一些简单的转换,首先不使用Streams: 现在使用 您可以调整方法或参数,而不是将数组从一个转换为另一个类型。例如,您可以从 到 然后它将接受Integer
与int
不同。虽然Java可以自动将两者相互转换(装箱),但对于高级类型喜欢数组Integer[]
和int[]
不会那样。< / p>
转换
Integer[]
相比,int[]
能够存储null
。// from int[] to Integer[]
int[] source = ...
Integer[] target = new Integer[source.length];
for (int i = 0; i < source.length; i++) {
// Convert int to Integer
target[i] = Integer.valueOf(source[i]);
}
// from Integer[] to int[]
Integer[] source = ...
int[] target = new int[source.length];
for (int i = 0; i < source.length; i++) {
if (source[i] == null) {
// Don't support null values
throw IllegalArgumentException();
}
// Convert Integer to int
target[i] = source[i].intValue();
}
Stream
s(Java 8):// from int[] to Integer[]
int[] source = ...
Integer[] target = Arrays.stream() // IntStream
.boxed() // Stream<Integer>
.toArray(Integer[]::new)
// from Integer[] to int[]
Integer[] source = ...
int[] target = Arrays.stream() // Stream<Integer>
.mapToInt(Integer::intValue) // Stream<Integer>
.toArray(int[]::new)
更改方法或参数
public static Integer[] recur(Integer[] entries_p)
public static Integer[] recur(int[] entries_p)
int[]
作为参数。您也可以将返回类型更改为int[]
。如上所述,另一种选择是将参数从int[]
更改为Integer[]
。这适用于该代码部分:// You may change both to Integer[]
int[] Larray = new int[middel];
int[] Rarray = new int[n - middel];
答案 1 :(得分:0)
将您的Integer[]
数组更改为int[]
数组!
答案 2 :(得分:0)
getChildren
是原始数据类型,"int"
是"Integer"
。
解决您的问题: -
Class