I have this problem occuring in a small portion of my JAVA program and its essen
ID: 3711321 • Letter: I
Question
I have this problem occuring in a small portion of my JAVA program and its essentially a sorting method. Can someone help fix these errors or understand how I can fix these. Thanks in advance
Sorts a list of Persons using merge sort * @param list public static void mergeSort (ArrayList list) /Provide this code if (list.length > 1) // Merge sort the first half int[] firstHalfnew int[list.length 2]; System.arraycopy (list, 0, firstHalf, 0, list.length /2); mergeSort (firstHalf); // Merge sort the second half int secondHalfLength-list.length - list.length / 2; intl secondHalf-new int secondHalfLength: System.arraycopy (list, list.length / 2, secondHalf, 0, secondHalfLength); mergeSort (secondHalf); // Merge firstHalf with secondHalf into list merge (firstHalf, secondHalf, list):Explanation / Answer
in mergeSort()
The issue here is your using ArrayList but your trying to use it as array
1 ) change length to size();
2) convert first array into arrayList
List<Person> list = Arrays.asList(firstHalf);
same for second half also
in merge()
your having ArrayList but your accessing elements like array with index
list1[current1] --- list.get(current1);
list2[current2] --- list.get(current2);
temp[current3] --- temp.add(current3,list1.get(current1));
length--size()
Note: please comment here if you face any issues or else copy page the code so that I can make changes accordingly