Simple use of Java study notes 10String to find duplicates

Use the record value type declaration using
the rercord extends java.lang.Record class

public record User(int id,String name){

}

Comparison of String running results

		String s1 = "ok";
        String s2 = "ok";
        System.out.println(s1 == s2);

        String b1 = new String("ok");
        String b2 = new String("ok");
        System.out.println(b1 == b2);
        System.out.println(b1.equals(b2));

        //字符数
        System.out.println("hello中国".length());
        //字节数 utf-8 一个占用三个字节
        System.out.println("hello中国".getBytes().length);
        System.out.println(b1.concat(b2));

The running results are as follows. If
insert image description here
you declare directly, you specify the address. Although the same string is generated when new, the address is different, so == is false when judging

Write a program to output the maximum

public int max1(int... a) {
    
    
        Arrays.sort(a);
        return a.length == 0 ? 0 : a[a.length - 1];
    }

In the java language, String is a fixed length

A Chinese character under utf-8 encoding occupies 3 bytes.

Implement character reverse order

public String ni(String a) {
    
    
        int b = a.length();
        //String[] c2 = a.split("", b);
        char[] c2 = a.toCharArray();
        String outcome = "";
        for (int i = b - 1; i >= 0; i--) {
    
    
            //outcome += c2[i];
            outcome = outcome.concat(String.valueOf(c2[i]));
        }
        return outcome;
    }

Determine the number of occurrences of a character or the number of occurrences of a string

public int frequency(String text, String fre) {
    
    
        char[] at1 = text.toCharArray();
        char[] af1 = fre.toCharArray();
        int sum = 0;
        for (int i = 0; i < at1.length; i++) {
    
    
            boolean a2 = false;
            int a1 = 0;
            if (at1[i] == af1[0]) {
    
    
                if (i + af1.length <= at1.length) {
    
    
                    for (int j = 0; j < af1.length; j++) {
    
    
                        if (at1[i + j] == af1[j]) {
    
    
                            a1++;
                        }
                        if (a1 == af1.length) {
    
    
                            a2 = true;
                            break;
                        }
                    }
                }
            }
            if (a2) {
    
    
                sum++;
            }
        }
        return sum;
    }

Some conclusions about object-oriented

Inheritance extends final class is not allowed to be inherited, no subclasses.
Your parent class of the super class will be used in the case of rewriting.
Super() will also be used in the first line of the construction method of the subclass;

The abstract class abstract class C{}
interface interface uses the implementation class of the interface. If there is only one abstract method, you can use lambda expression to use it.
Interface = constant + abstract method... + static implementation method... + default implementation method... There
can be many interfaces inherit interface

Guess you like

Origin blog.csdn.net/xxxmou/article/details/129132596
Recommended