6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MyList@Java

Posted at
package Homework;
import java.util.Collection;
import java.util.*;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class MyList<E> implements List<E> {
	private int nowIndex = 0; //Count an elements of array.
	E[] array; //create a default size array.
	MyList(int numElement) {
		array = (E[]) new Object[numElement];
	}
	MyList() {
		this(10);
	}
	
	public boolean isEmpty() {
		boolean flag = true;
		for(int r = 0; r < array.length; r++) {
			if(array[r] != null) flag = false;
		}
		return(flag);
	}
	
	public int size() {
		return(nowIndex);
	}
	
	public boolean add(E e) {
		E[] temp = (E[]) new Object[nowIndex + 1];
		for(E elm : array) {
			if(elm == e) return(false);
		}
		if(nowIndex == array.length) {
			for(int r = 0; r < array.length; r++) {
				temp[r] = array[r];
			}
			temp[nowIndex++] = e;
			array = temp;
			temp = null;
		} else {
			array[nowIndex++] = e;
			temp = null;
		}
		return(true);
	}
	
	public void add(int index, E element) {
		E[] temp = (E[]) new Object[nowIndex + 1];
		if(array[index] != null) {
			for(int r = nowIndex - 1; r >= index; r--)
				array[r + 1] = array[r];
			if(nowIndex == array.length) {
				for(int r = 0; r < array.length; r++) {
					temp[r] = array[r];
				}
				temp[index] = element;
				array = temp;
				temp = null;
				nowIndex++;
			} else {
				array[nowIndex++] = element;
				temp = null;
			}
		} else {
			array[index] = element;
		}
	}
	
	public boolean addAll(Collection<? extends E> c) {
		return(false);
	}
	
	public boolean addAll(int index, Collection<? extends E> c) {
		return(false);
	}
	
	public void clear() {
		for(int r = 0; r < array.length; r++) {
			array[r] = null;
		}
	}
	
	public boolean contains(Object o) {
		return(false);
	}
	
	public boolean containsAll(Collection<?> c) {
		return(false);
	}
	
	public boolean equals(Object o) {
		return(false);
	}
	
	public int hashCode() {
		return(-1);
	}
	
	public int indexOf(Object o) {
		int idx = -1;
		for(int r = 0; r < nowIndex; r++) {
			if(array[r] == o) {
				idx = r;
				break;
			}
		}
		return(idx);
	}
	
	public Iterator<E> iterator() {
		return(null);
	}
	
	public int lastIndexOf(Object o) {
		int idx = -1;
		for(int r = 0; r < nowIndex; r++) {
			if(array[r] == o) idx = r;
		}
		return(idx);
	}
	
	public ListIterator<E> listIterator() {
		return(null);
	}
	
	public ListIterator<E> listIterator(int index) {
		return(null);
	}
	
	public E get(int index) {
		return(array[index]);
	}
	public E remove(int index) {
		E temp = array[index]; //Retension
		array[index] = null;
		for(int r = index + 1; r < nowIndex - 1; r++) {
			array[r] = array[r + 1];
		}
		nowIndex--;
		return(temp);
	}
	public boolean remove(Object o) {
		int idx = -1;
		boolean flag = false;
		for(int r = 0; r < nowIndex; r++) {
			if(array[r] == o) {
				idx = r;
				flag = true;
				break;
			}
		}
		
		if(flag) {
			this.remove(idx);
			nowIndex--;
		}
		return(flag);
	}
	
	public boolean removeAll(Collection<?> c) {
		return(false);
	}
	
	public boolean retainAll(Collection<?> c) {
		return(false);
	}
	
	public E set(int index, E elem) {
		E temp = array[index];
		array[index] = null;
		array[index] = elem;
		return(temp);
	}
	
	public E[] subList(int fromIndex, int toIndex) {
		List<E> temp = new MyList<E>();
		if(fromIndex == toIndex) {
			//temp = null;
			return());
		}
		for(int r = fromIndex; r < toIndex; r++) {
			temp.add(array[r]);
		}
		temp = (MyList<E>)array;
		
		//temp = null;
		return();
	}
	
	public Object[] toArray() {
		return(null);
	}
	
	public <T> T[] toArray(T[] a) {
		return(null);
	}
	/*
	void sort() {
		if(this.size() < 2) return; //配列のサイズが0、もしくは1であれば何もしない
		
		MyList<E> left = (MyList<E>)this.subList(0,  this.size() / 2 - 1);//対象配列を2分割した内の左部分のみの配列を取り出す
		MyList<E> right = (MyList<E>)this.subList(this.size() / 2, this.size() - 1);//対象配列を2分割した内の右部分のみの配列を取り出す
		left.sort();//左側に対して、更に右側と左側に分割する
		right.sort();//右側に対して、更に右側と左側に分割する
		this.clear(); //自分自身のリストは既にleftとrightに分割されたので、メモリのために一旦クリアする
		
		while(true) {//無限ループ
			MyList<E> target = left.get(0).compareTo(right.get(0)) < 0 ? left : right; //leftの左端とrightの左端を比較し、leftの方が小さければleftを、そうでなければrightをtargetに参照させる
			this.add(target.get(0));//targetの左端を対象配列に追加    
			target.remove(0);//targetの左端を削除
			if(target.size() == 0) break; //もし、このブロック内の上記処理によってtargetのサイズが0であったら抜け出す
		}
		
		MyList<E> leftover = left.size() > 0 ? left : right; //leftの要素がまだ残っていればleftoverに参照させ、そうでなければrightを参照させる
		while(left.size() > 0) { //leftの要素がある限り
			this.add(leftover.get(0)); //対象配列にleftoverの左端を追加
			leftover.remove(0);//leftoverの左端を削除
		}
		return; //エラー回避のため返却
	}
	*/
	//�K�v�s�Œ��ȃ��\�b�h
	//isEmpty()�Asize()�Aadd(E e)�Aadd(int index, E element)�A
	//indexOf(Object o)�AlastIndexOf(Object o)�Aget(int index)�A
	//remove(Object o)�Aremove(int index)�Aset(int index E elem)
	public static void main(String[] args) /*throws NullPointerException*/ {
		//TurtleFrame f = new TurtleFrame();
		//MyList<Integer> a = new MyList<Integer>();
		/*
		for(int r = 0; r < 10; r++) {
			a.add((int)Math.random() * r);
		}
		*/
		//MyList<Turtle> t = new MyList<Turtle>();
		/*
		System.out.println("�v�f���O�?H�F" + a.isEmpty());
		for(int r = 0; r < 12; r++) {
			a.add(r);                //1,2,3,4,5,6,7,8,9,10,11�̌v�P�Q�v�f���i�[�i�Q��z����g������j
		}
		if(!a.isEmpty()) {
			System.out.println("size = " + a.size());
			System.out.println("�Y���R�̗v�f�F" + a.get(3));
			System.out.println("a.get(3)�̓Y���F" + a.indexOf(a.get(3)));
		} else {
			System.out.println("array \"a\" has been empty");
		}
		a.set(6, 3);
		System.out.println("�Y���R�̗v�f�i�Ō�ɏo��)" + a.lastIndexOf(3));
		System.out.println("�Y���R�̗v�f�i�ŏ��ɏo��)" + a.indexOf(3));
		a.remove(3);
		if(a.get(3) == null) {
			System.out.println("Element of array 3rd has already removed.");
		} else {
			System.out.println("Element of array 3rd is exists.");
		}
		a.add(3, 10);
		System.out.println("�V���ɂP�O����܂����F" + a.get(3));
		t.add(new Turtle(200, 200, 0));
		f.add(t.get(0));
		t.get(0).fd(100);
		t.remove(t.get(0)); //Delete a turtle.
		System.out.println(t.isEmpty());
		
		try {
			f.add(t.get(0)); //error
		} catch(NullPointerException e) {
			System.out.println("NullPointerException(�^�[�g����remove������ŎQ�Ƃ�������).");
			System.out.println("�`�悳�ꂽ�^�[�g���́A�^�[�g����remove���g�p���Ă��Ȃ��̂ŏ����Ȃ�");
		}
		*/
		//a.sort();
		
			List<Integer> list = new MyList<Integer>();
			List<Integer> right = new MyList<Integer>();
			list.add(5);
			list.add(10);
			list.add(6);
			right = list.subList(1, list.size() - 1);
			for(int i = 0; i < right.size(); i++){
				System.out.println("right要素:" + right.get(i));
			}
			right.add(2123);
			System.out.println("改行");
			for(int i = 0; i < list.size(); i++){
				System.out.println("list要素:" + list.get(i));
			}
	}
}
6
6
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?