java数组实现相关方法
2017-02-22 本文已影响0人
Moonsmile
import com.sun.corba.se.impl.orbutil.graph.Node;
import com.sun.corba.se.spi.presentation.rmi.IDLNameTranslator;
/**
* Created by junyi.pc on 2017/1/25.
*/
public class Main {
public int array[];
public int length;
public int max;
public Main(int max){
this.array=new int[max];
this.max=max;
}
public void insert(int value) {
if(length<max){
array[length]=value;
length++;
}
else {
}
}
public void traversal(){
System.out.print("[");
for (int i = 0; i < array.length; i++) {
System.out.print(" "+ array[i]);
}
System.out.println(" ]");
}
public void delete(int index){
if(index<0||index>=array.length){
throw new ArrayIndexOutOfBoundsException();
}
else{
for(int i=index;i<array.length;i++){
array[index]=array[index+1];
}
length--;
}
}
public int search(int value){
for (int i=0;i<array.length;i++){
if(array[i]==value){
return i;
}
}
return 0;
}
public int getMax(){
return array.length;
}
public int getValue(int index){
if(index<0||index>=array.length){
throw new ArrayIndexOutOfBoundsException();
}
else{
return array[index];
}
}
public static void main(String[] args){
Main array = new Main(30);
array.insert(1);
array.insert(2);
array.insert(3);
array.insert(4);
array.insert(5);
//显示
array.traversal();
}
}