JAVA自学笔记(3)

JAVA的心动之旅

Day1 字符串String

1.0 字符串的特点以及创建一个字符串

public class Practice {//构建字符串的3+1种方法
    public static void main(String[] args) {
        //第一种
        String one=new String();
        System.out.println("输出的字符串为:"+one);
        //第二种
        char str[]={'A','B','C'};
        String two=new String(str);
        System.out.println("输出的字符串为:"+two);
        //第三种
        byte []bytes={97,98,99,100};
        String three=new String(bytes);
        System.out.println("输出的字符串为:"+three);
        //+1种 直接构造 字符串常量不可改变 
        String four="HelloWorld";
        System.out.println("输出的字符串为:"+four);
        
    }
}

打印结果:

输出的字符串为:
输出的字符串为:ABC
输出的字符串为:abcd
输出的字符串为:HelloWorld

2.0 字符串的常量池

 3.0 字符串的获取方法

public class Practice {
    public static void main(String[] args) {
     String one="Hello";
     System.out.println("字符串的长度为:"+one.length());
     String two="say ".concat(one);
     System.out.println("字符串为:"+two);
     for(int i=0;i<two.length();i++)
     {
         char ch=two.charAt(i);
         System.out.print(ch+" ");
     }
      String three="o";
      System.out.println("\n"+one.indexOf(three));
      String four="h";
      System.out.println(one.indexOf(four));
    }
}

打印结果

扫描二维码关注公众号,回复: 11268380 查看本文章

字符串的长度为:5
字符串为:say Hello
s a y H e l l o
4
-1

4.0 字符串的比较 ==为地址的比较

5.0 字符串的截取

public class Practice {
    public static void main(String[] args) {
     String one="hellobts";
     String two=one.substring(5);
     System.out.println(two);
     String three=one.substring(0, 5);
     System.out.println(three);
    }
}

打印结果:

bts
hello

6.0 字符串的转化方法

public class Practice {
    public static void main(String[] args) {
     String one="hellobts";
     char a[]=one.toCharArray();
     for(int i=0;i<a.length;i++)
     {
         System.out.print(a[i]+" ");
     }
     System.out.println();
     byte []bytes=one.getBytes();
     for(int i=0;i<bytes.length;i++)
     {
         System.out.print(bytes[i]+" ");
     }
     System.out.println();
     String two=one.replace("hello", "love");
     System.out.println(two);
    }
}

打印结果:

h e l l o b t s
104 101 108 108 111 98 116 115
lovebts

Day2 static 关键字

1.0 static 概述

一旦用了static 关键字,那么这样的内容不再属于对象自己,它是属于类的,所以凡是本类的对象,都共享一份。

2.0 static 修饰成员变量 

public class Students {
     private static int idcounter=0;//每当创建一个对象(new)计数器++
      static String room;
     private String name;
     private int age;
     private int id;
     public Students() {
        this.id=++idcounter;
         }
     public Students(String name, int age) {
        this.name = name;
        this.age = age;
        this.id=++idcounter;
    }
    
    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 int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
}
public class Practice {
    public static void main(String[] args) {
    Students one=new Students("田小娟",22);
    one.room="101教室";
    Students two=new Students("徐穗珍",22);
    System.out.println("姓名:"+one.getName()+" "+"年龄:"+one.getAge()+" 教室:"+one.room+
" 学号"+one.getId()); System.out.println("姓名:"+two.getName()+" "+"年龄:"+two.getAge()+" 教室:"+two.room+
" 学号"+two.getId()); } }

 3.0 static 修饰方法

4.0 静态代码块

Day3 与Arrays相识

1.0 简单的应用

import java.util.Arrays;

public class Practice {
    public static void main(String[] args) {
        int []num={3,89,45,235,43,79};
        String str=Arrays.toString(num);
        System.out.println(str);
        Arrays.sort(num);
        for(int i=0;i<num.length;i++)
        {
            System.out.print(num[i]+" ");
        }
    }
}

打印结果:

[3, 89, 45, 235, 43, 79]
3 43 45 79 89 235

2.0 字符串倒序

import java.util.Arrays;

public class Practice {
    public static void main(String[] args) {
        String str="aihfjsdfhuwefwnf";
        //定义一个随机的字符串,并将字符串排序后倒序输出
        //需将字符串先转化为字符数组,才能使用Arrays
        char []chars=str.toCharArray();
        Arrays.sort(chars);
        for(int i=chars.length-1;i>=0;i--)
        {
            System.out.print(chars[i]+" ");//w w u s n j i h h f f f f e d a 
        }
        
    }
}

 Math类方法(百度)

猜你喜欢

转载自www.cnblogs.com/mzq-/p/12952229.html