We need to Define a Tools class which contains the following static generic methods: a. parallelSort which takes one parameter of type ArrayList and returns a sorted version of the parameter list without modifying it:
Divide the list into two parts and make a copy of each
Start two threads by making instances of SortingThread where each thread sorts one of the copies obtained in the previous step
Merge the results generated by the two threads
I am Getting this error:
Exception in thread "main" java.lang.ArrayStoreException:
arraycopy: source type java.util.ArrayList is not an array
at
java.base/java.lang.System.arraycopy(Native Method)
this is my code:
import java.util.ArrayList;
public class Tools {
public static <T> ArrayList<T> parallelSort(ArrayList<T> arr){
//Obtaining the first Half and making a copy of it
ArrayList<T> FirstHalf = new ArrayList<T>(arr.size()/2);
System.arraycopy(arr, 0, FirstHalf, 0, arr.size()/2);
//Obtaining the second Half and making a copy of it
ArrayList<T> SecondHalf = new ArrayList<T>(arr.size()- (arr.size()/2));
System.arraycopy(arr, arr.size()/2, SecondHalf, 0, arr.size()- (arr.size()/2));
//Creating Thread
SortingThread<T> thread1 = new SortingThread<T>();
//Start Thread
thread1.start(); //Creating Thread parallelSort
//Creating Thread
SortingThread<T> thread2 = new SortingThread<T>();
//Start Thread
thread2.start();
return arr;
}
public static <T> boolean areEqual(ArrayList<T> arr1, ArrayList<T> arr2) {
return arr1.equals(arr2);
}
public static void main(String[] args) {
//Testing on list of complex numbers
Complex c1= new Complex(3,4);
Complex c2= new Complex(2,2);
Complex c3= new Complex(1,2);
ArrayList<Complex> a1= new ArrayList<Complex>();
a1.add(c1);
a1.add(c2);
a1.add(c3);
System.out.println("The Original two lists:");
System.out.println(a1);
System.out.print(Tools.parallelSort(a1));
}
}
CodePudding user response:
The exception states it clearly. Java is a strongly types language. So you can't use array list in a place of array . Obviously arraycopy() method only supports array type not Lists.
CodePudding user response:
The java.lang.System#arrayCopy
method accepts an array type as its first and third arguments.
You should transform your input ArrayList
to and array before copying its items using the #toArray
method:
ArrayList<T> FirstHalf = new ArrayList<T>(arr.size()/2)
System.arraycopy(arr.toArray(), 0, FirstHalf.toArray(), 0, arr.size()/2);
Meanwhile, this would leave your FirstHalf
list useless, as:
- the
#toArray
method returns a array that is not referenced by any means from the originalArrayList
- the destination array will be off index when accessed as it holds no element
I recommend then that you review your implementation to either use arrays from entry to result (transforming the latter to an ArrayList
) at the end or use another approach such as:
ArrayList<T> FirstHalf = new ArrayList<T>(arr.size()/2);
Object [] temp = new Object[arr.size()/2];
System.arraycopy(arr.toArray(), 0, temp, 0, arr.size()/2);
FirstHalf.addAll(Arrays.asList(temp));