public class ArrayDuplicate {
public static void main(String[] args) {
//数组的复制,String[] arr1; arr1 = arr;是错误的,并非真正的复制。
String[] arr = new String[] {"GG","JJ","DD","MM","AA","BB"};
String[] arr1= new String[arr.length];
for(int i=0;i < arr1.length;i++) {
arr1[i] = arr[i];
System.out.print(arr1[i]+" ");
}
System.out.println();
//数组的查找
String s = "EE";
boolean isFlag = true;
for(int i = 0; i<arr.length;i++) {
if(s == arr[i]) {
System.out.print("此数据在数组中存在,存在索引位置为: "+i);
isFlag = false;
break;
}
}
if(isFlag) {
System.out.print("此数据在数组中不存在!!!");
}
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;i<arr.length;i++) {
System.out.print(arr[i]+" ");
}
}
}
//二分法查找
public class BinarySearchArray {
public static void main(String[] args) {
int[] arr = new int[] {1,12,13,15,26,58,69,98,102,235,546,689};
int value = 132;
int start = 0;
int end = arr.length - 1;
boolean isFlag = true;
for(int i=0; i<arr.length; i++) {
int middle = (start + end)/2;
if(value == arr[middle]) {
System.out.print("找到了,所在数组中索引位置为: "+middle);
isFlag = false;
break;
}else if(value > arr[middle]) {
start = middle +1;
}else {
end = middle;
}
}
if(isFlag) {
System.out.print("很遗憾,没有在当前数组中找到value!!!!");
}
}
}