//数组的复制和反转
public class ArrayTest2 {
public static void main(String[] args) {
String[] arr = new String[]{"AA","BB","CC","DD","EE","FF"};
//数组的复制
String[] arr1 = new String[arr.length];
for(int i = 0;i < arr.length;i++){
arr1[i] = arr[i];
}
//输出arr1数组
for(int i = 0;i < arr1.length;i++){
System.out.print(arr[i] + "\t");
}
System.out.println();
//数组的反转
//方法一
// for(int i = 0;i < arr.length/2;i++){
// String temp = arr[i];
// arr[i] = arr[arr.length - 1 - i];
// arr[arr.length - 1 - i] = temp;
// }
//方法二
for(int i = 0,j = arr.length-1;i < j;i++,j--){
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
//输出arr数组
for(int i = 0;i < arr.length;i++){
System.out.print(arr[i] + "\t");
}
//查找
//线性查找
String dest = "GG";
boolean isFlag = true;
for(int i = 0;i <arr.length;i++){
if(dest.equals(arr[i])){
System.out.println("找到了指定的元素,位置为:" + i);
isFlag = false;
break;
}
}
if(isFlag == true){
System.out.println("很遗憾,没有找到哦");
}
System.out.println();
//二分法查找(前提:所要查找的数组必须有序)
.
int[] arr2 = new int[]{-98,-34,2,34,54,66,79,105,210,333};
boolean isFlag1 = true;
int dest1 = -34;
int head = 0;
int end = arr2.length - 1;
while(head <= end){
int middle = (head + end) / 2;
if(dest1 == arr2[middle]){
System.out.println("找到了指定的元素,位置为:" + middle);
isFlag1 = false;
break;
}else if(dest1 < arr2[middle]){
end = middle - 1;
}else{
head = middle + 1;
}
}
if(isFlag1){
System.out.println("很遗憾,没有找到哦");
}
}
}
}
}