課題2 プログラム

TDUList.java


import java.util.*;
public class TDUList extends AbstractSequentialList<String> {
    Node head;
    public TDUList(){
	head = new Node();
    }
    @Override  
    public ListIterator<String> listIterator(int n){
	return new TDUListIterator(n,this);
    }
    @Override
    public int size(){
	return 0;
    }
}

Node.java


class Node {
    Node next;
    String object;
    public Node(){}
}

TDUListIterator.java


import java.util.*;
public class TDUListIterator implements ListIterator<String> {
    private Node current;
    private int index;
    private TDUList list;
    TDUListIterator(int n,TDUList list){
	this.list = list;
	index=n;
	current = list.head;
	for(int i=0; i<n; i++){
	    if(current==null){
		throw new IndexOutOfBoundsException();
	    }
	    current=current.next;
	}
    }
    public void add(String object){
	System.out.println("Called add("+object+")");
    }
    public String next(){
	System.out.println("Called next()");
	return "";
    }
    public boolean hasNext(){
	return false;
    }
    public int nextIndex(){
	// return index+1;//訂正 (12/26/2008)
	return index;
    }
    public void set(String object){
	throw new UnsupportedOperationException();
    }
    public void remove(){
	throw new UnsupportedOperationException();
    }
    public int previousIndex(){
	return -1;
    }
    public String previous(){
	throw new NoSuchElementException();
    }
    public boolean hasPrevious(){
	return false;
    }
}

テストプログラム

テスト1

訂正しました(2008/12/4)


import java.util.*;
class Test1 {
    public static void main(String[] arg){
	//LinkedList<String> tdu = new LinkedList<String>();
	TDUList tdu = new TDUList();
	System.out.println(tdu.size());
	tdu.add("def");
	System.out.println(tdu.size());
	tdu.add("abc");
	System.out.println(tdu.size());
	tdu.add("ghi");
	System.out.println(tdu.size());
	ListIterator<String> it = tdu.listIterator();
	it.add("jkl"); // 追加 12/26/2008
	it.add("mno"); // 追加 12/26/2008
	while(it.hasNext()){
	    System.out.println(it.nextIndex());
	    System.out.println(it.next());
	}
	for(String str : tdu){
	    System.out.println(str);
	}
    }
}

テスト2


import java.util.*;
class Test2 {
    public static void main(String[] arg){
	TDUList tdu = new TDUList();
	System.out.println(tdu.size());
	tdu.add("def");
	System.out.println(tdu.size());
	tdu.add("abc");
	System.out.println(tdu.size());
	tdu.add("ghi");
	System.out.println(tdu.size());
	for(String str : tdu){
	    System.out.println(str);
	}
	Collections.sort(tdu);
	for(String str : tdu){
	    System.out.println(str);
	}
    }
}

テスト3


import java.util.*;
class Test3 {
    private static <E extends Comparable<? super E>>
			      void testTDU(List<E> list){
	System.out.println(list.size());
	for(E value : list){
	    System.out.println(value);
	}
	Collections.sort(list);
	for(E value : list){
	    System.out.println(value);
	}
    }
    public static void main(String[] arg){
	TDUList<String> tdustr = new TDUList<String>();
	tdustr.add("def");
	tdustr.add("abc");
	tdustr.add("ghi");
	testTDU(tdustr);

	TDUList<Integer> tduint = new TDUList<Integer>();
	tduint.add(3);
	tduint.add(1);
	tduint.add(2);
	testTDU(tduint);
    }
}