UPDATE: It's still not working with:
List<Integer> mylist = new MyArrayList<>();
I'm having an issue with calling a class.
In the code, the mergeSort() is underline red and has the following error:
Cannot find symbol
symbol: method mergeSort(int,int)
location: variable mylist of type ArrayList<Integer>
I have tried declaring the ArrayList in the main file as an Object, or ElemType, but it doesn't fix the issue at all. Potentially, I think it could be an error with NetBeans.
I have also tried converting the Integer ArrayList to an Int ArrayList, i.e. myList[] = (values)
The main Java file is:
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);
}
}
And the classes file is:
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));
}
}
}