Java----数组

Java----数组

一维数组的声明、定义和初始化

多维数组

数组的拷贝

JavaBean和数组存储数据信息

数组也是对象,存放于堆中

一、一维数组

数组的初始化有三种方法:静态初始化、动态初始化和默认初始化

静态初始化就是在声明定义数组的同时对数组进行赋值,数组元素用{}包起来

动态初始化通过数组下标对数组进行单个赋值

默认初始化就是让数组元素为数组类型的默认值,int[ ]类型的默认值为0

package com.company.arrylist;

public class test1 {

    public static void main(String[] args) {

        int a[] = {1,2};  //声明+定义+赋值

        int[] b = new int[3];  //声明+定义

        int[] c;  //先声明
        c = new int[2];   //后定义

        int[] as = new int[2];

        //静态初始化
        int[] asd = {0,1,2};

        //先声明,后定义,再动态初始化
        int[] asdf;
        asdf = new int[5];
        for(int i = 0;i<asdf.length;i++)
        {
            asdf[i] = i*2+1;
            System.out.println(asdf[i]);
        }

        //声明+定义
        int[] aw = new int[10];

        System.out.println(c[0]);
    }
}

数组的遍历可以通过for循环遍历

        for(int i = 0;i<asdf.length;i++)
        {
            asdf[i] = i*2+1;
            System.out.println(asdf[i]);
        }

二、多维数组

多维数组相当于一维数组中的元素依旧为数组,数组嵌套n层为n维数组

package com.company.array01;

import java.util.Arrays;

/*
* 多维数组定义
*/
public class test04 {

    public static void main(String[] args) {

        int[][] a = new int[2][];
        a[0] = new int[]{1, 2};
        a[1] = new int[]{0, 5};

        int[][] b = {{7, 8},
                {8, 9}};

        System.out.println(a[0].length);
        System.out.println(Arrays.toString(a[1]));

        //for循环搭配Arrays.toString()打印二维数组
        for (int i = 0; i < a.length; i++) {
            System.out.println(Arrays.toString(a[i]));
        }


        //for循环遍历二维数组
        for (int i = 0; i < a.length; i++)
        {
            for(int j=0;j<a[i].length;j++)
            {
                System.out.print(a[i][j]+"\t");
            }
            System.out.println();
        }
    }
}

多维数组的遍历可以通过for循环嵌套或者for循环搭配Arrays.toString()方法遍历


        //for循环搭配Arrays.toString()打印二维数组
        for (int i = 0; i < a.length; i++) {
            System.out.println(Arrays.toString(a[i]));
        }


        //for循环遍历二维数组
        for (int i = 0; i < a.length; i++)
        {
            for(int j=0;j<a[i].length;j++)
            {
                System.out.print(a[i][j]+"\t");
            }
            System.out.println();
        }

三、array类的使用

Java中定义了java.util.array类

这个类专门用来实现数组的一些工作

package com.company.array01;

import java.util.Arrays;

/*
* 测试iava.util.arrays类
*/
public class test03 {
    public static void main(String[] args) {

        int[] a = {0,2,4,1};

        //测试打印
        System.out.println (Arrays.toString(a));

        //测试排序
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));

        //测试二分法查找,不排序无法查找
        System.out.println(Arrays.binarySearch(a,5));

        //测试填充
        Arrays.fill(a,1,3,100);
        System.out.println(Arrays.toString(a));
    }
}

四、 数组拷贝

数组的拷贝使用arraycopy方法

arraycopy(源数组,源数组位置,目的数组,目的数组位置,长度)

数组的拷贝和字符串的截取一样,都是包头不包尾。如从位置2到位置5 ,那么就复制2,3,4位置的元素

package com.company.array01;

public class arraycopy {
    public static void main(String[] args) {
        int[] a = {0,1,2,3};
        int[] b = new int[5];

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

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

五、 JavaBean和数组存储数据

通过创建类和类的对象存储数据

类的对象作为数组元素,创建新的数组(一维)将对象包装起来,形成数据存储

package com.company.array01;
/*
* javabean和数组存储数据*/

import java.util.Arrays;

public class test05 {
    public static void main(String[] args) {

        student s1 = new student(10,"woman",78);
        student s2 = new student(11,"woman",80);
        student s3 = new student(12,"man",90);

        //在使用类数组包装对象时定义一维数组
        student[] stus =new student[3];
        stus[0] = s1;
        stus[1] = s2;
        stus[2] = s3;

        System.out.println(Arrays.toString(stus));

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

        System.out.println(stus[0].getAge()+"\t"+stus[0].getGender());
    }
}
class student{
    private int age;
    private  String gender;
    private double score;

    public student(int age, String gender, double score) {
        this.age = age;
        this.gender = gender;
        this.score = score;
    }

    public int getAge() {
        return age;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "student{" +
                "age=" + age +
                ", gender='" + gender + '\'' +
                ", score=" + score +
                '}';
    }
}

可以通过在类中重写toString方法,然后调用重写后的方法输出数组元素或想要的结果

在JavaBean和数组解和使用时,toString方法指向的是对象使用的,在类中必须重写toString方法才能返回自定义的内容,否则一律返回地址。

且重写的toString方法不能直接用于数组,直接用于数组时返回地址:System.out.println(mans);
在这里插入图片描述
只能用于Arrays.toString(mans)和在for循环中使用System.out.println(mans[i]);
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值