Implementation of "Java Data Structure" ArraySet Collection

package cn.com.Inf;

import java.util.Iterator;


//interface
/*................Gist
 * The underlying implementation of the collection is implemented with an array
 * The collection is not limited by the length of the array, and the storage space will be automatically increased when the space is insufficient
 * Set collection cannot have duplicate elements
 * Set collection is non-linear
 * */
public interface SetADT<T> {
	public void add(T element);//Add element
	public void addAll(SetADT<T> set);//Add elements from another set to your own set
	public T remove(T element);//Remove element
	public T removeRandom();//Remove randomly
	public boolean contains(T target);//Determine whether there are duplicate elements
	public boolean equals(SetADT<T> set);//Determine whether the elements in the two sets are equal
	public boolean isEmpty();//Determine whether there is an element in the collection
	public SetADT<T> union(SetADT<T> set);//Merge the two sets together
	public int size();//The size of the collection
	public Iterator<T> iterator();//Iterators need to traverse the collection
	public String toString();//Turn the elements in the collection into strings for display
}
package cn.com.MyArrayList;

import java.util.EmptyStackException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Random;

import cn.com.Inf.SetADT;

public class MyArrayList<T> implements SetADT<T>{
	private T[] content;//Define the array
	private int count;//Define an index, equivalent to C, C++ pointer.
	private int initCapacity = 100;//Array initialization length 100
	Random r= new Random();//Random function, which needs to be used in the random deletion method
	
	  public MyArrayList(){
		count = 0;
		content = (T[])(new Object[initCapacity]);//Initialize an array of 100
	}

	@Override
	public void add(T element) {
		// TODO Auto-generated method stub
		if(!contains(element)){ //Determine whether there are duplicates////* (required)
			if(this.size() == content.length){
				expandContent();//The array space is insufficient to call the function to expand the space	
			}	
			content[count] = element;
			count++;//Add to the array
		}
		
	}

	private void expandContent() {
		// TODO Auto-generated method stub
		// increase the array space
		T[] target = (T[])(new Object[content.length*2]);
		for(int i = 0 ; i < content.length ; i++){
			target[i] = content[i];
		}
		target = content;
	}

	@Override
	public T remove(T element) {
		// TODO Auto-generated method stub
		boolean bool = false;
		int search = -1;
		if(isEmpty()){
			throw new EmptyStackException();//Determine whether the array is empty
		}
		for(int i = 0 ; i < content.length; i++){
			if(content[i].equals(element)){
				search = i;
				break;
			}
		}
		if(search == -1){
				throw  new NoSuchElementException();
		}
		//Replace the last element with the element currently to be deleted, and delete the last element by count--
		T result = content[search];
		content[search] = content[count-1];
		content[count-1] = null;
		count--;
		return result;
	}

	@Override
	public T removeRandom() {
		// TODO Auto-generated method stub
		if(isEmpty()){
			throw new EmptyStackException();
		}
		//Randomly delete elemnt with subscript 0-count-1
			int choice = r.nextInt(count);
			T result = content[choice];
			content[choice] = content[count-1];
			content[count-1] = null;
			count--;
			return result;
	}
	public boolean contains(T target) {
		// TODO Auto-generated method stub
		// loop through the array
		boolean bool =false;
		for(int i = 0; i < count; i++){
			if(content[i].equals(target)){
				bool = true;
			}
		}
		return bool;
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return(count==0);
	}
	
	@Override
	public int size() {
		// TODO Auto-generated method stub
		return count;
	}

	
	@Override
	public Iterator<T> iterator() {
		// TODO Auto-generated method stub
		// instantiate
		return new ArrayIterator<T>(content,count);
	}

	@Override
	public boolean equals(SetADT<T> set) {
		// TODO Auto-generated method stub
		boolean result = false;
		//define two sets
		MyArrayList<T> temp1 = new MyArrayList<T>();
		MyArrayList<T> temp2 = new MyArrayList<T>();
		 //Check if the set lengths are equal
		 T obj;
		 if(this.size() == set.size()){
			 temp1.addAll(this);
			 temp2.addAll(set);
			 Iterator<T> scan = set.iterator();
			 while(scan.hasNext()){
				 obj = scan.next();
				 if(temp1.contains(obj)){
					 temp1.remove(obj);
					 temp2.remove(obj);
				 }
			 }
			 // delete one by one
			 result = ((temp1.isEmpty())&&(temp1.isEmpty()));
		 }
		 return result;
	}


	@Override
	public SetADT<T> union(SetADT<T> set) {
		// TODO Auto-generated method stub
		// merge the two sets
		MyArrayList<T> both = new MyArrayList<T>();
		for(int i = 0 ; i < count ; i++){
			both.add(content[i]);
			}
			Iterator<T> scan = set.iterator();
			while(scan.hasNext()){
			both.add(scan.next());
		}
			return both;
	}

	
	
	public void addAll(SetADT<T> set) {
		// TODO Auto-generated method stub
		// Traverse one by one and add to the collection
		Iterator<T> i = set.iterator();
		while(i.hasNext()){
			add(i.next());
		}
	}
	public String toString(){
		// convert element to string
		String result = "";
		for(int i = 0 ; i < count; i++){
			result = result + content[i].toString()+ "\n";
		}
		return result;
		
	};

	
}
package cn.com.MyArrayList;

import java.nio.channels.UnsupportedAddressTypeException;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class ArrayIterator<T> implements Iterator<T> {
	private T[] item;
	private int count;
	private int current;
	
	public ArrayIterator(T[] collection,int size){
		//initialize an array
		this.item = collection;
		this.count = size;
		current = 0;
	}
	@Override
	public boolean hasNext() {
		// TODO Auto-generated method stub
		// Determine if there is a next
		return (current < count);
	}

	@Override
	public T next() {
		// TODO Auto-generated method stub
		T result = null;
		if(hasNext()){
			result = item[current];
			current++;
		}else{
			throw new NoSuchElementException();
		}
		return result;
	}

	@Override
	public void remove() {
		// TODO Auto-generated method stub
		throw new UnsupportedOperationException();
	}

}


 
 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325929282&siteId=291194637