JavaSE学习笔记(三)

API
jdk给我们提供了很多现成的类,这就是API。Java API就是一本字典
API文档:告诉我们有那些类,那些方法,各自有哪些功能,(说明书)

==================================================

引用类型的一般使用步骤:
1.导包
import 包路径.类名称;
如果需要使用的目标类,和当前类位于同一个包下,则可以省略包语句;
只有java.lang包下的内容不需要包,其他的包都需要导入
//比如String类,是java.lang包内,直接 String name=“你好”;不用导包

2.创建
类名称 对象名 = new 类名称();

3.使用
对象名.成员变量名();

====================================================
Scanner类
功能:实现键盘输入数据,到程序中

 package cn.itcast.day.demo0;
	 
	 import java.util.Scanner;//1.导包
	 
	 public class Demo0{
    
    
	 
	        public static void main(String[] args){
    
    
			          //2.创建
					  //System.in 代表从键盘输				
                Scanner sc = new Scanner(System.in);
				
				       //3.使用:获取键盘中的int数字
				int num = sc.nextInt();
				System.out.println("输入的int数字是:"+num);
				
				        //3.使用:获取键盘输入的字符串
				String str = sc.next();
				System.out.println("输入的字符串是:"+str);
				}

====================================================
匿名对象
创建对象的标准格式: 类名称 对象名 = new 类名称();
匿名对象: new 类名称();

注意事项:匿名对象只能使用唯一一次,下次再用还要创建
使用建议:如果确定有一个对象只需要使用一次,可以用

public class Demo{
    
    
      public static void main(String[] args){
    
    
	           //标准格式
			   Person one = new Person();
			   one.name = "高圆圆";
			   one.showName();//我叫高圆圆
			   System.out.println("===========================");
			   
			   //匿名对象
			   new Person().name = "赵又廷";
			   new Person().showName();//我叫null
			   }
			}

//一共有三个对象

匿名对象作为参数

........
Scanner sc = methodReturn();
int num = sc.nextInt();
System.out.println("输入的是:"+num);
}

public static void methodParam(Scanner sc){
    
    
            int num = sc.nextInt();
			}
public static Scanner methodReturn(){
    
    
              return new SCanner(System.in);
			  }

============================================================
Random 类
生成随机数: Random r=new Random() 没有参数,生成整个int中的随机数;
Random r=new Random(3) 有参数,生成[0,3)中的

随机int数;
package mod.two;
//导包

import java.util.Random;

public class Ram {
    
    
    public static void main(String[] args){
    
    
        Random r=new Random();
        int num = r.nextInt();
        System.out.println("随机数是:"+num);

    }

}	

====================================================================
对象数组

public static void main(String[] args){
    
    
        Person[] arrary=new Person[3];//创建存Person类对应对象的数组

Person one=new Person("迪丽热巴",20);
        Person two=new Person("古力娜扎",21);
        Person three=new Person("马尔扎哈",23);

        arrary[0]=one;
        arrary[1]=two;
        arrary[2]=three;

        System.out.println(arrary[0]);//地址值
        System.out.println(arrary[1]);
        System.out.println(arrary[2]);

        System.out.println(arrary[0].getName()+":"+arrary[0].getAge());

    }

=========================================================

ArraryList类
长度可以改变,E:泛型

import java.util.ArrayList;

public class Arrarylist {
    
    
    public static void main(String[] args){
    
    
        ArrayList<String> list = new ArrayList<>();
        System.out.println(list);//打印【】,不打印地址值
        list.add("古力娜扎");     //增加内容
        list.add("欧阳娜娜");
        list.add("马尔扎哈");
        System.out.println(list);
    }
}

==========================================================
ArraryList常用方法:

public boolean add(E e); //向集合中添加元素
public E get (int index);//获取元素,返回值就是对应元素
public E remove (int index);//删除元素,返回值就是被删除的元素;
public int size();//获取集合的尺寸长度,返回值包括元素的个数

import java.util.ArrayList;

