package array;
/**
* 1) æ°ç»çæå
¥ãå é¤ãæç
§ä¸æ éæºè®¿é®æä½ï¼
* 2ï¼æ°ç»ä¸çæ°æ®æ¯intç±»åçï¼
*
* Author: Zheng
* modify: xing
*/
public class Array {
//å®ä¹æ´åæ°æ®dataä¿åæ°æ®
public int data[];
//å®ä¹æ°ç»é¿åº¦
private int n;
//å®ä¹ä¸å®é
个æ°
private int count;
//æé æ¹æ³ï¼å®ä¹æ°ç»å¤§å°
public Array(int capacity){
this.data = new int[capacity];
this.n = capacity;
this.count=0;//ä¸å¼å§ä¸ä¸ªæ°é½æ²¡æåæä»¥ä¸º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 == count && count == 0) {
// data[index] = value;
// ++count;
// return true;
//}
// æ°ç»ç©ºé´å·²æ»¡
if (count == n) {
System.out.println("没æå¯æå
¥çä½ç½®");
return false;
}
// 妿countè¿æ²¡æ»¡ï¼é£ä¹å°±å¯ä»¥æå
¥æ°æ®å°æ°ç»ä¸
// ä½ç½®ä¸åæ³
if (index < 0||index > count ) {
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