1、数组
package chap01;
public class TestArray {
public static void main(String[] args) {
long []arr=new long[] {2,3,4};
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
}
}
2、自定义数组
package chap01;
public class MyArray {
private long[]arr;
//有效数据的长度
private int elements;
public MyArray() {
arr=new long[50];
}
public MyArray(int maxsize) {
arr=new long[maxsize];
}
//添加数据
public void insert(long value) {
arr[elements]=value;
elements++;
}
//显示数据
public void display() {
System.out.print("[");
for (int i = 0; i < elements; i++) {
System.out.print(arr[i]+" ");
}
System.out.println("]");
}
//查找数据
public int search(long value) {
int i;
for ( i = 0; i < elements; i++) {
if(value==arr[i]) {
break;
}
}
if(i==elements) {
return -1;
}
return i;
}
//查找数据
public long get(int index) {
if(index>=elements||index<0) {
throw new ArrayIndexOutOfBoundsException();
}else {
return arr[index];
}
}
//删除数据
public void delete(int index) {
if(index>=elements||index<0) {
throw new ArrayIndexOutOfBoundsException();
}else {
for (int i = index; i < elements; i++) {
arr[index]=arr[index+1];
}
elements--;
}
}
//更新数据
public void change(int index ,int newvalue) {
if(index>=elements||index<0) {
throw new ArrayIndexOutOfBoundsException();
}else {
arr[index]=newvalue;
}
}
}
测试类
package chap01;
public class TestMyArray {
public static void main(String[] args) {
MyArray arr=new MyArray();
arr.insert(13);
arr.insert(34);
arr.insert(90);
arr.display();
System.out.println(arr.search(34));
System.out.println(arr.get(2));
//arr.delete(1);
//arr.display();
arr.change(1, 55);
arr.display();
}
}
运行结果:
3、自定义有序数组
package chap01;
/**
* 有序数组跟普通数组的其它方法都一样,只是在插入元素的时候做一个操作
* @author 86187
*
*/
public class MyOrderArray {
private long[]arr;
private int elements;
public MyOrderArray() {
arr=new long[50];
}
public MyOrderArray(int maxsize) {
arr=new long[maxsize];
}
//添加数据
public void insert(long value) {
int i;
for ( i = 0; i < elements; i++) {
if(arr[i]>value) {
break;
}
}
//循环从后面开始
for (int j = elements; j >i; j--) {
arr[j]=arr[j-1];
}
arr[i]=value;
elements++;
}
//显示数据
public void display() {
System.out.print("[");
for (int i = 0; i < elements; i++) {
System.out.print(arr[i]+" ");
}
System.out.println("]");
}
//查找数据
public int search(long value) {
int i;
for ( i = 0; i < elements; i++) {
if(value==arr[i]) {
break;
}
}
if(i==elements) {
return -1;
}
return i;
}
//二分查找
public int binarysearch(long value) {
int middle=0;
int low=0;
int high=elements;
while(true) {
middle=(low+high)/2;
if(arr[middle]==value) {
return middle;
}else if(low>high){
return -1;
}else {
if(arr[middle]>value) {
high=middle-1;
}else {
low=middle+1;
}
}
}
}
//查找数据
public long get(int index) {
if(index>=elements||index<0) {
throw new ArrayIndexOutOfBoundsException();
}else {
return arr[index];
}
}
//删除数据
public void delete(int index) {
if(index>=elements||index<0) {
throw new ArrayIndexOutOfBoundsException();
}else {
for (int i = index; i < elements; i++) {
arr[index]=arr[index+1];
}
elements--;
}
}
//更新数据
public void change(int index ,int newvalue) {
if(index>=elements||index<0) {
throw new ArrayIndexOutOfBoundsException();
}else {
arr[index]=newvalue;
}
}
}
测试类
package chap01;
public class TestOrderArray {
public static void main(String[] args) {
MyOrderArray arr=new MyOrderArray();
arr.insert(40);
arr.insert(30);
arr.insert(10);
arr.insert(20);
arr.display();
System.out.println(arr.binarysearch(20));
}
}
运行结果: