Java基础自学记录五

数组、排序与查找

此为笔者个人笔记,如有错误还请指出

数组是一种数据类型,是引用类型

public class Hens{
    public static void main(String[] args){
        double[] hens = {3, 5, 1, 4, 2, 50};
        System.out.println(hens.length);
        double sum = 0;
        for(double hen: hens){
            sum += hen;
        }
        System.out.println(sum/6);
    }
}

数组定义方式

  • 静态初始化 double[] hens = {1, 2, 3, 4,…};

  • 动态初始化 int[] a = new int[6];

  • 还可先声明再创建: int[] a; a = new int[10];

  • int[] a = new int[]{1, 2, 3}; //注意右边中括号里一定不要加长度,不然编译不通过

数组的索引

a[i] 数组名加下标,与c++一致

数组的一些操作

  • 数组名.length得到数组的长度/大小

注意事项

  • 数组中的元素可以是任何数据类型(引用类型或者基本类型)但是不能混用

  • 数组初创建后若没有赋值,会有默认值 int 0 short 0 byte 0 long 0 float 0.0 double 0.0 char \u0000 boolean false String null

  • 使用数组前必须经过两个步骤: 1、声明数组并开辟空间 2、给数组中的元素赋值

  • 数组下标不能越界,会抛出异常(长度减一)

  • 数组属于引用类型,数组型数据是对象(Object)

应用案例

public class ArrayUse{
    public static void main(String[] args){
        char[] a = new char[26];
        for(int i = 0; i < 26; i++){
            a[i] = 'A' + i;
        }
        System.out.println(a);
    }
}
public class ArrayUse{
    public static void main(String[] args){
        int[] a = {4, -1, 9, 10, 23};
        int max = a[0], index = 0;//假设第一个元素就是最大值
        for(int i = 1; i < 5; i++){ //直接从第二个元素开始比较即可
            if(a[i] > max){
                max = a[i];
                index = i;
            }
        }
        System.out.println("maxnum: " + max + "  index:" + index);
    }
}

数组的赋值机制

基本数据的互相赋值,这个值就是具体的数据,不会影响到原来的变量(值拷贝)

数组互相赋值相当于C++中的浅拷贝,改变赋给的那个元素也会改变之前的值(引用拷贝或地址拷贝)

public class giveValue{
    public static void main(String[] args){
        int a1 = 1;
        int a2 = a1;
        int a2 = 2;
        System.out.println("a1:"+a1+"a2"+a2);
        int[] arr1 = {1,4,7,8};
        int[] arr2 = arr1;
        arr2[0] = 2;
        for(int i: arr1){
            System.out.print(i+"\t");
        }
        //注意两者区别
    }
}

在这里插入图片描述

原理

在这里插入图片描述

数组深拷贝方法:创建新数组,开辟新空间,运用循环进行拷贝

实现数组反转:

public class ReverseArray{
    public static void main(String[] args){
        int[] arr1 = {20, 30, 55, 12, 67, 98};
        int temp;
        for(int i = 0; i < arr1.length / 2; i++){
            int wt = arr1.length - i - 1;
            temp = arr1[i];
            arr1[i] = arr1[wt];
            arr1[wt] = temp;
        }
		for(int i: arr1){
			System.out.print(i+"\t");
		}
    }
}

数组扩容

还以为有什么神奇的操作,没想到还是创建一个更长的数组然后赋值

//实现数组扩容,每增加一个询问是否继续添加
import java.util.*;
public class ArrayAdd{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int[] arr = {6, 9, 9};
        do{
            char ans = input.next().charAt(0);
            System.out.println("continue? y/n");
            if(ans == 'n') break;
            System.out.print("input: ");
            int wt = input.nextInt();
            int l = arr.length;
            int[] newarr = new int[l + 1];
            for(int i = 0; i < l; i++){
                newarr[i] = arr[i];
            }
            newarr[l] = wt;
            arr = newarr;//重新指向
			for(int i: arr){
				System.out.print(i+"\t");
			}
        }while(true);
    }
}

数组排序与查找

冒泡排序

