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;
} else {
return false;
}
}
public int[] getData() {
return this.data;
}
}
class Reverse extends Array {
public Reverse(int len) {
super(len);
}
public int[] getData() {
int count = super.getData().length / 2;
int head = 0;
int tail = super.getData().length - 1;
for (int i = 0; i < count; i++) {
int temp = super.getData()[head];
super.getData()[head] = super.getData()[tail];
super.getData()[tail] = temp;
head++;
tail--;
}
return super.getData();
}
}
public class ArraysDemo02 {
public static void main(String[] args) {
Reverse re = new Reverse(3);
re.add(90);
re.add(80);
re.add(70);
int[] temp = re.getData();
for (int i = 0; i < temp.length; i++) {
System.out.println(temp[i]);
}
}
}
Java 综合实战:数组操作(开发反转类)
最新推荐文章于 2022-03-13 21:59:04 发布