//查找数组中的元数
/*public class Test58{
public static void main(String[] args) {
int[] arr={1,2,3,4,5,6,7,8,9};
int ret=find(arr,5);
System.out.println(ret);
}
public static int find(int[] arr1,int num){
for(int i=0;i<arr1.length;i++){
if(arr1[i]==num){
return i;
}
}
return -1;
}
}*/
//查找数组中指定的元数(二分查找)
public class Test58{
public static void main(String[] args) {
int[] arr={1,2,3,4,5,6,7,8,9};
int ret=find(arr,5);
System.out.println(ret);
}
public static int find(int[] arr1,int num){
int left=0;
int right=arr1.length-1;
while(left<=right){
int mid=(left+right)/2;
if(num>arr1[mid]){
left=mid+1;
}else if(num<arr1[mid]){
right=mid-1;
}else{
return mid;
}
}
return -1;
}
}
//找数组中最大的元数
/*public class Test57{
public static void main(String[] args) {
int[] arr={1,2,3,4,56,8,45,6};
System.out.println(MAX(arr));
}
public static int MAX(int[] arr1){
int max=arr1[0];
for(int i=1;i<arr1.length;i++){
if(max<arr1[i]){
max=arr1[i];
}
}
return max;
}
}*/
//求数组中原数的平均值
public class Test57{
public static void main(String[] args) {
int[] arr={1,2,3,4,56,6};
double num=agv(arr);
System.out.println(num);
}
public static double agv(int[] arr1){
double num=0;
for(int x:arr1){
num+=x;
}
return num/arr1.length;
}
}
import java.util.Arrays;
//数组转换为字符串
/*public class Test56{
public static void main(String[] args) {
int[] arr={1,2,3,4};
newarr(arr);
}
public static void newarr(int[] arr1){
System.out.println(Arrays.toString(arr1));
}
}*/
//数组的拷贝
import java.util.Arrays;
public class Test56{
public static void main(String[] args) {
int[] arr={1,3,4,5,6};
int[] newarr=Arrays.copyOf(arr,arr.length);
System.out.println(Arrays.toString(newarr));
}
}
import java.util.Arrays;
//数组逆序
/*public class Test60{
public static void main(String[] args) {
int[] arr={1,2,3,4};
reverse(arr);
}
public static void reverse(int[] arr1){
int left=0;
int right=arr1.length-1;
while(left<right){
int tmp=arr1[left];
arr1[left]=arr1[right];
arr1[right]=tmp;
left++;
right--;
}
System.out.println( Arrays.toString(arr1));
}
}*/
//数组数字排列
public class Test60{
public static void main(String[] args) {
int[] arr={1,2,3,4,5,6};
transform(arr);
}
public static void transform(int[] arr1){
int left=0;
int right=arr1.length-1;
while(left<right){
if(left<right&&arr1[left]%2==0){
left++;
}else if(left<right&&arr1[right]%2!=0){
right--;
}else{
int tmp=arr1[left];
arr1[left]=arr1[right];
arr1[right]=tmp;
}
}
System.out.println(Arrays.toString(arr1));
}
}