com.atguigu.exec Package;
/ *
* array1 and array2 two variables declared in Mian () method, they int type array
* initialize array1 as 2,3,5,7,11,13,17,19
* Display content of array1
* variable is equal to the assignment array2 array1, array2 modify the even-indexed elements to be equal to the index value and print array1
relationship of * array1 and array2
* modifications of the subject to achieve replication of array1 array2
*
* /
public class ArrayDome3 {
public void main static (String [] args) {
int [] array1, array2;
array1 = new new int [] {2,3,5,7,11,13,17,19};
for (int I = 0; I < array1.length; I ++) {
System.out.println (array1 [I] + "\ T");
}
// assignment array2 array1 address variable equal to the value of the array rather than copy assignment
array2 array1 =;
/ *
* copy method
* array2 = new int [array.length] ; // new array must be re 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");
}
}
}
/ *
* reverse
* /
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 ( "find, position:" + I);
isFlag = to false;
BREAK;
}
}
IF (isFlag) {
System.out.println ( " not found ");
}
// binary search
@ premise: the array must be ordered by looking
int [] of arr1 = new new int [] {} 1,4,6,8,43,48,99,100;
int = dest1 48;
int head = 0;
int = End-arr1.length. 1;
Boolean = isFlag1 to true;
the while (head <= End) {
int = Middle (End + head) / 2;
IF (== dest1 of arr1 [Middle]) {
System.out.println ( "find, position" + Middle);
isFlag1 = to false;
BREAK;
} the else IF (Middle <dest1) {
head = middle + 1;
}
else {
end = middle -1;
}
}
if(isFlag1) {
System.out.println("未找到");
}
}
}
* Java.util.Arrays: array operation tools, many of which defines a method of operating an array
*
*
* /
public static void main (String [] args) {
//1.boolean the equals (int [] A, int [] B) determines whether the same two arrays
int [] arr1 = new int [ ] {1, } 2,3,4;
int [] = arr2 is new new int [] {1,3,2,4};
Boolean = isEquals that Arrays.equals (of arr1, arr2 is);
System.out.println (isEquals);
// 2 .String toString (int [] a) : the information output array
System.out.println (of Arrays.toString (arr2 is));
//3.void fill (int [] A, int Val): the specified value is filled into an array
Arrays.fill (arr1,10);
System.out.println (of Arrays.toString (of arr1));
//4.void Sort (int [] A): sort the array
Arrays.sort (arr2 is);
the 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("未找到");
}
}
}
*数值型数组中元素、最大值、最小值、平均值、总和
*/
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;
}
/*
* 数组的冒泡排序
*/
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]);
}
}
}