之前通过B站了解到了几个GitHub网站,其中一个较好的是:
- http://github.com/trending
自己常常在GitHub上进行浏览,其中一个开源项目叫JAVA
,主要是讲Java
相关算法的。链接地址。自己学习了几个算法,接下来简单介绍一下。
Bag
实现了Bag
类,实现add和contains
方法,在类中具体Element
使用的Node
来实现存储,具体看代码,在代码中Bag
类实现了Iterable
接口,可以实现迭代访问;从add
方法中可以知道,添加元素是从头部开始添加的,首先建立一个oldfirst
指针指向原有的链表,之后建立firstElement
,让firstElement
指向oldfirst
实现从头部添加元素(其中firstElement
只是一个指针);在contains
中,利用了迭代器实现链表的遍历,其中利用this.iterator()
指向当前对象。
public class Bag<Element> implements Iterable<Element> {
private Node<Element> firstElement; // first element of the bag
private int size; // size of bag
//结点
private static class Node<Element> {
private Element content;
private Node<Element> nextElement;
}
/** Create an empty bag */
public Bag() {
firstElement = null;
size = 0;
}
/** @return true if this bag is empty, false otherwise */
public boolean isEmpty() {
return firstElement == null;
}
/** @return the number of elements */
public int size() {
return size;
}
/** @param element - the element to add */
public void add(Element element) {
Node<Element> oldfirst = firstElement;
firstElement = new Node<>();
firstElement.content = element;
firstElement.nextElement = oldfirst;
size++;
}
/**
* Checks if the bag contains a specific element
*
* @param element which you want to look for
* @return true if bag contains element, otherwise false
*/
public boolean contains(Element element) {
Iterator<Element> iterator = this.iterator();
while (iterator.hasNext()) {
if (iterator.next().equals(element)) {
return true;
}
}
return false;
}
DynamicArray
实现一个动态数组,可实现添加、扩容、删除、取值;定义了三个属性,容量、当前容量、元素数组;扩容直接对容量扩大两倍。
public class DynamicArray<E> implements Iterable<E> {
private int capacity;
private int size;
private Object[] elements;
/**
* constructor
*
* @param capacity the starting length of the desired array
*/
public DynamicArray(final int capacity) {
this.size = 0;
this.capacity = capacity;
this.elements = new Object[this.capacity];
}
/** No-args constructor */
public DynamicArray() {
this.size = 0;
this.capacity = 10;
this.elements = new Object[this.capacity];
}
/**
* Doubles the capacity of the array
*
* @return int the new capacity of the array
*/
public int newCapacity() {
this.capacity *= 2;
// changed from this.capacity <<= 1; now much easier to understand
return this.capacity;
}
/**
* Adds an element to the array If full, creates a copy array twice the size of the current one
*
* @param element the element of type <E> to be added to the array
*/
public void add(final E element) {
if (this.size == this.elements.length) {
this.elements = Arrays.copyOf(this.elements, newCapacity());
}
this.elements[this.size] = element;
size++;
}
/**
* Places element of type <E> at the desired index
*
* @param index the index for the element to be placed
* @param element the element to be inserted
*/
public void put(final int index, E element) {
this.elements[index] = element;
}
/**
* get method for element at a given index returns null if the index is empty
*
* @param index the desired index of the element
* @return <E> the element at the specified index
*/
public E get(final int index) {
return getElement(index);
}
/**
* Removes an element from the array
*
* @param index the index of the element to be removed
* @return <E> the element removed
*/
public E remove(final int index) {
final E oldElement = getElement(index);
fastRemove(this.elements, index);
return oldElement;
}
/**
* get method for size field
*
* @return int size
*/
public int getSize() {
return this.size;
}
/**
* isEmpty helper method
*
* @return boolean true if the array contains no elements, false otherwise
*/
public boolean isEmpty() {
return this.size == 0;
}
public Stream<E> stream() {
return StreamSupport.stream(spliterator(), false);
}
private void fastRemove(final Object[] elements, final int index) {
final int newSize = this.size - 1;
if (newSize > index) {
System.arraycopy(elements, index + 1, elements, index, newSize - index);
}
elements[this.size = newSize] = null;
}
private E getElement(final int index) {
return (E) this.elements[index];
}
}
添加元素时,首先需要判断此时数组是否已到容量极限,如果已满容量极限,则对原有的元素进行复制,利用到了Arrays.copyOf()
,方法。
public void add(final E element) {
if (this.size == this.elements.length) {
this.elements = Arrays.copyOf(this.elements, newCapacity());
}
this.elements[this.size] = element;
size++;
}
删除的时候,首先通过下标找出元素,之后对该元素移除。
其中找下标时,是对数组进行遍历;删除是用到了System.arraycopy(源数组,起始下标,目标数组,起始下标,复制的长度)
public E remove(final int index) {
final E oldElement = getElement(index);
fastRemove(this.elements, index);
return oldElement;
}
private E getElement(final int index) {
return (E) this.elements[index];
}
private void fastRemove(final Object[] elements, final int index) {
final int newSize = this.size - 1;
if (newSize > index) {
System.arraycopy(elements, index + 1, elements, index, newSize - index);
}
elements[this.size = newSize] = null;
}