class Array { private int data[]; private int foot; public Array(int len){ if(len>0){ this.data= new int[len]; }else { this.data= new int[1]; } } public boolean add(int num){ if(this.foot < this.data.length){ this.data[this.foot ++]= num; return true; } return false; } } public class TestArray { public static void main(String[] args) { Array arr= new Array(3); System.out.println(arr.add(100)); System.out.println(arr.add(200)); System.out.println(arr.add(300)); System.out.println(arr.add(500)); } }
输出结果:
true
true
true
false