数组
Java数组
1 public class GeneralArray { 2 3 private int[] a; 4 private int size; //数组的大小 5 private int nElem; //数组中有多少项 6 7 public GeneralArray(int max){ 8 this.a = new int[max]; 9 this.size = max; 10 this.nElem = 0; 11 } 12 13 public boolean find(int searchNum){ //查找某个值 14 int j; 15 for(j = 0; j < nElem; j++){ 16 if(a[j] == searchNum){ 17 break; 18 } 19 } 20 if(j == nElem){ 21 return false; 22 }else { 23 return true; 24 } 25 } 26 27 28 public boolean insert(int value){ //插入某个值 29 if(nElem == size){ 30 System.out.println("数组已满"); 31 return false; 32 } 33 a[nElem] = value; 34 nElem++; 35 return true; 36 } 37 38 public boolean delete(int value){ //删除某个值 39 int j; 40 for(j = 0; j < nElem; j++){ 41 if(a[j] == value){ 42 break; 43 } 44 } 45 if(j == nElem){ 46 return false; 47 } 48 if(nElem == size){ 49 for(int k = j; k < nElem - 1; k++){ 50 a[k] = a[k+1]; 51 } 52 }else { 53 for(int k = j; k < nElem; k++){ 54 a[k] = a[k+1]; 55 } 56 } 57 nElem--; 58 return true; 59 } 60 61 public void display(){ //打印整个数组 62 for(int i = 0; i < nElem; i++){ 63 System.out.println(a[i] + " "); 64 } 65 System.out.println(""); 66 } 67 } 68
--------------------------------------
勿忘初心 方得始终
--------------------------------------