import java.util.ArrayList;
import java.util.Collections;
class MyArrayList <T> extends ArrayList <T> {
// T[]a should receive both integer array and string array
public MyArrayList (T[] a)
{ super();
// This is the problem
// it does't work
//I'm trying to use a new object to add all of the elements in the input array to the MyArrayList object.
MyArrayList <T> list = new MyArrayList <T>();
for (int i=0;i<a.length;i++)
list.add(a[i]);
}
public static void print()
{
// Display array elements
// I don't work it out
System.out.println("");
}
}
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();
}
}
我知道方法ArrayList<String> list = new ArrayList<>(Arrays.asList(array))
,但我不确定如何在继承中使用它,或者我应该在代码中使用for循环来存储?