Reputation: 31
I created my own MyArrayList but it can not be sorted. A normal Arraylist can be sorted by Collections.sort
but mine can not. Could some one tell me where is wrong?
import java.util.*;
public class TestMyArrayList
{
public static void main(String[] args){
String[] subjects = {"EIE320", "EIE558", "EIE375", "EIE424"};
Integer[] marks = {90,85,70,80};
MyArrayList<String> sList = new MyArrayList<String>(subjects);
Collections.sort(sList);
sList.print();
MyArrayList<Integer> mList = new MyArrayList<Integer>(marks);
Collections.sort(mList);
mList.print();
}
}
public class MyArrayList<E> extends ArrayList<E>
implements List<E>
{
private List<E> data;
public MyArrayList()
{
}
public MyArrayList(E[] inputdata){
super();
data = new ArrayList<E>();
for(E e:inputdata){
data.add(e);
}
}
public void print(){
for(Iterator iter = data.iterator();iter.hasNext();){
System.out.print(iter.next() + " ");
}
System.out.println(" ");
}
}
Upvotes: 3
Views: 285
Reputation: 40438
Just a heads up that you can use Arrays.asList()
instead of doing what you are doing here.
String[] subjects = {"EIE320", "EIE558", "EIE375", "EIE424"};
ArrayList<String> sList = new ArrayList<String>(Arrays.asList(subjects));
If you are doing this as an excercise that's fine, but I would usually avoid re-writing Collections
classes if at all possible.
Upvotes: 0
Reputation: 5336
If you see the source code of Collections.java
you will see that Collections.sort
calls the sort
method defined in the concrete list which in this case is 'MyArrayList.java'. You have not provided this method so your class inherits this from the super class ArrayList.java
Now if you see the source code of ArrayList.java
you will find that sort
method defined in it does the process of sorting on the Object[] elementData;
. You have not set your data in elementData
(defined in ArrayList.java
).
You can use public ArrayList(Collection<? extends E> c)
constructor from your public MyArrayList(E[] inputdata)
. You will have to transform the inputdata
and then instead of calling super()
, you should call the constructor public ArrayList(Collection<? extends E> c)
.
As a suggestion, also pointed by answer given by Eran, you can use composition, in you class you can have a reference to ArrayList and then can use it for addition, deletion and do sorting on it (define a method sort and within it call Collections.sort on the object of ArrayList you have).
Upvotes: 0
Reputation: 394146
The constructor of MyArrayList
is wrong. Since you are extending ArrayList
, you shouldn't create another ArrayList
instance in the MyArrayList
constructor. Just add the elements to this
ArrayList
.
When you call Collections.sort()
on your MyArrayList
instance, it doesn't sort the data
member List
.
public MyArrayList(E[] inputdata){
super();
for(E e:inputdata){
super.add(e);
}
}
Your data
member is not needed. You would only need such a member if you use composition instead of inheritance, in which case your class wouldn't extend ArrayList
.
You should also modify your print
method :
public void print(){
for(Iterator iter = iterator();iter.hasNext();){
System.out.print(iter.next() + " ");
System.out.println(" ");
}
Upvotes: 5