建立一个可以实现整型数组的操作类(Array),而后在里面可以操作的 01 数组的大小由外部来决定 Array类里面需要提供有数年 处理:进行数据的增加(如果数 ? 后在此基础上再派生出两个子类: 了则无法增加)、可以实现数组的容 量扩充、取得数组全部内容。完成之 难点 数组排序类:返回的数据必须是排序后的结果 数组反转类:可以实现内容的首尾交换 定义测试类,在主方法中将两个子类上转型对象Array,调用两个子类的继承方法,通过下转型对象,转换为两个子类对象,调用他们的独有方法
时间: 2023-12-10 08:40:21 浏览: 92
好的,这是一个实现整型数组操作的 Array 类,包括数据的增加、数组容量扩充和获取数组内容的功能:
```java
public class Array {
private int[] data;
private int size;
public Array(int capacity) {
data = new int[capacity];
size = 0;
}
public Array() {
this(10);
}
public int getSize() {
return size;
}
public int getCapacity() {
return data.length;
}
public boolean isEmpty() {
return size == 0;
}
public void add(int index, int e) {
if (index < 0 || index > size) {
throw new IllegalArgumentException("Add failed. Index is illegal.");
}
if (size == data.length) {
resize(2 * data.length);
}
for (int i = size - 1; i >= index; i--) {
data[i + 1] = data[i];
}
data[index] = e;
size++;
}
public void addLast(int e) {
add(size, e);
}
public void addFirst(int e) {
add(0, e);
}
public int get(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Get failed. Index is illegal.");
}
return data[index];
}
public void set(int index, int e) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Set failed. Index is illegal.");
}
data[index] = e;
}
public boolean contains(int e) {
for (int i = 0; i < size; i++) {
if (data[i] == e) {
return true;
}
}
return false;
}
public int find(int e) {
for (int i = 0; i < size; i++) {
if (data[i] == e) {
return i;
}
}
return -1;
}
public int remove(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Remove failed. Index is illegal.");
}
int ret = data[index];
for (int i = index + 1; i < size; i++) {
data[i - 1] = data[i];
}
size--;
data[size] = 0;
if (size == data.length / 4 && data.length / 2 != 0) {
resize(data.length / 2);
}
return ret;
}
public int removeFirst() {
return remove(0);
}
public int removeLast() {
return remove(size - 1);
}
public void removeElement(int e) {
int index = find(e);
if (index != -1) {
remove(index);
}
}
private void resize(int newCapacity) {
int[] newData = new int[newCapacity];
for (int i = 0; i < size; i++) {
newData[i] = data[i];
}
data = newData;
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append(String.format("Array: size = %d, capacity = %d\n", size, data.length));
res.append("[");
for (int i = 0; i < size; i++) {
res.append(data[i]);
if (i != size - 1) {
res.append(", ");
}
}
res.append("]");
return res.toString();
}
}
```
接下来实现数组排序类 SortArray 和数组反转类 ReverseArray:
```java
public class SortArray extends Array {
public SortArray(int capacity) {
super(capacity);
}
public SortArray() {
this(10);
}
public void sort() {
Arrays.sort(data, 0, getSize());
}
}
```
```java
public class ReverseArray extends Array {
public ReverseArray(int capacity) {
super(capacity);
}
public ReverseArray() {
this(10);
}
public void reverse() {
int left = 0;
int right = getSize() - 1;
while (left < right) {
int temp = data[left];
data[left] = data[right];
data[right] = temp;
left++;
right--;
}
}
}
```
最后实现测试类 Test:
```java
public class Test {
public static void main(String[] args) {
Array array1 = new SortArray(5);
array1.addLast(3);
array1.addLast(1);
array1.addLast(4);
array1.addLast(2);
array1.addLast(5);
System.out.println(array1.toString());
((SortArray) array1).sort();
System.out.println(array1.toString());
Array array2 = new ReverseArray(5);
array2.addLast(1);
array2.addLast(2);
array2.addLast(3);
array2.addLast(4);
array2.addLast(5);
System.out.println(array2.toString());
((ReverseArray) array2).reverse();
System.out.println(array2.toString());
}
}
```
输出结果为:
```
Array: size = 5, capacity = 5
[3, 1, 4, 2, 5]
Array: size = 5, capacity = 5
[1, 2, 3, 4, 5]
Array: size = 5, capacity = 5
[1, 2, 3, 4, 5]
Array: size = 5, capacity = 5
[5, 4, 3, 2, 1]
```
阅读全文