public class ArrayDemo {
private long[] arr;
//表示数据的长度
private int enElems;
public ArrayDemo(){
this.arr=new long[50];
}
public ArrayDemo(int max){
this.arr=new long[max];
}
//添加数据
public void inert(long value){
arr[enElems]=value;
enElems++;
}
//显示数据
public void display(){
System.out.print("[");
for (int i = 0; i <enElems; i++) {
System.out.print(arr[i]+",");
}
System.out.print("]");
}
//查找数据
public int seach(long value){
int i=0;
for (i = 0; i < arr.length; i++) {
if(arr[i]==value)
break;
}
if(i==enElems)
return -1;
else
return i;
}
//根据索引来查
public long get(int index){
if(index<0||index>enElems){
throw new ArrayIndexOutOfBoundsException();
}else{
return arr[index];
}
}
//删除数据
public void delete(int index) {
if(index >= enElems || index < 0) {
throw new ArrayIndexOutOfBoundsException();
} else {
for(int i = index; i < enElems; i++) {
arr[i] = arr[i + 1];
}
enElems--;
}
}
//更新数据
public void change(int index,int newvalue){
if(index >= enElems || index < 0)
throw new ArrayIndexOutOfBoundsException();
else
arr[index]=newvalue;
}
private long[] arr;
//表示数据的长度
private int enElems;
public ArrayDemo(){
this.arr=new long[50];
}
public ArrayDemo(int max){
this.arr=new long[max];
}
//添加数据
public void inert(long value){
arr[enElems]=value;
enElems++;
}
//显示数据
public void display(){
System.out.print("[");
for (int i = 0; i <enElems; i++) {
System.out.print(arr[i]+",");
}
System.out.print("]");
}
//查找数据
public int seach(long value){
int i=0;
for (i = 0; i < arr.length; i++) {
if(arr[i]==value)
break;
}
if(i==enElems)
return -1;
else
return i;
}
//根据索引来查
public long get(int index){
if(index<0||index>enElems){
throw new ArrayIndexOutOfBoundsException();
}else{
return arr[index];
}
}
//删除数据
public void delete(int index) {
if(index >= enElems || index < 0) {
throw new ArrayIndexOutOfBoundsException();
} else {
for(int i = index; i < enElems; i++) {
arr[i] = arr[i + 1];
}
enElems--;
}
}
//更新数据
public void change(int index,int newvalue){
if(index >= enElems || index < 0)
throw new ArrayIndexOutOfBoundsException();
else
arr[index]=newvalue;
}