package com.atguigu.exec;
/*
* 在mian()方法中声明array1和array2两个变量,他们是int型数组
* 初始化array1为 2,3,5,7,11,13,17,19
* 显示array1的内容
* 赋值array2变量等于array1,修改array2中偶索引元素,使其等于索引值并打印出array1
* array1和array2的关系
* 修改题目 实现对array2对array1的复制
*
*/
public class ArrayDome3 {
public static void main(String[] args) {
int[] array1,array2;
array1 = new int[] {2,3,5,7,11,13,17,19};
for(int i = 0;i <array1.length;i++) {
System.out.println(array1[i]+"\t");
}
//赋值array2变量等于array1 将地址值赋值 而非数组的复制
array2 = array1;
/*
* 复制方法
* array2 = new int[array.length];//新的数组则必须重新new
* for(int i = 0; i < array.length;i++){
* array2[i] = array1[i];
* }
*
*/
for( int i = 0;i < array2.length;i++) {
if(i % 2 == 0) {
array2[i] = i;
}
}
for(int i = 0;i <array1.length;i++) {
System.out.println(array1[i]+"\t");
}
}
}
/*
* 反转
*/ public class ArrayDemo4 {
public static void main(String[] args) {
String [] arr = new String[] {"aa","bb","cc","dd","jj"};
//方法一
for(int i = 0;i < arr.length/2;i++) {
String temp = arr[i];
arr[i] = arr[arr.length-i-1];
arr[arr.length-i-1] = temp;
}
//方法二
for(int i = 0,j = arr.length-1;i < j;i++,j--) {
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
//查找
//线性查找
boolean isFlag = true;
String dest = "bb";
for(int i = 0;i < arr.length;i++) {
//判断字符是否相等:equals
if(dest.equals(arr[i])) {
System.out.println("找到,位置为:"+i);
isFlag = false;
break;
}
}
if(isFlag) {
System.out.println("未找到");
}
//二分法查找
//前提:所查找的数组必须有序
int[] arr1 = new int[] {1,4,6,8,43,48,99,100};
int dest1 = 48;
int head = 0;
int end = arr1.length-1;
boolean isFlag1 = true;
while(head <= end) {
int middle = (end + head)/2;
if(dest1 == arr1[middle]) {
System.out.println("找到,位置为"+middle);
isFlag1 = false;
break;
}else if(middle < dest1) {
head = middle + 1;
}
else {
end = middle -1;
}
}
if(isFlag1) {
System.out.println("未找到");
}
}
} ********************************************************************************************************************************** package com.atguigu.java; import java.util.Arrays; /*
* java.util.Arrays:操作数组的工具类,里面定义了很多操作数组的方法
*
*
*/ public class ArrayTest1 {
public static void main(String[] args) {
//1.boolean equals(int[] a, int[] b) 判断两个数组是否相等
int[] arr1 = new int[] {1,2,3,4};
int[] arr2 = new int[] {1,3,2,4};
boolean isEquals = Arrays.equals(arr1, arr2);
System.out.println(isEquals);
//2.String toString(int[] a):输出数组信息
System.out.println(Arrays.toString(arr2));
//3.void fill(int[] a,int val):将指定值填充到数组中
Arrays.fill(arr1,10);
System.out.println(Arrays.toString(arr1));
//4.void sort(int[] a):对数组进行排序
Arrays.sort(arr2);
System.out.println(Arrays.toString(arr2));
//5.int binarySearch(int[] a, int key)//二分查找
int[] arr3 = new int[] {-98,-34,2,34,54,66,79,105,210,333};
int index = Arrays.binarySearch(arr3, 79);
if(index >= 0) {
System.out.println(index);
}else {
System.out.println("未找到");
}
}
} ******************************************************************************************************************************** package com.atguigu.exec; /*
*数值型数组中元素、最大值、最小值、平均值、总和
*/
public class ArrayDemo2 {
public static void main(String[] args) {
int[] arr = new int[10];
for(int i = 0;i < arr.length;i++) {
arr[i] = (int)(Math.random()*(99-10+1)+10);
}
int maxValue = arr[0];
for(int i = 1;i < arr.length;i++) {
if(arr[i]>maxValue) {
maxValue = arr[i];
}
}
int minValue = arr[0];
for(int i = 1;i < arr.length;i++) {
if(arr[i] < minValue) {
minValue = arr[i];
}
}
int sum = 0;
for(int i = 0;i < arr.length;i++) {
sum += arr[i];
}
int avgValue = sum / arr.length;
}
} ***************************************************************************************************************************** package com.atguigu.java;
/*
* 数组的冒泡排序
*/
public class BubbleSort {
public static void main(String[] args) {
int[] arr = new int[] {43,32,76,-98,0,64,33,11};
for(int i = 0; i < arr.length - 1;i++) {
for(int j = 0; j < arr.length -1-i;j++) {
if(arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for(int i = 0;i < arr.length;i++) {
System.out.println(arr[i]);
}
}
}