public class Sorts{
    public static void main(String[] args){
        int[] a = {24, 69, 80, 57, 13};
        BubbleSort(a, false);
    }
    public static void BubbleSort(int[] a, boolean wt){
        //思路: 每次比较相邻元素,a[j]>a[j+1], 继续比较a[j+1]与a[j+2], 这样每趟都能把最大的放到最后位置
        //且设立一个flag,若有一趟每发生交换,证明排序完成了,就退出循环
        //倘若有n轮排序,则最多进行n-1轮排序,每轮最多循环n-1次,因此时间复杂度是O(n^2)
        //false小到大,true大到小
        int l = a.length, temp;
        for(int i = 1; i < l; i++){
            boolean flag = true;
            for(int j = 0; j < l - i; j++){ //length-1的原因:每轮已经把最大的放到最后面了
                if((a[j] > a[j + 1] && !wt) || (a[j] < a[j + 1] && wt)){
                    flag = false;
                    temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
            if(flag == true) break;
        }
        for(int i: a){
            System.out.print(i + "\t");
        }
        System.out.println();
    }
}

查找

顺序查找 二分查找
public class Search{
    public static void main(String[] args){
        int[] a = {1, 8, 10, 89, 1000, 1234};
        int index = DoubleSearch(a, 8);
        System.out.println("index: "+index);
    }
    public static int DoubleSearch(int[] a, int target){
        //二分查找用于有序数列
        int lt = 0, rt = a.length - 1;
        int pt = 0;
        while(true){
            pt = (lt + rt) / 2;
            if(target == a[pt]){
                System.out.println("Bingo!");
                break;
            }else if(target > a[pt]){
                lt = pt + 1;
            }else{
                rt = pt - 1;
            }
        }
        return pt;
    }
}

多维数组-二维数组

定义方法

  • 直接int[][] arr = {{1, 2, 3}, {4, 5 , 6}, {7, 8, 9}};
  • 动态初始化 int[][] a = new int[2][3] ; 如果没初始化默认元素都为0

int[][] arr = {{1, 2, 3}, {4, 5 , 6}, {7, 8, 9}};
//遍历方法
for(int[] i: arr){
    for(int j: i){
        System.out.print(i+"\t");
    }
}
//i,j下标遍历...
int[][] a = new int[2][2];
int[][] b = a;
b[1] = {1, 2};
//仍然会改变a
int[][] c;
c = new int[2][4];

内存结构

在这里插入图片描述

注意事项:二维数组中每个一维数组元素不一定要相等

int[][] arr = {{1},
               {2, 2},
               {3, 3, 3}};
int[] arr[];
int arr[][];
//动态创建
int[][] arr = new int[5][];//不要忘记加空中括号
for(int i = 0;i < arr.length; i++){
    //给每个一维数组开空间
    arr[i] = new int[i + 1];
    for(int j = 1; j <= i + 1; j++){
        arr[i][j - 1] = j;
    }
}

在这里插入图片描述

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-a3xE4v8A-1664368450805)(D:\Typora\typora-user-images\image-20220923132634923.png)]

我超,好神奇

练习题

int[][] arr = new int[10][];
for(int i = 0; i < arr.length; i++){
    arr[i] = new int[i + 1];
    for(int j = 0; j <= i; j++){
        arr[i][j] = j + 1;
    }
}
for(int[] i: arr){
    for(int j: i){
        System.out.print(j+"  ");
    }
    System.out.println();
}
//杨辉三角
import java.util.*;
public class YangHui{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int l = input.nextInt();
        int[][] arr = new int[l][];
        for(int i = 0; i < l; i++){
            arr[i] = new int[i + 1];
            for(int j = 0; j <= i; j++){
                if(j == 0 || j == i) arr[i][j] = 1;
                else arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
            }
        }
        for(int[] a: arr){
            for(int b: a){
                System.out.print(b+"  ");
            }
            System.out.println();
        }
    }
}

Homework

//在升序的数组中插入一个元素,使其仍然是升序的
public class homework{
    public static void main(String[] args){
        int[] a = {10, 12, 45, 90};
        int target = 23, index = -1;
        int[] b = new int[a.length + 1];
        for(int i = 0, j = 0; i < b.length; i++){
            //还没到换的位置就等下标赋值,到了换的位置就输入target并错位赋值
		   if(target <= a[j] && target > a[j - 1]){
                b[i] = target;
                i++;
            }
            b[i] = a[j];
            j++;
        }
        a = b;
        for(int i: a){
            System.out.print(i+"\t");
        }
        System.out.println();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值