• Java学习笔记(十五)


    在完成对C语言的学习后,我最近开始了对C++和Java的学习,目前跟着视频学习了一些语法,也跟着敲了一些代码,有了一定的掌握程度。现在将跟着视频做的笔记进行整理。本篇博客是整理Java知识点的第十五篇博客。

    本篇博客介绍了Java的Object类,Arrays工具类和基本类型包装类。

    本系列博客所有Java代码都使用IntelliJ IDEA编译运行,版本为2022.1。所用JDK版本为JDK11

    目录

    Object

    Object类的toString方法

    Object类的equals方法

    Arrays

    Arrays类的概述和常用方法

    冒泡排序

    基本类型包装类

    基本类型包装类概述

    Integer类的概述和使用

    int和String的相互转换

    字符串中数组排序

    自动装箱和拆箱


    Object

    所有类都直接或间接继承自Object类。Object类的构造方法是:

    public Object()

    Object类的toString方法

    public String toString()返回对象的字符串表示形式,子类可以重写此方法。

    1. public class object1 {
    2. String name;
    3. int age;
    4. public object1(){};
    5. public object1(String name,int age){
    6. this.name = name;
    7. this.age = age;
    8. }
    9. public void setname(String name){
    10. this.name = name;
    11. }
    12. public String getname(){
    13. return name;
    14. }
    15. public void setage(int age){
    16. this.age = age;
    17. }
    18. public int getage(){
    19. return age;
    20. }
    21. public String toString(){
    22. return "The name is " + name + " and the age is " + age ;
    23. }
    24. }

    object1类重写了toString方法,返回关于本类成员的一些信息的字符串。

    1. public class objecttest1 {
    2. public static void main(String[] args){
    3. object1 obj = new object1();
    4. obj.setname("Kevin");
    5. obj.setage(18);
    6. System.out.println(obj.toString());
    7. }
    8. }

    创建了object1类的对象obj,调用了其toString方法,并将其结果输出。

    程序的输出是:

    The name is Kevin and the age is 18

    Object类的equals方法

    public boolean equals(Object obj)比较两个对象是否相等。默认比较地址,可以通过重写改为比较内容。

    1. public class object2 {
    2. String name;
    3. int age;
    4. public object2(){};
    5. public object2(String name,int age){
    6. this.name = name;
    7. this.age = age;
    8. }
    9. public void setname(String name){
    10. this.name = name;
    11. }
    12. public String getname(){
    13. return name;
    14. }
    15. public void setage(int age){
    16. this.age = age;
    17. }
    18. public int getage(){
    19. return age;
    20. }
    21. public boolean equals(Object o) {
    22. if (this == o) return true;
    23. if (o == null || getClass() != o.getClass()) return false;
    24. object2 object2 = (object2) o;
    25. return age == object2.age && name.equals(object2.name);
    26. }
    27. }

    object2类重写了equals方法。

    1. public class objecttest2 {
    2. public static void main(String[] args){
    3. object2 obj1 = new object2();
    4. obj1.setname("Laura");
    5. obj1.setage(14);
    6. object2 obj2 = new object2("Laura",14);
    7. System.out.println(obj1.equals(obj2));
    8. }
    9. }

    创建了两个object2类对象,并进行比较。程序的输出是:

    true
     

    Arrays

    Arrays类的概述和常用方法

    Arrays类包含用于操作数组的一些方法。

    public static String toString(int[] a)返回指定数组内容的字符串表示。

    public static void sort(int[] a)按照数字顺序排列指定的数组,默认升序。

    使用Arrays类需要导包,import java.util.Arrays

    1. import java.util.Arrays;
    2. public class array {
    3. public static void main(String[] args){
    4. int arr[] = new int[]{24, 69, 80, 57, 14};
    5. System.out.println(Arrays.toString(arr));
    6. Arrays.sort(arr);
    7. System.out.println(Arrays.toString(arr));
    8. }
    9. }

    程序创建了一个数组,并调用了上面两个方法。程序的输出是:

    [24, 69, 80, 57, 14]
    [14, 24, 57, 69, 80]

    冒泡排序

    冒泡排序是排序的一种算法,下面程序使用冒泡排序进行排序。

    1. public class bubble {
    2. public static void main(String[] args){
    3. int[] num = new int[]{24, 69, 80, 57, 13};
    4. int i,j;
    5. for(i = 0;i < num.length - 1;i += 1){
    6. for(j = 0; j 1;j += 1){
    7. if(num[j] > num[j + 1]){
    8. int temp = num[j];
    9. num[j] = num[j + 1];
    10. num[j + 1] = temp;
    11. }
    12. }
    13. }
    14. for(i = 0; i < num.length; i += 1){
    15. System.out.print(num[i] + " ");
    16. }
    17. System.out.println();
    18. }
    19. }

    程序的输出是:

    13   24   57   69   80   
     

    基本类型包装类

    基本类型包装类概述

    将基本类型封装成对象,可以在对象中定义更多的功能方法操作该数据。最常用的操作之一是用于基本数据类型和字符串之间的转换。

    byte对应的包装类为Byte,short类对应的包装类为Short,int类对应的包装类为Integer,long类的包装类为Long,float类的包装类为Float,double类的包装类为Double,char类的包装类为Character,boolean类的包装类为Boolean

    下面的内容以Integer类为例,其他包装类大同小异。

    Integer类的概述和使用

    Integer用于包装一个对象中原始类型int的值。

    public static Integer valueOf(int i)返回指定的int值的Integer实例。

    public static Integer valueOf(String s)返回一个保存指定值的String对象的Integer实例。

    1. public class integertest {
    2. public static void main(String[] args){
    3. Integer i1 = Integer.valueOf(100);
    4. System.out.println(i1);
    5. Integer i2 = Integer.valueOf("100");
    6. System.out.println(i2);
    7. }
    8. }

    这段程序利用了这两个方法。程序的输出是:

    100
    100

    int和String的相互转换

    基本类型包装类最常见的操作是用于基本类型和字符串的转换。

    public static String valueOf(int i)返回int参数的字符串表示形式,是String类中的方法。

    public static int parseInt(String s)将字符串解析为int类型,是Integer类中的方法。

    1. public class intstring {
    2. public static void main(String[] args){
    3. int number = 100;
    4. String s1 = "" + number;
    5. System.out.println(s1);
    6. String s2 = String.valueOf(number);
    7. System.out.println(s2);
    8. String s3 = "100";
    9. Integer i = Integer.valueOf(s3);
    10. int x = i.intValue();
    11. System.out.println(x);
    12. int y = Integer.parseInt(s3);
    13. System.out.println(y);
    14. }
    15. }

    程序的输出是:

    100
    100
    100
    100

    字符串中数组排序

    1. import java.util.Arrays;
    2. public class sortstring {
    3. public static void main(String[] args){
    4. String str = "91 27 46 38 50";
    5. String[] strs = str.split(" ");
    6. int i;
    7. int arr[] = new int[strs.length];
    8. for(i = 0;i < strs.length; i += 1){
    9. arr[i] = Integer.parseInt(strs[i]);
    10. }
    11. Arrays.sort(arr);
    12. StringBuilder sb = new StringBuilder();
    13. for(i = 0;i < arr.length; i += 1){
    14. sb.append(arr[i]);
    15. if(i < arr.length - 1){
    16. sb.append(" ");
    17. }
    18. }
    19. String s = sb.toString();
    20. System.out.println(s);
    21. }
    22. }

    程序创建了一个字符串str,内部是空格分隔的数字。随后将str按空格打碎,用字符串数组strs接收。随后遍历strs,将每个成员转换为int,放在数组arr里,然后将arr排序。然后通过StringBuilder创建了一个字符串并输出。程序的输出是:

    27 38 46 50 91

    自动装箱和拆箱

    装箱就是把基本数据类型转换为对应的包装类型。拆箱就是把包装类类型转换为对应的基本数据类型。JDK5后引入了自动装箱和拆箱。

    1. public class box {
    2. public static void main(String[] args){
    3. int number = 15;
    4. Integer i1 = Integer.valueOf(number);
    5. System.out.println(i1);
    6. Integer i2 = number;
    7. System.out.println(i2);
    8. int num1 = i2.intValue();
    9. System.out.println(num1);
    10. int num2 = i2;
    11. System.out.println(i2);
    12. }
    13. }

    程序中只使用了等号就完成了装箱和拆箱。程序的输出是:

    15
    15
    15
    15

  • 相关阅读:
    【笔试强训选择题】Day43.习题(错题)解析
    PMP考前知识点口诀
    HashSet的存储机制
    梦开始的地方——C语言柔性数组
    C#,图论与图算法,图着色问题(Graph Coloring)的威尔士-鲍威尔(Welch Powell Algorithm)算法与源代码
    Spring Cloud Gateway 集成Sa-Token
    Java 入门练习:1 - 5
    数据还可以刷新但是显示不到页面上了
    Python中的协程、线程和进程
    我的创作纪念日—小梁说代码
  • 原文地址:https://blog.csdn.net/m0_71007572/article/details/126335696