增强前面的数组

2017-11-29 16:10:16来源:oschina作者:Favour人点击

分享
第七城市th7cn
package com.jomster.数组拓展;
import javafx.beans.binding.ObjectExpression;
import java.util.Arrays;
import java.util.stream.Stream;
public class ArrayExpand> {
private Object[] arrayExpand;
private int elements;
private int expandSpace = 50;
public ArrayExpand() {
this(50);
}
public ArrayExpand(int initialCapacity) {
this.arrayExpand = new Object[initialCapacity];
}
/**
* 线性添加数据
*
* @param t
*/
public void add(T t) {
if (elements + 1 == arrayExpand.length) {
} else {
arrayExpand[elements] = t;
elements++;
}
}
/**
* 获取该坐标的数据
*
* @param index
* @return
*/
public T get(int index) {
if (index >= elements || index < 0) {
throw new ArrayIndexOutOfBoundsException("你获取的坐标超限");
}
return (T) arrayExpand[index];
}
/**
* 打印数据
*/
public void print() {
Stream is = Arrays.stream(arrayExpand).filter(n -> n != null);
is.forEach(i -> System.out.println("arr value = " + i));
}
/**
* 查询数据坐标(线性查询)
*/
public int search(T t) {
for (int i = 0; i < elements; i++) {
if (t == (T) arrayExpand[0]) {
return i;
}
}
return -1;
}
/**
* 删除数据
*
* @param index
*/
public void delete(int index) {
if (index >= elements || index < 0) {
throw new ArrayIndexOutOfBoundsException("你获取的坐标超限");
}
for (int i = index; i < elements - 1; i++) {
arrayExpand[i] = arrayExpand[i + 1];
}
elements--;
arrayExpand[elements] = null;
}
/**
* 修改数据
*
* @param index
* @param t
*/
public void change(int index, T t) {
if (index >= elements || index < 0) {
throw new ArrayIndexOutOfBoundsException("你获取的坐标超限");
} else {
arrayExpand[index] = t;
}
}
/**
* 有序数组,从小到大
*
* @param t
*/
public void addSort(T t) {
if (elements + 1 == arrayExpand.length) {
} else {
if (elements == 0) {
arrayExpand[elements] = t;
elements++;
} else {
int i;
for (i = 0; i < elements; i++) {
if (t.compareTo((T) arrayExpand[i]) < 0) {
break;
}
}
for (int j = elements; j > i; j--) {
arrayExpand[j] = arrayExpand[j - 1];
}
arrayExpand[i] = t;
elements++;
}
}
}
/**
* 二分法查找
* @param t
* @return
*/
public int binarySearch(T t) {
int middle = 0;
int pow = elements - 1;
int low = 0;
while (true) {
middle = (pow + low) / 2;
if (t.compareTo((T) arrayExpand[middle]) == 0) {
return middle;
} else if (low > pow) {
return -1;
} else {
if (t.compareTo((T) arrayExpand[middle]) > 0) {
low = middle + 1;
} else {
pow = middle - 1;
}
}
}
}
/**
* 冒泡排序
*/
public void bubbleSort(){
for(int i=0;i for(int j = i;j < elements -1;j++){
if((int)arrayExpand[j]>(int)arrayExpand[j+1]){
Object temp = arrayExpand[j];
arrayExpand[j] = arrayExpand[j+1];
arrayExpand[j+1] = temp;
}
}
}
}
/**
* 选择排序
*/
public void selectSort(){
int k = 0;
Object temp;
for(int i=0;i k = i;
for(int j = i;j < elements;j++){
if((int)arrayExpand[k]>(int)arrayExpand[j]){
k = j;
}
}
temp = arrayExpand[i];
arrayExpand[i] = arrayExpand[k];
arrayExpand[k] = temp;
}
}
/**
* 插入排序
*/
public void insertSort(){
int i,j;
Object insertNote;
for (i=1;i {
insertNote = arrayExpand[i];
j=i-1;
while(j>=0&&(int)insertNote<(int)arrayExpand[j])
{
arrayExpand[j+1]=arrayExpand[j];
j--;
}
arrayExpand[j+1]=insertNote;
}
}
}

性能:


第七城市th7cn

微信扫一扫

第七城市微信公众平台