写的方法在这里,我只是将它们的名字粘贴为简洁:
public void clear();
public boolean contains(E e);
public E get(int index);
public int indexOf(E e); //returns first E e
public boolean isEmpty();
public int lastIndexOf(E e);
public boolean remove(E e);//removes first E e in list
public E remove(int index);
public E set(int index, E e);
public int size();
public boolean addAll(MyList<? extends E> l);
因此,我创建了一个myArraylist和mylinkedlist类,以为arraylist和链接列表实现这些方法。
这是为arraylist制作的驱动程序,我应该添加一些内容以使其适用于链接列表:
public class MyArrayListDriver {
public static void main(String[] args)
{
MyArrayList<String> arrayList1 = new MyArrayList<String>(); // Constructor 1
MyArrayList<String> arrayList2 = new MyArrayList<String>(5); // Constructor 2
arrayList1.add("Monday"); // 1
arrayList1.add("Tuesday");
arrayList1.add("Thursday");
arrayList1.add("Saturday");
System.out.println(arrayList1);
arrayList1.add(2, "Wednesday"); // 2
arrayList1.add(4, "Friday");
arrayList1.add(6, "Sunday");
System.out.println(arrayList1);
System.out.println("There are " + arrayList1.size() + " days of the week."); // 3
System.out.println(arrayList1.get(5) + " is my favorite day of the week!"); // 4
////////////////////////////////////////////////////////////////////////////////////////////////////////
System.out.println("n");
arrayList2.add("Uncle John");
arrayList2.add("Tom");
arrayList2.add("Pa");
arrayList2.add("Jim Casy");
arrayList2.add("Ma");
if(arrayList2.contains("Jim Casy")) // 5
{
System.out.println(arrayList2.get(3) + " says, "Don't break up the fambly."");
}
if(arrayList2.indexOf("Jim Casy") == 1) // 6
System.out.println("One is the lonliest number.");
else
System.out.println("Not alone today...");
arrayList2.remove("Jim Casy"); // 7
System.out.println(arrayList2 + "... Goodbye Jim Casy.");
arrayList2.clear(); // 8
if(arrayList2.isEmpty()) // 9
{
System.out.println(arrayList2 + "nSee ya Joads; I hope California treats you well!");
}
////////////////////////////////////////////////////////////System.out.println("n");
MyArrayList<String> arrayList3 = new MyArrayList<String>(arrayList1); // Constructor 3
System.out.println(arrayList3 + " is the third ArrayList.");
arrayList3.remove(2); // 10
arrayList3.add(2, "Assembly Day");
System.out.println("Schedule: " + arrayList3);
arrayList3.set(5, "Standardized Test Day"); // 11
System.out.println("Revised Schedule: " + arrayList3);
for(int i = 0; i < arrayList3.size(); i++)
{
if(i % 2 == 0)
{
arrayList3.set( i, "Monday");
}}
System.out.println(arrayList3);
System.out.println(arrayList3.lastIndexOf("Monday") + " is rad..."); // 12
System.out.println("n");
arrayList2.addAll(arrayList3); // 13
System.out.println(arrayList2);
} }
再次,如何使此驱动程序也用于链接列表的ArrayList函数?
您应该从一个类(更好的接口)继承它们,让我们称之为:
public interface MyList<T>{
//your methods without implementation
}
在您的课堂中写(例如,在myArraylist上):
public class MyArrayList<T> implements MyList<T>{\implementations...}
然后在您的驾驶员中做类似:
的事情MyList<String> list;
if(//define when you want to use linked list){
list = new MyLinkedList<String>();
} else {
list = new MyArrayList<String>();
}
,然后您的list
将是链接或数组列表的实例,因此,当您在其上调用方法时,它将使用您在construction
链接到读取:继承,接口