更新:它仍未使用:
List<Integer> mylist = new MyArrayList<>();
我在调用课程时遇到了问题。
在代码中,mergeSort()下划线为红色,并出现以下错误:
Cannot find symbol
symbol: method mergeSort(int,int)
location: variable mylist of type ArrayList<Integer>
我尝试将主文件中的ArrayList声明为Object或ElemType,但它根本没有解决问题。可能,我认为NetBeans可能是一个错误。
我也尝试将Integer ArrayList转换为Int ArrayList,即myList [] =(values)
主要的Java文件是:
package testermergesort;
import java.util.*;
public class TesterMergeSort {
public static void main(String[] args) {
ArrayList<Integer> mylist = new ArrayList<Integer>();
for(int i = 0; i < 100; i++)
{
int randomNum = (int) (Math.random()*1000);
mylist.add(new Integer(randomNum));
}
//System.out.println(mylist.toString());
int start = 0;
int end = mylist.size() -1;
mylist.mergeSort(start,end);
}
}
类文件是:
package testermergesort;
import java.util.*;
public class MyArrayList<ElemType> extends ArrayList<ElemType>
{
public void mergeSort(int start, int end)
{
// use array,start,end if static only
if (start < end){
int mid = (start+end)/2;
mergeSort(start, mid);
mergeSort(mid+1, end);
merge(start, mid, end);
}
else{
return;
}
}
public void merge (int left, int middle, int right)
{
ArrayList<Object> temp_array = new ArrayList<Object>();
int i = left;
int j = middle + 1;
int k = 0;
int pos;
Comparable ElemAtI = (Comparable)get(i);
Comparable ElemAtJ = (Comparable)get(j);
Comparable ElemAtK = (Comparable)get(k);
// Comparable temp_I = (Comparable)temp_array.get(i);
// Comparable temp_J = (Comparable)temp_array.get(j);
// Comparable temp_K = (Comparable)temp_array.get(k);
while(i <= middle && j <= right)
{
if(ElemAtI.compareTo(ElemAtJ) < 0)
{
temp_array.add(k,ElemAtI);
i = i+1;
}
else{
temp_array.add(k,ElemAtJ);
j = j+1;
}
k = k+1;
}
while(i <= middle)
{
temp_array.add(k,ElemAtI);
k = k+1;
i = i+1;
}
while(j <= right)
{
temp_array.add(k,ElemAtJ);
k = k+1;
j = j+1;
}
for(i = left, k = 0; i <= right; i++, k++){
set(i,(ElemType)temp_array.get(k));
}
}
}