2021-08-25(数组)

学习内容:

一、数组

1.基本概念

数组是有序对象的集合。
有几个特点:

  1. 长度固定,创建时就需要声明
  2. 元素类型相同
  3. 元素类型可以为所有
  4. 数组其实也是对于数组类的一个对象

2.数组创建

代码示例:

    int[] a =new int[10];

在实际操作过程中,我发现一个问题,搜索之后发现:
!!Java对象(包括数组)的声明和生成可以放在任意地方,任意代码块,只有一个原则,生成(或理解成实例化)必须在声明代码之后(我说的执行顺序,否则是会提示编译错误的)!!
链接: https://zhidao.baidu.com/question/330926994.html.
错误代码示例:

public class Regular {
    int[] b=null;
    b = new int[10];
    public static void main(String[] args) {

    }
}

java: 需要<标识符>

更改后正常运行

public class Regular {
    public static void main(String[] args) {
        int[] b = null;
        b = new int[10];
    }
}

下面这张图是内存的分析,其中int[]中间都是0,是因为初始化的原因,int初始化都为0.
在这里插入图片描述

3.数组遍历

没什么问题,就直接用代码泡一下就行

public class Regular {
    public static void main(String[] args) {
        int[] b = null;
        b = new int[]{
                10,11,12,556,415415,11
        };

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

    }
}

其中还有一个增强for循环

格式:for(元素的数据类型 变量 : Collection集合or数组){
}

举例:
public class Regular {

    public static void main(String[] args) {
        int[] a =new int[10];
        for (int i = 0; i < a.length; i++) {
            a[i]=i*i*i;
        }

        for(int i:a){
            System.out.print(i+"\t");
        }

    }
}

0	1	8	27	64	125	216	343	512	729	


4.javabean

一般情况下,封装下,我们对于属性使用private(定义设置以及获取),对于方法使用public。

    private int k;

    public int getK() {
        return k;
    }

    public void setK(int k) {
        this.k = k;
    }

5.数组拷贝

    public static void main(String[] args) {
        int[] a =new int[10];
        for (int i = 0; i < a.length; i++) {
            a[i]=i*i*i;
        }
        for(int i:a){
            System.out.print(i+"\t");
        }
        System.out.println("---------------------------------------------------------");
        int[] b =new int[10];
        System.arraycopy(a,0,b,0,a.length);
        for (int i :b){
            System.out.print(i+"\t");
        }
    }

0	1	8	27	64	125	216	343	512	729	---------------------------------------------------------
0	1	8	27	64	125	216	343	512	729	

这里介绍的是一个System.arraycopy(a,0,b,0,a.length);a、b分别是拷贝的源头和结束;0和0分别是拷贝的位置;a.length指的是拷贝的长度。

6.数组工具类

java.util.Arrays
这个后续自己练一下就行,不写了。

7.二维数组,表格,及作业

直接将作业做一下。
在这里插入图片描述

package com.lu.test1;
import java.util.Arrays;

public class Regular {


    public static void main(String[] args) {

        goodInfo[] info =new goodInfo[4];
        info[0]=new goodInfo(1, "百战牌鼠标", "bz001", 99.21, 0.9);
        info[1]=new goodInfo(2,"jianpanxia","wo102",403.0,0.7);
        info[2]=new goodInfo(3,"shizhan java","bk-001",89.0,0.8);
        info[3]=new goodInfo(4,"gaoqi","gq11",700.0,0.5);
        for (int i = 0; i < info.length; i++) {
            System.out.println(info[i]);
        }
        String[] strings=teding(info,100);
        System.out.println(Arrays.toString(strings));
        System.out.println(Arrays.toString(teding(info,100)));


    }


//提取出特定
    public static String[] teding(goodInfo[] g,double p){
        String[] strings=new String[g.length];
        for (int i = 0; i < g.length; i++) {
            if (g[i].getFinal()>p){
                strings[i]=g[i].getName();
            }
        }
        return strings;
    }

}

class goodInfo{
    private int ID;
    private String name;
    private String type;
    private double price;
    private double discount;

    public goodInfo(){}

    public goodInfo(int ID, String name, String type, double price, double discount) {
        this.ID = ID;
        this.name = name;
        this.type = type;
        this.price = price;
        this.discount = discount;
    }

    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 String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getPrice() {
        return price+"元";
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getDiscount() {
        return discount;
    }

    public void setDiscount(double discount) {
        this.discount = discount;
    }

    @Override
    public String toString() {
        return "goodInfo{" +
                "ID=" + ID +
                ", name='" + name + '\'' +
                ", type='" + type + '\'' +
                ", price=" + price +
                ", discount=" + discount +
                '}';
    }


    public double getFinal(){
        return this.price+this.discount;
    }
}

自己的几个错误:
1、创建类数组时,对于类的new不熟悉,应该是 数组[i]= new 类名(……)
2、Arrays.toString 是对数组的打印进行辅助,既不能脱离sout使用,也不能打印二维的全部值。
3、普通类内的 .toString(),可以进行重构,从而当打印该类的对象,或者是该对象.tostring时,打印的就是 toString 重构的。

8.算法

算了,先不学,主要是要后面建网站,相关性不大。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值