0

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));
        }



    }
}
  • your List is of type ArrayList, not of your own created type – Stultuske Oct 27 '17 at 12:39
  • I don't understand what you mean. –  Oct 27 '17 at 12:40
  • `mylist` must be an instance of `MyArrayList` , it is not the case here . – Arnaud Oct 27 '17 at 12:41
  • ArrayList mylist = new ArrayList();. It's nice that you create your own class, but you are not using it. change this statement to: List mylist = new MyArrayList<>(); – Stultuske Oct 27 '17 at 12:41
  • That didn't change anything. But I do see that as an error. –  Oct 27 '17 at 12:44
  • Image of code: [link]https://image.ibb.co/hKn1RR/screenshot.png –  Oct 27 '17 at 12:52
  • that didn't provide you with what you needed, since I declared it as a List. I was just pointing out the difference between ArrayList and your class. I'm pretty sure you can figure out how to adapt it to fit your needs, and explain why my change won't gives you an error – Stultuske Oct 27 '17 at 12:53
  • Thanks, just been wrecking my brain with it. Got it now. Making silly errors. I'm going to take a break. :) Thanks. –  Oct 27 '17 at 12:55

0 Answers0