1.整型数组
1)数组的插入、删除、按照下标随机访问操作
2)数组中的数据时Int类型的
package array;
/**
* 1)数组的插入、删除、按照下标随机访问操作
* 2)数组中的数据时Int类型的
*
*/
public class Array {
//定义保存整型数据data的数组
private int[] data;
//定义数组的长度
private int n;
//定义数组中元素的个数
private int count;
//构造方法,定义数组大小
public Array(int capacity){
this.data=new int[capacity];
this.n=capacity;
this.count=0;//一开始一个数据都没有
}
//根据索引,找到数据中的元素并返回
public int find(int index){
if(index<0 || index>=count)
return -1;
return data[index];
}
//插入元素:头部插入,尾部插入
public boolean insert(int index,int value){
//位置不合法
if(index<0 || index>count){
System.out.println("插入位置不合法");
return false;
}
//数组为空
// if(index==count && count==0){
// data[index]=value;
// ++count;
// return true;
// }
//数组空间已满
if(count==n){
System.out.println("空间已满");
return false;
}
//数组空间未满,位置合法
//将插入位置之后的数据向后搬移一位
for(int i=count;i>index;--i){
data[i]=data[i-1];
}
data[index]=value;
++count;
return true;
}
//根据索引,删除数组中元素
public boolean delete(int index){
if(index<0 || index>=count) return false;
//位置合法
//将删除位置之后的元素向前移动一位
for(int i=index+1;i<count;++i){
data[i-1]=data[i];
}
--count;
return true;
}
public void printAll(){
for(int i=0;i<count;++i){
System.out.print(data[i]+" ");
}
System.out.println();
}
public static void main(String[] args) {
Array array=new Array(5);
array.printAll();
array.insert(0, 3);
array.insert(0, 4);
array.insert(1, 5);
array.insert(3, 8);
array.insert(3, 10);
array.delete(3);
array.insert(3, 11);
array.printAll();
}
}
2.不确定类型数组GenericArray
package array;
public class GenericArray<T> {
private T[] data;
private int size;
// 根据传入容量,构造Array
@SuppressWarnings("unchecked")
public GenericArray(int capacity) {
data = (T[]) new Object[capacity];
size = 0;
}
// 无参构造方法,默认数组容量为10
public GenericArray() {
this(10);
}
// 获取数组容量
public int getCapacity() {
return data.length;
}
// 获取当前元素个数
public int count() {
return size;
}
// 判断数组是否为空
public boolean isEmpty() {
return size == 0;
}
// 修改 index 位置的元素
public void set(int index, T e) {
checkIndex(index);
data[index] = e;
}
// 获取对应 index 位置的元素
public T get(int index) {
checkIndex(index);
return data[index];
}
// 查看数组是否包含元素e
public boolean contains(T e) {
for (int i = 0; i < size; i++) {
if (data[i].equals(e)) {
return true;
}
}
return false;
}
// 获取对应元素的下标, 未找到,返回 -1
public int find(T e) {
for ( int i = 0; i < size; i++) {
if (data[i].equals(e)) {
return i;
}
}
return -1;
}
// 在 index 位置,插入元素e, 时间复杂度 O(m+n)
public void add(int index, T e) {
checkIndex(index);
// 如果当前元素个数等于数组容量,则将数组扩容为原来的2倍
if (size == data.length) {
resize(2 * data.length);
}
for (int i = size - 1; i >= index; i--) {
data[i + 1] = data[i];
}
data[index] = e;
size++;
}
// 向数组头插入元素
public void addFirst(T e) {
add(0, e);
}
// 向数组尾插入元素
public void addLast(T e) {
add(size, e);
}
// 删除 index 位置的元素,并返回
public T remove(int index) {
checkIndexForRemove(index);
T ret = data[index];
for (int i = index + 1; i < size; i++) {
data[i - 1] = data[i];
}
size --;
data[size] = null;
// 缩容
if (size == data.length / 4 && data.length / 2 != 0) {
resize(data.length / 2);
}
return ret;
}
// 删除第一个元素
public T removeFirst() {
return remove(0);
}
// 删除末尾元素
public T removeLast() {
return remove(size - 1);
}
// 从数组中删除指定元素
public void removeElement(T e) {
int index = find(e);
if (index != -1) {
remove(index);
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(String.format("Array size = %d, capacity = %d \n", size, data.length));
builder.append('[');
for (int i = 0; i < size; i++) {
builder.append(data[i]);
if (i != size - 1) {
builder.append(", ");
}
}
builder.append(']');
return builder.toString();
}
// 扩容方法,时间复杂度 O(n)
private void resize(int capacity) {
T[] newData = (T[]) new Object[capacity];
for (int i = 0; i < size; i++) {
newData[i] = data[i];
}
data = newData;
}
private void checkIndex(int index) {
if (index < 0 || index > size) {
throw new IllegalArgumentException("Add failed! Require index >=0 and index <= size.");
}
}
private void checkIndexForRemove(int index) {
if(index < 0 || index >= size) {
throw new IllegalArgumentException("remove failed! Require index >=0 and index < size.");
}
}
}
3.基于数组实现LRU缓存
package linkedlist;
import java.util.HashMap;
import java.util.Map;
/**
* Created by SpecialYang in 2018/12/7 2:00 PM.
*
* 基于数组实现的LRU缓存
* 1. 空间复杂度为O(n)
* 2. 时间复杂度为O(n)
* 3. 不支持null的缓存
*/
public class LRUBasedArray<T> {
private static final int DEFAULT_CAPACITY = (1 << 3);
private int capacity;
private int count;
private T[] value;
private Map<T, Integer> holder;
public LRUBasedArray() {
this(DEFAULT_CAPACITY);
}
public LRUBasedArray(int capacity) {
this.capacity = capacity;
value = (T[]) new Object[capacity];
count = 0;
holder = new HashMap<T, Integer>(capacity);
}
/**
* 模拟访问某个值
* @param object
*/
public void offer(T object) {
if (object == null) {
throw new IllegalArgumentException("该缓存容器不支持null!");
}
Integer index = holder.get(object);
if (index == null) {
if (isFull()) {
removeAndCache(object);
} else {
cache(object, count);
}
} else {
update(index);
}
}
/**
* 若缓存中有指定的值,则更新位置
* @param end
*/
public void update(int end) {
T target = value[end];
rightShift(end);
value[0] = target;
holder.put(target, 0);
}
/**
* 缓存数据到头部,但要先右移
* @param object
* @param end 数组右移的边界
*/
public void cache(T object, int end) {
rightShift(end);
value[0] = object;
holder.put(object, 0);
count++;
}
/**
* 缓存满的情况,踢出后,再缓存到数组头部
* @param object
*/
public void removeAndCache(T object) {
T key = value[--count];
holder.remove(key);
cache(object, count);
}
/**
* end左边的数据统一右移一位
* @param end
*/
private void rightShift(int end) {
for (int i = end - 1; i >= 0; i--) {
value[i + 1] = value[i];
holder.put(value[i], i + 1);
}
}
public boolean isContain(T object) {
return holder.containsKey(object);
}
public boolean isEmpty() {
return count == 0;
}
public boolean isFull() {
return count == capacity;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; i++) {
sb.append(value[i]);
sb.append(" ");
}
return sb.toString();
}
static class TestLRUBasedArray {
public static void main(String[] args) {
testDefaultConstructor();
testSpecifiedConstructor(4);
// testWithException();
}
private static void testWithException() {
LRUBasedArray<Integer> lru = new LRUBasedArray<Integer>();
lru.offer(null);
}
public static void testDefaultConstructor() {
System.out.println("======无参测试========");
LRUBasedArray<Integer> lru = new LRUBasedArray<Integer>();
lru.offer(1);
lru.offer(2);
lru.offer(3);
lru.offer(4);
lru.offer(5);
System.out.println(lru);
lru.offer(6);
lru.offer(7);
lru.offer(8);
lru.offer(9);
System.out.println(lru);
}
public static void testSpecifiedConstructor(int capacity) {
System.out.println("======有参测试========");
LRUBasedArray<Integer> lru = new LRUBasedArray<Integer>(capacity);
lru.offer(1);
System.out.println(lru);
lru.offer(2);
System.out.println(lru);
lru.offer(3);
System.out.println(lru);
lru.offer(4);
System.out.println(lru);
lru.offer(2);
System.out.println(lru);
lru.offer(4);
System.out.println(lru);
lru.offer(7);
System.out.println(lru);
lru.offer(1);
System.out.println(lru);
lru.offer(2);
System.out.println(lru);
}
}
}