第七章、数组和算法

一、一维数组

1、数组的本质和初始化

/**
 * 测试数组的定义和基本使用方式
 */
public class Test01 {

    public static void main(String[] args) {

        //数组的定义
        int[] a=new int[5];
        int b[]=new int[3];

        //数组的静态初始化
        int[] c={100,200,300,400};

        //数组的动态初始化
        for(int i=0;i<a.length;i++){
            a[i]=10+10*i;
        }

        //遍历数组
        for(int i=0;i<a.length;i++){
            System.out.print(a[i]+" ");
        }

        //通过for-each循环,遍历数组
        for(int temp:c){
            System.out.println(temp);
        }

    }
}
/**
 * 测试引用类型数组的使用
 */
public class Test02 {

    public static void main(String[] args) {

        User[] users=new User[3];
        //数组的静态初始化
        User[] user={
                new User(101,"tom"),
                new User(102,"jack"),
                new User(103,"mary")
        };

        users[0]=new User(1001,"张三");
        users[1]=new User(1002,"李四");
        users[2]=new User(1003,"王五");

        for(int i=0;i<users.length;i++){
            System.out.println(users[i].toString());
        }

        for(User u:user){
            System.out.println(u);
        }
    }
}

class User{
    private int id;
    private String name;

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
/**
 * 测试数组的拷贝
 */
public class Test03 {

    public static void main(String[] args) {
        String[] a={"阿里","淘宝","京东","搜狐","网易"};
        String[] b=new String[6];

        System.arraycopy(a,1,b,0,2);

        for(String s:b){
            System.out.println(s);
        }
    }
}
import java.util.Arrays;

/**
 * 测试工具类:java.util.Arrays
 */
public class Test04 {

    public static void main(String[] args) {

        int[] c={10,82,34,2,100,40};
        System.out.println(Arrays.toString(c));
        Arrays.sort(c);
        System.out.println(Arrays.toString(c));
        System.out.println(Arrays.binarySearch(c,100));

        int[] d=new int[5];
        Arrays.fill(d,100);
        System.out.println(Arrays.toString(d));
    }
}

二、多维数组

import java.util.Arrays;

/**
 * 测试二维数组的基本语法
 */
public class Test05 {

    public static void main(String[] args) {
        int[][] a=new int[3][];

        a[0]=new int[2];
        a[1]=new int[4];
        a[2]=new int[3];

        a[0][0]=10;
        a[0][0]=20;

        a[1]=new int[]{10,20,30,40};
        a[2]=new int[]{10,20,30};

        int[][] b={
                {10,20,30},
                {10,20},
                {100,200,300,400}
        };

        //遍历二维数组,使用循环嵌套
        for(int i=0;i<b.length;i++){
            for (int j=0;j<b[i].length;j++){
                System.out.print(b[i][j]+" ");
            }
            System.out.println();
        }

        //使用Arrays.toString()遍历数组
        for(int i=0;i<b.length;i++){
            System.out.println(Arrays.toString(b[i]));
        }
    }
}

import java.util.Arrays;

/**
 * 测试二维数组,利用Object存储表格信息
 */
public class Test06 {
    public static void main(String[] args) {
        Object[] a1={1001,"高琪", 18,"讲师","2021-4-15"};     //使用一个Object数组,表达一行信息
        Object[] a2={1002,"高小七", 19,"助教","2019-10-10"};
        Object[] a3={1003,"高小八", 20,"班主任","2019-5-5"};

        Object[][] emps=new Object[3][];
        emps[0]=a1;
        emps[1]=a2;
        emps[2]=a3;
        System.out.println(Arrays.toString(emps[0]));
        System.out.println(Arrays.toString(emps[1]));
        System.out.println(Arrays.toString(emps[2]));

        for(int i=0;i<emps.length;i++){
            System.out.println(Arrays.toString(emps[i]));
        }

        Emp e1=new Emp(1001,"高琪", 18,"讲师","2021-4-15");
        Emp e2=new Emp(1002,"高小七", 19,"助教","2019-10-10");
        Emp e3=new Emp(1003,"高小八", 20,"班主任","2019-5-5");

        Emp[] employees={e1,e2,e3};
        for(Emp temp:employees){
            System.out.println(temp);
        }
    }
}

class Emp{
    private int id;
    private String name;
    private int age;
    private String job;
    private String hiredate;

    public Emp(int id, String name, int age, String job, String hiredate) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.job = job;
        this.hiredate = hiredate;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", job='" + job + '\'' +
                ", hiredate='" + hiredate + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public String getHiredate() {
        return hiredate;
    }

    public void setHiredate(String hiredate) {
        this.hiredate = hiredate;
    }
}

三、冒泡排序法

/**
 * 测试冒泡排序法
 */
public class Test08 {
    public static void main(String[] args) {
        int[] values={3,1,6,2,9,0,7,4,8,5};
        bubbleSort(values);
    }
    
    public static void bubbleSort(int[] values){
        System.out.println("排序前数组是:"+Arrays.toString(values));

        int temp;       //用于交换的临时变量
        boolean flag=true;   //标记数组是否已达到有序状态
        for(int i=0;i<values.length;i++){

            for(int j=0;j<values.length-i-1;j++){
                if(values[j]>values[j+1]){
                    temp=values[j];
                    values[j]=values[j+1];
                    values[j+1]=temp;
                    flag=false;     //表明数组仍处于无序状态,需要继续排序
                }
                System.out.println("第"+i+"趟,第"+j+"次:"+Arrays.toString(values));
            }
            //冒泡排序的优化
            if(flag==true){
                System.out.println("排序结束!");
                break;
            }else if(flag==false){
                flag=true;
            }
        }
    }
}

四、二分法查找(binary search)

/**
 * 测试二分法查找元素
 */
public class Test09 {
    public static void main(String[] args) {
        int[] arr={30,20,50,10,80,9,7,12,100,40,8};
        int searchWord=20;      //定义在数组内要查找的元素

        System.out.println(searchWord+"的索引位置是:"+binarySearch(arr,searchWord));
    }

    /**
     * 二分法查找
     * @param array:目标数组
     * @param value:所要查找的值
     * @return 如果查找到,则返回值在数组中的索引;如果未查找到,则返回-1
     */
    public static int binarySearch(int[] array,int value){
        int low=0;
        int high=array.length-1;

        //二分法查找,必须先对数组进行排序
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));

        while(low<=high){
            int middle=(low+high)/2;
            if(value==array[middle]){
                return middle;
            }

            if(value<array[middle]){
                high=middle-1;
            }
            if(value>array[middle]){
                low=middle+1;
            }
        }
        return -1;

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值