数组
import java.util.Arrays;
/*
* 1、一维数组的创建和使用
* 1.1、创建一维数组
* 数组元素类型 数组名[];
* 数组元素类型[] 数组名;
* 数组名 = new 数组元素类型[数组元素的个数];
* 使用new关键字为数组分配内存时,整形数组中各个元素的初始值都为0.
* 1.2、初始化一维数组
* int arr[] = new int[]{1,2,3,4,25};
* int arr2[] = {34,23,12,6};
* 1.3、使用一维数组
* 通过for循环遍历,或者通过指定的索引获取指定的元素。
* 2、二维数组的创建及使用
* 2.1、二维数组的创建
* 数组元素类型 数组名[][];
* 数组元素类型[][] 数组名;
* 2.2、二维数组的初始化
* 和一维数组类似
* 2.3、使用二维数组
* 对于整型二维数组,创建成功后系统会赋给数组中每个元素初始值0.
* 3、数组的基本操作
* 3.1、遍历数组
* 通过数组的length属性可以获得数组的长度,使用for循环遍历
* 或者使用foreach循环更加简单
* 3.2、填充替换数组元素
* fill(int[] a,int value);
* a:要进行替换的数组; value:要存储数组中所有元素的值
* fill(int[] a,int fromIndex,int toIndex,int value);
* 如果指定的索引位置大于或等于要进行填充的数组的长度,那么会报数ArrayIndexOutOfBoundsException();异常
* 3.3、对数组进行排序
* Arrays.sort(Object); Object为要进行排序的数组名称
* 3.4、复制数组
* copyOf(arr,int newlength) arr:要进行复制的数组;newlength:新数组长度,长补0,短从头。
* copyOfRange(arr, int fromIndex,int endIndex)
* 3.5、数组查询
* 通过二分搜索法来搜索,所以在调用之前必须对数组进行sort()排序
* binarySearch(Object[] a,Object key) 如果key在数组a中,那么返回搜索值的索引,否则返回-1或插入点
* binarySearch(Object[] a,int fromIndex,int endIndex,Object key)
* 指定范围不能超出数组的范围
* 4、数组的排序算法
* 4.1、冒泡排序
* 4.2、直接选择排序
* 4.3、反转排序
* */
public class Demo {
public static void main(String[] args) {
//创建一个数组,这个数组的元素是乱序的
int[] array = {9,4,6,7,5,8,2,1,3};
//创建冒泡排序类的对象
Demo bs = new Demo();
//调用排序方法将数组排序
bs.sort2(array);
// int[] arr1 = {1,2,3,4,5,6,7,8,9};
// int[] arr2 = {6,6,6,6,6,6};
//
// arr2 = Arrays.copyOfRange(arr1,0,4);
//
// showArray(arr1);
// showArray(arr2);
//编写程序,将数组中最小的输出
// int[] array = {5,4,6,9,7,8,1,3,2};
// int min=0;
// for(int i = 1;i<array.length;i++){
// if(array[i]>array[i-1]){
// min = array[i-1];
// }
// }
// System.out.println(min);
//编写程序,将数组arr索引位置是2的元素替换成"bb",并将替换前数组中的元素和替换后的数组元素全部输出;
// String[] arr = {"1","2","3","4"};
// String[] arr1 = Arrays.copyOf(arr, 2*arr.length);
// Arrays.fill(arr,2,3,"bb");
// for(int i= 0;i<arr.length;i++){
// arr1[arr.length+i] = arr[i];
// }
//
// for(String s : arr1){
// System.out.print(s+" ");
// }
//编写程序,花式反转
int a[][] = {{1,2,3},{4,5,6},{7,8,9}};
for(int i = 0;i<a.length;i++){
for(int j= 0; j<i;j++){
int temp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = temp;
}
}
for(int i = 0; i<a.length;i++){
for(int j = 0;j<a[i].length;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
/**
* 冒泡排序
* */
public void sort(int[] array){
for(int i = 1; i<array.length;i++){
//比较相邻的两个元素,较大的数往后冒泡
for(int j = 0;j<array.length-i;j++){
if(array[j]>array[j+1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
showArray(array);
}
/**
* 直接选择排序
* */
public void sort1(int[] array){
int index;
for(int i = 1;i<array.length;i++){
index = 0;
for(int j = 0;j<=array.length-i;j++){
if(array[j]>array[index]){
index = j;
}
}
//交换在array.length-i和index上的两个数
int temp = array[array.length-i];
array[array.length-i] = array[index];
array[index] = temp;
}
showArray(array);
}
/**
* 反转排序
* */
public void sort2(int[] array){
System.out.println("原数组内容为:");
showArray(array);
int temp;
int len = array.length;
for(int i = 0; i<len/2;i++){
temp = array[i];
array[i] = array[len-1-i];
array[len-1-i] = temp;
}
System.out.println("数组反转后的内容");
showArray(array);
}
/**
* 显示数组中所有的元素
* */
public static void showArray(int[] array){
for(int i : array){
System.out.print(i+" ");
}
System.out.println("");
}
}