定义整数数组操作类
package com.kaishengit;
/**
* @author Wgs
* @version 1.0
* @create:2018/05/02 定义整数数组操作类
*/
public class Array {
// 控制数组脚标
private int foot = 0;
// 保存的数据
private int data[];
// 由外部传入大小
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 int[] getData() {
return this.data;
}
// 数组增长
public void increment(int num) {
int[] newArr = new int[this.data.length + num];
System.arraycopy(this.data, 0, newArr, 0, this.data.length);
// 改变引用
this.data = newArr;
}
}
class TestDemo {
public static void main(String[] args) {
Array array = new Array(5);
System.out.println(array.add(8)+",");
System.out.println(array.add(12)+",");
System.out.println(array.add(2)+",");
System.out.println(array.add(5)+",");
System.out.println(array.add(3)+",");
System.out.println(array.add(9)+",");
array.increment(3);
System.out.println(array.add(9)+",");
System.out.println(array.add(0)+",");
System.out.println(array.add(7)+",");
System.out.println(array.add(5)+",");
int result[] = array.getData();
for (int r : result){
System.out.println(r);
}
}
}