R
Red Orchid
Occasionally I think that the design of 'ArrayList' is not good.
Because ...
The access modifier of 'E[] elementData' in 'ArrayList'
is 'private'. That is, Java do not allow a programmer
to access 'E[] elementData'.
Therefore, he will write the following statements to
sort 'ArrayList'.
<example>
List<String> strList = new ArrayList<String>();
Collections.sort(strList); // <- #1
</example>
By the way, the source of #1 is as follows.
<Quote>
public static <T extends Comparable<? super T>> void sort(List<T> list) {
Object[] a = list.toArray(); // <- #2
Arrays.sort(a); // <- #3
ListIterator<T> i = list.listIterator();
for (int j=0; j<a.length; j++) { // <- #4
i.next();
i.set((T)a[j]);
}
}
</Quote>
If 'strList' is large array, #3 is merge sort.
Merge sort requires 2 * M when M is the memory
size of 'strList'.
Because the memory size of 'a' in #2 is M,
'Collections.sort' requires 3 * M and has to execute #4.
It seems inefficient. (note that 'strList' is large array)
If the access modifier of 'E[] elementData' is 'protected',
he can sort 'strList' with 2 * M and without #4.
What is your comment ?
Thanks.
Because ...
The access modifier of 'E[] elementData' in 'ArrayList'
is 'private'. That is, Java do not allow a programmer
to access 'E[] elementData'.
Therefore, he will write the following statements to
sort 'ArrayList'.
<example>
List<String> strList = new ArrayList<String>();
Collections.sort(strList); // <- #1
</example>
By the way, the source of #1 is as follows.
<Quote>
public static <T extends Comparable<? super T>> void sort(List<T> list) {
Object[] a = list.toArray(); // <- #2
Arrays.sort(a); // <- #3
ListIterator<T> i = list.listIterator();
for (int j=0; j<a.length; j++) { // <- #4
i.next();
i.set((T)a[j]);
}
}
</Quote>
If 'strList' is large array, #3 is merge sort.
Merge sort requires 2 * M when M is the memory
size of 'strList'.
Because the memory size of 'a' in #2 is M,
'Collections.sort' requires 3 * M and has to execute #4.
It seems inefficient. (note that 'strList' is large array)
If the access modifier of 'E[] elementData' is 'protected',
he can sort 'strList' with 2 * M and without #4.
What is your comment ?
Thanks.