N
N
nen0y2018-05-22 15:46:54
Java
nen0y, 2018-05-22 15:46:54

How to call a method from another class?

public class BubbleSort implements Sort{
    
    
    public void bubbleSort(int[] unsortedArray){
        
        int count = 0;
        
        do{
            count = 0;
            for(int  i = 0; i < unsortedArray.length - 1; i++){
                if(unsortedArray[i] > unsortedArray[i + 1]){
                    int tmp = unsortedArray[i];
                    unsortedArray[i] = unsortedArray[i + 1];
                    unsortedArray[i+1] = tmp;
                    count++;
                }
            }
        }while(count > 0);
        System.out.println(Arrays.toString(unsortedArray));
    }

}

I'm trying to call in main
public class Main {

    public static void main(String[] args){
        
        int[] unsortedArray = {2, 16, 5, 1, 8, 21, 4};
        
        Sort bubblesort = new BubbleSort(unsortedArray);
        
    }
    
}

Gives an error message "The constructor BubbleSort(int[]) is undefined

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
netrox, 2018-05-22
@nen0y

You are passing an array to the constructor.

public class Main {

    public static void main(String[] args){
        
        int[] unsortedArray = {2, 16, 5, 1, 8, 21, 4};
        
         BubbleSort instance =new BubbleSort();
         instance.bubbleSort(unsortedArray );
        
    }
    
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question