public class Arrarylist1 {
    
    
    public static void main(String[] args){
    
    
        ArrayList<String> list = new ArrayList<>();

        boolean a=list.add("高圆圆");
        System.out.println(a);
        list.add("刘甜甜");
        list.add("王露露");
        list.add("刘婷婷");
        System.out.println(list);
        System.out.println("==========================");


        System.out.println("第一个人是:"+list.get(0));
        System.out.println("第二个人是:"+list.get(1));
        System.out.println("第三个人是:"+list.get(2));

        String b = list.remove(2);//删除第二个人;
        System.out.println(b);

        System.out.println(list);
        System.out.println(list.size());

    }
}

============================================================

ArraryList类 不能直接存储基本类型,必须使用基本类型对应的包装类

基本类型 包装类(引用类型,包装类型位于java.lang包下)
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

注意:JDK 1.5+开始,支持自动装箱、自动拆箱
自动装箱:基本类型–>包装类型(引用类型);
自动拆箱:包装类型(包装类型)–>基本类型;

import java.util.ArrayList;

public class IntArraryLins {
    
    
    public static void main(String[] args){
    
    
        ArrayList<Integer> listA=new ArrayList<>();
        listA.add(100);
        listA.add(200);
       System.out.println(listA);
        int A = listA.get(1);
        System.out.println(A);
    }
}

=====================================================
随机1-20的随机数,并存储

import java.util.ArrayList;
import java.util.Random;

public class Randomint {
    
    
    public static void main(String[] args){
    
    
	
        ArrayList<Integer> List = new ArrayList<>();// 初始化存储空间
        Random r= new Random();   //初始化随机数

		
        for(int i=1;i<=6;i++){
    
    
            int num = r.nextInt(20)+1;      //产生随机数

             List.add(num);
        }
        System.out.println(List);

    }
}

-=========================================
字符串
1.字符串的内容用不可变(常量) 可以共享使用
2.效果上相当于字符数组char[]字符数组,但是底层原理是byte【】字节数组

创建字符串
1.public String(); 创建一个空白字符串,不含任何内容
2.public String (char[] array); 根据字符数组内容,来创建相应数组
3.public String(byte[] array);根据字节数组内容,来创建相应字符串
4.String str = “Hello”;

public class chuang {
    
    
    public static void main(String[] args) {
    
    
        String str1=new String();
        System.out.println(str1);

        char[] array={
    
    'a','b','c'};
        String str2=new String(array);
        System.out.println(str2);

        byte[] arra = {
    
    97,98,99};
        String str3=new String(arra);
        System.out.println(str3);
		
		String str4 = "hello";
		Syrtem.out.println(str4);
		}
}

==================================================
字符串常量池
程序直接写上的双引号字符串,就在常量池中。

对于基本类型:==是数值的比较
对于引用类型:==是地址值的比较

public class Stringpool {
    
    
    public static void main(String[] args) {
    
    
        String str1="abc";
        String str2="abc";

        char[] str={
    
    'a','b','c'};
        String str3 = new String(str);

        System.out.println(str1==str2);//地址值相同
        System.out.println(str1==str3);
        System.out.println(str2==str3);
    }
}

在这里插入图片描述

===========================================
字符串比较是否相同
== 是进行对象地址值比较
比较:
1.public boolean equals(Object obj);参数是任何对象,只有是字符串的时候 才会返回ture/false
备注:Object可以接受任何对象

2.public boolean equalsIgnoreCase(tring str);忽略大小写,进行内容比较

