Java学习_Day 07(学习内容:狂神说JAVA零基础2P59-3P63)

2P59 稀疏数组

数组大部分元素为0,或者为同一值得数组时,可以使用稀疏数组来保存该数组

package com.hu.array;

import java.util.Arrays;

public class P59 {
    public static void main(String[] args) {
        // 创建一个二维数组11*11   0:没有棋子  1:黑棋  2:白棋
        int[][] array = new int[11][11];
        array[1][2] = 1;
        array[2][3] = 2;
        System.out.println("原始数组:");
        for (int[] ints : array) {
            for (int anInt : ints){
                System.out.print(anInt + " ");
            }
            System.out.println();

        }
        // 转换为稀疏数组
        // 1.获取有效值的个数
        int sum = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (array[i][j] != 0){
                    sum++;
                }

            }

        }
        System.out.println("有效值个数:" + sum);

        // 2.创建一个稀疏数组的数组
        int[][] array1 = new int[sum + 1][3];

        array1[0][0] = 11;
        array1[0][1] = 11;
        array1[0][2] = 2;

        // 遍历二维数组,将非0存放于稀疏数组中
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                if (array[i][j] != 0){
                    count ++;
                    array1[count][0] = i;
                    array1[count][1] = j;
                    array1[count][2] = array[i][j];
                }
            }
            
        }
        // 输出稀疏数组
        System.out.println("稀疏数组为:");
        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                System.out.print(array1[i][j] + "\t");
            }
            System.out.println();

        }

        System.out.println("===================");
        System.out.println("还原数组");
        // 1.读取稀疏数组
        int[][] array2 = new int[array1[0][0]][array1[0][1]];

        array2[array1[1][0]][array1[1][1]] = array1[1][2];
        array2[array1[2][0]][array1[2][1]] = array1[2][2];
        // 2.还原
        System.out.println("还原数组为:");
        for (int[] ints : array2) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();
        }
        
    }
}

3P60 什么是面向对象

属性 + 方法 就是一个类

面向过程 & 面向对象

  • 核心:以类的方式组织代码,以对象的组织(封装)数据。
  • 三大特性:封装、继承和多态
  • 抽象:抽象成一个类

对象是具体的事物,类是对象的抽象。

3P61 回顾方法的定义

package com.oop;

import java.io.IOException;

// 主类
public class P61 {
    // main方法
    public static void main(String[] args) {

    }

    /*
    修饰符 返回值类型 方法名()
        方法体
        返回值
     */
    public String sayHello(){
        return "Hello,world";
    }
    public void hello(){
        return;
    }

    public int max(int a, int b){
        // 三元运算符
        return a > b ? a : b;
    }

    // 数组下标越界异常
    public void readFile(String file) throws IOException{

    }
}

3P62 回顾方法的调用

package com.oop;

public class P62 {
    // 静态方法
    // 非静态方法
    public static void main(String[] args) {
        // 非静态方法需要实例化类
        // 对象类型  对象名 = 对象值
        P62_student p62_student = new P62_student();
        p62_student.say();
    }
    // static是和类一起加载的
    public static void a(){
        // b();
    }
    // 这个方法是在实例化之后才存在
    public void b(){

    }

}
package com.oop;

public class P62_student {
    // 非静态方法
    public void say(){
        System.out.println("学生说话了");
    }
}
package com.oop;

public class P62_1 {
    // 实际参数和形式参数一一对应
    public static void main(String[] args) {
        int add = P62_1.add(1, 2);
        System.out.println(add);
    }
    public static int add(int a, int b){
        return a + b;
    }
}
package com.oop;

public class P62_2 {
    public static void main(String[] args) {
        // 值传递
        int a = 1;
        System.out.println(a); // 1
        P62_2.change(a);
        System.out.println(a);
    }
    public static void change(int a){
        // 这里的形参相当于一个临时变量,当返回后,才会把形参的值赋给实参
        a = 10;
    }
}
package com.oop;

public class P62_3 {
    public static void main(String[] args) {
        // 引用传递:对象,本质还是值传递
        Student student = new Student();
        System.out.println(student.name); //null
        P62_3.change(student);
        System.out.println(student.name);

    }
    public static void change(Student student){
        student.name = "Rye";
    }

}

// 一个java文件里只能有一个public类
// Student类里有一个属性name
class Student{
    String name;
}

3P63 类与对象的创建

package com.oop;
// 一个项目只有一个main方法
public class P63_main {
    public static void main(String[] args) {
        // 类是抽象的,需要实例化
        // 类实例化会返回一个具体对象
        P63_student jack = new P63_student();
        P63_student lucky = new P63_student();
        jack.name = "jack";
        jack.age = 18;
        System.out.println(jack.name);
        System.out.println(jack.age);

        lucky.name = "lucky";
        lucky.age = 16;
        System.out.println(lucky.name);
        System.out.println(lucky.age);
    }
}
package com.oop;

public class P63_student {
    // 属性 抽象概念不应初始化
    // 类是对象的模板
    String name;
    int age;

    // 方法
    public void study(){
        System.out.println(this.name + "在学习哦");
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值