import java.util.Arrays;
public class TestFive {
//数组相关学习
//相同的数据类型有序的放在一起;
// 数据类型[] 数组名称=new 数据类型[]{存放指定数据类型的数据}
//数据静态初始化,在创建数据组时已经确定有多少个元素
public static void test(){
//静态初始化,也可以写成
//String[] names = {"zhangsan","lisi","wanger"};
String[] names = new String[]{"zhangsan","lisi","wanger"};
String[] stNames = {"zhangsan","lisi","wanger"};
}
public static void printList(String[] a){
//打印数组内容Arrays.toString()方法
String list = Arrays.toString(a);
System.out.println(list);
}
public static void main(String[] args) {
int a =10;//一个变量只能存一个值
//数组可以存放相同数据类型的多个值
//静态初始化数组
int[] nums = new int[]{10,2,15,2}; //定义一个int类型的名字为nums的数组,索引从0开始
//通过索引读取数组中对应的值
System.out.println(nums[1]);
//动态初始化数组,从空间申请一个5索引的空间动态数组
String[] students = new String[5];
//往动态数组中存放数据
students[0] ="张三";
students[1] = "李四";
students[2] = "王二";
students[3] = "麻子";
students[4] = "张无忌";
//students[101] = "王二"; //这样写数组越界报错,申请的只有100
System.out.println(students[0]);
System.out.println(students[1]);
//获取数组长度length
System.out.println(students.length);
//遍历数组for循环,动态初始化的没有值的索引,其值为null
for (int i = 0; i < students.length; i++) {
System.out.println(students[i]);
}
//直接打印数组是一个对象地址
System.out.println(students);
printList(students);
//倒序打印数组中的元素
for (int i = students.length-1; i>=0 ; i--) {
System.out.print(students[i]);
System.out.print("\t");
}
//整一个冒泡排序
//需要排序的数字放在一个数组中
int[] numbers = {15,2,4,3,8,20};
//升序
for (int i = 0; i < numbers.length; i++) {
for (int j = i; j <numbers.length-1; j++) {
if(numbers[i]>=numbers[j+1]){
int temp = numbers[j+1];
numbers[j+1]=numbers[i];
numbers[i] = temp;
}
}
}
System.out.println(Arrays.toString(numbers));
//降序
for (int i = 0; i < numbers.length; i++) {
for (int j = i; j <numbers.length-1; j++) {
if(numbers[i]<=numbers[j+1]){
int temp = numbers[j+1];
numbers[j+1]=numbers[i];
numbers[i] = temp;
}
}
}
System.out.println(Arrays.toString(numbers));
//冒泡排序
int[] se = {2,5,13,18,6,1,0};
for (int j = se.length-1; j >0; j--) {
for (int i = 0; i < j; i++) {
if(se[i]>se[i+1]){
int temp = se[i];
se[i] = se[i+1];
se[i+1] = temp;
}
}
}
System.out.println(String.format("冒泡排序结果: %s",Arrays.toString(se)));
//选择排序 逐步把最小的值排到第一 第二 第三
int[] selectSort = {15,1,7,14,2,0};
for (int i = 0; i < selectSort.length; i++) {
//先排序第一轮,拿出第一个值依次和后面的比较,如果第一个小于后面的相互交换
for (int j = i; j < selectSort.length-1; j++) {
if(selectSort[i]>=selectSort[j+1]){
int temp = selectSort[i];
selectSort[i]=selectSort[j+1];
selectSort[j+1]=temp;
}
}
}
System.out.println(String.format("选择排序结果: %s",Arrays.toString(selectSort)));
//二分查找算法
int[] twoSort = {0, 1, 2, 7, 14, 15,50,100,101};
//查找1的索引值在数组中哪个位置
System.out.println(Arrays.toString(twoSort));
int find =50;
int indexMax = twoSort.length-1;
int indexMin = 0;
for (int i = indexMin; i <indexMax; i++) {
int midNum = twoSort[(indexMin+indexMax)/2];
if(midNum>find){
indexMax = (indexMin+indexMax)/2-1;
}else if(midNum<find) {
indexMin =(indexMin+indexMax)/2+1;
}else {
System.out.print(String.format("要查找的数据%s 在数组中的索引位置为:",find));
System.out.println((indexMin+indexMax)/2);
break;
}
}
//二分查找法
int f = 50;
int max = twoSort.length-1;
int min = 0;
while(true){
int mid = twoSort[(min+max)/2];
if (f>mid){
min=(min+max)/2+1;
}else if(f<mid){
max = (min+max)/2-1;
}else{
int fIndex = (min+max)/2;
System.out.println(String.format("要查找的数据%s 在数组中的索引位置为: %s",f,fIndex));
break;
}
}
//Arrays自带二分查找法
int F = 50;
int indexF = Arrays.binarySearch(twoSort,F);
System.out.println(String.format("要查找的数据%s 在数组中的索引位置为 %s",F,indexF));
//二维数组,就是一维数组中嵌套一个数组,开发过程中使用较少
//定义一个二维数组
int[][] twoList = new int[][]{{1,2,3},{4,5,6}};
//二维数组遍历取值
int val = twoList[1][2];
System.out.println(val);
for (int i = 0; i < twoList.length ; i++) {
for (int j = 0; j <twoList[i].length ; j++) {
System.out.print(twoList[i][j]);
}
}
//快速遍历数组
System.out.println("\n");
int[] shu = new int[]{1,2,3,4,5,6};
for (int i : shu) {
System.out.print(i);
}
}
}