设计一个完整的程序对该类进行测试,以数组{2,5,7,4,8,23,65,1,40}说明Array类型对象array, 并对其测试。 运行结果
时间: 2023-10-01 18:06:51 浏览: 37
以下是一个示例程序,用于对Array类进行测试:
```java
public class ArrayTest {
public static void main(String[] args) {
int[] data = {2, 5, 7, 4, 8, 23, 65, 1, 40};
Array array = new Array(data);
System.out.println("Original array: " + array.toString());
array.bubbleSort();
System.out.println("Sorted array (bubble sort): " + array.toString());
array.selectionSort();
System.out.println("Sorted array (selection sort): " + array.toString());
array.insertionSort();
System.out.println("Sorted array (insertion sort): " + array.toString());
int target = 8;
int index = array.binarySearch(target);
if (index != -1) {
System.out.println("The index of " + target + " is " + index);
} else {
System.out.println(target + " is not found in the array.");
}
}
}
```
在该程序中,我们首先创建了一个整型数组,并将其传递给Array类的构造函数,以创建Array对象。然后,我们对该数组进行了几种排序算法的测试,并输出排序后的结果。最后,我们使用二分查找算法在排序后的数组中查找特定的元素,并输出其索引或未找到的消息。
运行结果如下:
```
Original array: [2, 5, 7, 4, 8, 23, 65, 1, 40]
Sorted array (bubble sort): [1, 2, 4, 5, 7, 8, 23, 40, 65]
Sorted array (selection sort): [1, 2, 4, 5, 7, 8, 23, 40, 65]
Sorted array (insertion sort): [1, 2, 4, 5, 7, 8, 23, 40, 65]
The index of 8 is 5
```