public class Ayyayequal {
    
    
    public static void main(String[] args) {
    
    
        String str1="hello";
        String str2="hello";

        char[] array={
    
    'h','e','l','l','o'};
        String str3=new String(array);


        System.out.println(str1.equals(str2));
        System.out.println(str1.equals(str3));
        System.out.println(str2.equals(str3));
		 System.out.println(str2.equals("Hello"));//推荐,如果str2=Null,报错
		  System.out.println("Hello.equals(str3));//推荐,如果str2=null,不报错
    }
}



=====================================
字符串获取
1.public int Length();//获取字符串中含有的字符个数,拿到字符串长度
2.public String concat(String str);//拼接
3.public char charAt(int index);获取索引位置的单个字符

4.public int index0f(String str);查找参数字符串在本字符串中首次出现的位置

public class Stringget {
    
    
    public static void main(String[] args) {
    
    
        String str1="hello";
        String str2="world";
        String str3=str1.concat(str2);//链接
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
        System.out.println("=====================");

        System.out.println("huycftfytdtydftfyuiu".length());//str1.length(),字符串的长度
        int num=str1.length();//字符串长度
        System.out.println(num);

        System.out.println(str1.charAt(1)); //第一个位置是什么

        System.out.println(str1.lastIndexOf("llo"));//在哪个位置第一次出现llo


    }
}

===============================================
字符串截取
方法:
public String substring(int index);截止从参数位置一直到末尾
public String substring(int index,int end);截取相应位置

public class Stringcut {
    
    
    public static void main(String[] args) {
    
    
        String str1 = "asdfghjk";
        String str2=str1.substring(2);//dfghjk
        System.out.println(str2);
        String str3=str1.substring(3,5);//fg
        System.out.println(str3);

    }
}

String str =“hello”;
str=“java”;
str保存的是地址值,上面保存的是hello的0x999
下面保存的是java的0x666

===========================================================

String 当中与转换相关的常用方法:

public char[] toCharArray(); 将当前字符串拆分为字符数组作为返回值
Public byte[] getBytes();获得当前字符串底层的字节数组
public String replace(CharSequence oldString,CharSequence newString)
将所有出现的老字符串替换成为新字符串
备注:CharSequence意思是可以字符串

char[] array = "hello".toCharArray();

byte[] bytes = "abc".getBytes();

String str2 = str1.replace("o","*");

=========================================================================

字符串切割成多个字符串
方法:
public String [] split(String regex);按照参数规则,将字符串切分为若干不符

注意事项:split 方法的参数是一个正则表达式
注意:如果按照英文句点切割,"\."

public class Stringsplit {
    
    
    public static void main(String[] args) {
    
    
        String str1="hello,world.jidh ferf";
        String [] arrary=str1.split(",");
        for(int i=0;i<arrary.length;i++){
    
            // hello
                                                 //world.jidh ferf
            System.out.println(arrary[i]);
        }

 
        String [] arrar=str1.split(" ");          //hello,world.jidh
        for(int i=0;i<arrary.length;i++){
    
             //ferf
            System.out.println(arrar[i]);
        }



        String [] arra=str1.split("\\.");
        for(int i=0;i<arrary.length;i++){
    
              //hello,world
            System.out.println(arra[i]);           //jidh ferf
        }

    }
}

=====================================================
拼接字符串
定义一个方法,把数组{1,2,3}按照指定格式拼接成[word1#word2#word3]

public class Stringpinjie {
    
    
    public static void main(String[] args) {
    
    
        int [] arrary = {
    
    1,2,3};
        String str = "[";
        for(int i=0;i<arrary.length;i++){
    
    
            str+="word"+arrary[i]+"#";
            if(i==arrary.length-1){
    
    
                str+="]";
            }
        }
        System.out.println(str);
    }
}

===============================================
题目:输入一个字符串,并且统计其中各种字符串的次数
种类:大写字母 小写字母 数字 其他

思路:
1.键盘输入:Scanner
2.输入的是字符串,那么 String str= sc.next()
3.定义四个变量,代表四种字符出现的次数
4.需要对字符串一个字一个字检查,string–>char[],方法就是:toCharArray
5.遍历字符数组,++相应数
6.打印四个变量;

import java.util.Scanner;

public class Stringfind {
    
    
    public static void main(String[] args) {
    
    
	
        Scanner sc = new Scanner(System.in);      //Scanner类,键盘输入

        String str = sc.next();                   //利用Scanner输入字符串

        char[] arrary = str.toCharArray();        //利用toCharArray()方法,将字符串转化为char型数组
        int num = arrary.length;

        int D=0,X=0,S=0,Q=0;

        for(int i=0;i<num;i++){
    
                       //判断并统计

            if(arrary[i]>='A'&&arrary[i]<='Z')
                D++;
            else if(arrary[i]>='a'&&arrary[i]<='z')
                X++;
            else if(arrary[i]>='0'&&arrary[i]<='9')
                S++;
            else
                Q++;
        }

        System.out.println("大写字母这些个:"+D+",小写字母有这些个:"+X+",数字有这些个:"+S+",其它字符有这些个:"+Q); //打印结果
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_45511599/article/details/108586395