统计字符串中每一个不同的字符

import java.util.*;
//统计字符串每一个字符出现的字数
public class StringDemo{
public static void main(String[] args){
Scanner aScanner = new Scanner(System.in);
//让用户输入字符串
System.out.println("请输入你要统计的语句");
String aString = aScanner.next();
//要统计每一个字符,就要把字符串转换为字符,可以调用字符串的方法toCharArray
char[] aCharArray = aString.toCharArray();
//创建HashMap集合,让其存储键与值
HashMap<Character,Integer> aHashMap = new HashMap<>();
//遍历字符数组
for(Character key:aCharArray){
//把aChar作为键,先判断集合中有没有该键,若有则value加一,没有的话添加键进去
if(aHashMap.containsKey(key)){
Integer value =aHashMap.get(key);
value++;
//后来的值会取代前面的值,put方法把值添加进去,
aHashMap.put(key,value);
}else{
//不存在添加进去
aHashMap.put(key,1); }
}
//把key存储都Set集合中,遍历集合
Set<Character> aSet = aHashMap.keySet();
//遍历aHashMap
for(Character key : aSet){
Integer value = aHashMap.get(key);
System.out.println(key+"="+value);
} }
}

JDK9的新特性:

List接口,Set接口,Map接口里边增加了一个静态方法of,可以给集合一次性添加多个元素

使用前提:当集合中存储的元素的个数已经确定了,不再改变使用,也就是说,添加完元素之后,就不能再使用put方法来添加元素了

注意事项:

  1. of方法只适用于List接口,Set接口,Map接口,不适用于接口的实现类

  2. of方法的返回值是一个不能够改变的集合

  3. Set接口和Map接口在调用of方法适合,不能存放重复元素,否则会抛出异常

import java.util.*;
public class JDK9Demo{
public static void main(String[] args){
List<String> list = List.of("a","b","c","d","a");//可以重复元素
System.out.println(list); Set<String> set = Set.of("a","b","c");//不允许重复元素
System.out.println(set); Map<String,Integer> map = Map.of("张三",18,"李四",17,"王五",16,"赵三",18);//不允许重复元素,键不允许重复,值可以
System.out.println(map);
}
}

Debug追踪

Debug调试程序

​ 可以让代码逐行执行,查看代码执行的过程,调试程序中出现的bug

f8:逐行执行程序 f7:进入到方法中 shift+f8跳出方法

f9:跳到下一个断点 ctrl +f2 退出debug模式,停止程序

console:切换到控制台

斗地主案列

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List; public class DouDiZhu {
public static void main(String[] args){
//Integer表示的索引,String表示牌的大小
HashMap<Integer,String> poker = new HashMap<>();
//创建一个List集合,存储牌的索引,sort方法可以对索引进行排序
ArrayList<Integer> pokerIndex = new ArrayList<>();
//定义两个集合,存储花色和牌的序号
List<String> colors = List.of("♠","♥","♣","♦");
List<String> numbers = List.of("2","A","K","Q","J","10","9","8","7","6","5","4","3");
//把大小王存储到集合中
int index = 0;
poker.put(index,"大王");
pokerIndex.add(0);
index++;
poker.put(index,"小王");
pokerIndex.add(index);
index++;
//0索引代表着大王,1索引代表着小王,以此类推,到后面就可以对索引排序,也就是对牌排序
for(String number : numbers){
for(String color :colors){
String str = color+number;
poker.put(index,str);
//牌的索引也要添加,因为他代表着牌的大小,
pokerIndex.add(index);
index++;
}
}
//使用Collections中的方法shuffle();
Collections.shuffle(pokerIndex);
ArrayList<Integer> p1 = new ArrayList<>();
ArrayList<Integer> p2 = new ArrayList<>();
ArrayList<Integer> p3 = new ArrayList<>();
ArrayList<Integer> p4 = new ArrayList<>();
for(int i=0;i <pokerIndex.size();i++){
//先转为Integer类型,不然会添加失败
Integer in = pokerIndex.get(i);
if(i>=51){
p4.add(in);
}else if(i%3==0){
p1.add(in);
}else if(i%3==1){
p2.add(in);
}else if(i%3==2){
p3.add(in);
} }
//把分完的牌进行排序
Collections.sort(p1);
Collections.sort(p2);
Collections.sort(p3);
Collections.sort(p4);
lookPoker("刘德华",poker,p1);
lookPoker("周润发",poker,p2);
lookPoker("周星驰",poker,p3);
lookPoker("底牌",poker,p4);
}
public static void lookPoker(String name,HashMap<Integer,String> poker,ArrayList<Integer> list){
System.out.print(name+": ");
for(Integer key : list){
//通过索引获取值
String value = poker.get(key);
System.out.print(value+" ");
}
System.out.println();
}
}
异常

Throwable有两个子类Exception和Error

Error:严重错误error,无法通过处理的错误,只能实现避免,好比绝症

Exception:表示异常,异常产生后程序员可以通过代码的方式纠正,使程序继续运行,是必须要处理的

throw关键字可以在指定的方法里面抛出指定的异常对象

/*
throw关键字
作用:
可以使用throw关键字在指定的方法中抛出指定的异常
使用格式:
throw new xxxException("异常产生的原因");
注意:
1.throw关键字必须写在方法的内部
2.throw关键字后边new的对象必须是Exception或者Exception的子类对象
3.throw关键字抛出指定的异常对象,我们必须处理这个异常对象
throw关键字后边创建的是RuntimeException或者是RuntimeException的子类对象,我们可以不处理,默认交给jvm处理
throw关键字后边创建的是编译器异常,我们必须处理这个异常,
*/
public class ThrowDemo{ public static void main(String[] args){
int[] arr= new int[3];
int e = getElement(arr,-2);
System.out.println(e);
//抛出指定了异常
//Exception in thread "main" java.lang.NullPointerException: 传递的数组是空
//at ThrowDemo.getElement(ThrowDemo.java:24)
//at ThrowDemo.main(ThrowDemo.java:18)
}
//如果传递的参数不合法,那么我们就必须使用抛出异常的方式,告知方法的调用者,传递参数有问题
public static int getElement(int[] arr,int index){ if(index<0 || index>arr.length-1){
throw new ArrayIndexOutOfBoundsException("传递的索引不能超过范围或者低于范围");
}
if(arr == null){
//如果是空,就指定他为空指针异常
throw new NullPointerException("传递的数组是空");
}
int i = arr[index];
return i;
}
}

Objects的非空判断

Objects.requireNonNull();可以判断一个对象是否为null

import java.util.Objects;
public class ThrowDemo{ public static void main(String[] args){
method("云想衣裳花想容");//传递null将会返回NullPointerException异常
}
public static void method(Object obj){
Object objects=Objects.requireNonNull(obj);
System.out.println(objects);
}
}

throws关键字_异常处理的第一种方式

/*
throws关键字:当方法内部抛出异常对象的时候,那么我们就必须处理这个异常对象,可以使用thorws关键字来处理异常对象,会把异常对象声明抛出给方法的调用者处理 */
import java.io.FileNotFoundException;
public class ThrowsDemo{
public static void main(String[] args)throws FileNotFoundException{
readFile("d:\\a.txt");
}
public static void readFile(String fileName)throws FileNotFoundException{
//传递进来的文件不对就抛出异常
if(!fileName.equals("c:\\a.txt")){
throw new FileNotFoundException("传递的文件路径不对,请重新传递");
} System.out.println("传递路径成功");
}
}

try_catch异常处理的第二种方式

捕获异常语法格式
try{
编写可能会出现异常的代码
}catch(用来接收try中抛出的异常信息 异常类型 e){
处理异常代码
}
....
catch(){} 注意:
try中可能会抛出多个异常对象,那么久可以使用多个catch来处理这些异常对象
如果try中产生了异常,那么就会执行catch中的异常处理逻辑
import java.io.FileNotFoundException;
public class ThrowsDemo1{
public static void main(String[] args){
try{
readFile("d:\\a.txt");
}catch(FileNotFoundException e){
System.out.println("传递的文件不对");
}
System.out.println("后续代码");
}
public static void readFile(String fileName)throws FileNotFoundException{
//传递进来的文件不对就抛出异常
if(!fileName.equals("c:\\a.txt")){
throw new FileNotFoundException("传递的文件路径不对,请重新传递");
}
System.out.println("路径没有问题");
}
}

Throwable类中3个异常处理的方法

getMessage() :简短的描述

toString() :详细的消息字符串

printStackTrace():打印的异常信息最全面

字异常符类
public class Fu{
public void show1()throws NullPointerException,ClassCastException{}
public void show2()throws IndexOutOfBoundsException{}
public void show3()throws IndexOutOfBoundsException{} class Zi extens Fu{
//子类重写父类方法是,抛出和父类相同异常
public void show1()throws NullPointerException,ClassCastException{}
//子类重写父类方法时,抛出父类异常的子类
public void show2()throws ArrayIndexIndexOutOfBoundsException{}
//子类重写父类方法时,不抛出异常
public void show3(){}
//父类没有抛出异常,子类也不可以抛出异常,如若有异常,需要try catch处理异常

自定义异常

//自定义一个注册异常
//需要一个空参数的构造方法,一个带异常信息的构造方法
//继承Exception //那么自定义的异常类就是一个编译器异常,那就需要throws或者try...catch处理
//继承的是RuntimeException,那么异常无序处理,交给虚拟机 public class RegisterException extends Exception{
public RegisterException(){}
public RegisterException(String message){
super(message);
} }
import java.util.*;

//使用自定义异常类
public class ExceptionDemo{
//定义一个集合,存储姓名
public static void main(String[] args){
ArrayList<String> list = new ArrayList<>();
list.add("詹姆斯");
list.add("科比");
list.add("乔丹");
list.add("诺维斯基");
list.add("邓肯");
System.out.println(list);
//让用户输入昵称
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的昵称");
String nicheng = sc.next();
//向里面添加姓名,如果不存在则添加成功,如果存在则返回注册异常
for(String name : list){
//使用字符串的方法equals判断集合是否存在元素,将会返回一个boolean
//为真,就表示昵称已经存在,需要返回一个注册异常
if(name.equals(nicheng)){
try{
throw new RegisterException("亲,该昵称已经被注册过了");
}catch(RegisterException e){
e.printStackTrace();
return;//一旦放生异常,立马结束方法
}
}
//遍历完了,如果没有发生异常,就表示昵称可以创建 }
list.add(nicheng);
System.out.println("恭喜您,注册成功");
}
}
多线程
public class ThreadDemo{
public static void main(String[] args){
MyThread mt = new MyThread();
//执行start方法,开启线程,执行run方法
//如果使用mt.run();的话程序会在堆内存中执行,那就不是多行程了,而是单线程
//每一次使用start();都会开辟一条新的线程,在栈空间里面执行run方法,多行程的好处就是线程之间互相不影响,cup喜欢哪一个线程就执行哪一个线程
mt.start();
for(int i= 0;i<10;i++){
System.out.println("main线程"+i);
}
}
}
public class MyThread extends Thread{
//重写run方法
public void run(){
for(int i=0;i<10;i++){
System.out.println("run线程"+i);
}
}
}

Thread类的常用方法

获取线程名称的方法

1.使用Thread类中的方法getName
String getName() 返回该线程的名称,那条线程调用我,我就返其的名称
2.可以先获取到当前正在执行的线程,然后使用线程中的方法getName()获取线程的名称
public class MyThread1 extends Thread{
public void run(){
/*第一种获取名字的方式
String name=getName();
System.out.println(name);*/
// 第二种获取名字的方式
//先获取当前线程(currentThread是一个静态方法,可以通过类名直接获取线程)
Thread t = Thread.currentThread();//这个方法将会返回当前的线程
//通过线程获取名字
String naem =t.getName();
System.out.println(name);
}
}
public class ThreadName{
public static void main(String[] args){
MyThread1 mt = new MyThread1();
mt.start();
}
}

设置线程名称

一种通过setName方法,一种通过构造方法来设置

public class ThreadName{
public static void main(String[] args){
MyThread1 mt = new MyThread1();
mt.setName("小强");
mt.start();
new MyThread1("旺财").start();
}
}
public class MyThread1 extends Thread{
public MyThread1(){}
public MyThread1(String name){
super(name);
}
public void run(){
/*第一种获取名字的方式
String name=getName();
System.out.println(name);*/
// 第二种获取名字的方式
//先获取当前线程
Thread t = Thread.currentThread();
//通过线程获取名字
String name =t.getName();
System.out.println(name);
}
}

sleep方法

是一个静态方法,使用类名就可以直接调用,让当前线程睡眠指定时间

创建多线程程序的第二种方式_实现Runnable接口

实现步骤:

1.创建一个Runnable接口的实现类

  1. 在实现类中重写Runnable接口的run方法,设置线程任务

  2. 创建一个Runnable接口的实现类对象

  3. 创建Thread类对象,构造方法中传递Runnable接口的实现类对象

  4. 调用Thread类的start方法,开启线程

    public class RunnableImp implements Runnable{
    public void run(){
    for(int i=0;i<10;i++){
    //获取当前的线程,然后获取线程名字
    System.out.println(Thread.currentThread().getName()+"--"+i);
    }
    }
    }
    public class RunnableDemo {
    public static void main(String[] args){
    //创建Runnable接口实现类对象
    RunnableImp run = new RunnableImp();
    //Thread中有个构造方法可以接Runnable接口作为参数传递
    new Thread(run).start(); for(int i=0;i<10;i++){
    //想要获取main方法的线程,只能够用下面的方式获取,然后获取线程名字
    System.out.println(Thread.currentThread().getName()+"--"+i);
    }
    }
    }

笔记-迎难而上之Java基础进阶3的更多相关文章

  1. 笔记-迎难而上之Java基础进阶1

    Collection集合 数组的长度是固定的,集合的长度是可变的 数组中存储的是同一类型的元素,可以存储基本数据类型值.集合存储的都是对象.而且对象的类型可以不一致. 集合框架 import java ...

  2. 笔记-迎难而上之Java基础进阶-终

    使用Stream流的方式,遍历集合 import java.util.*; public class StreamDemo{ public static void main(String[] args ...

  3. 笔记-迎难而上之Java基础进阶8

    函数式接口 函数式接口:有且只有一个抽象方法的接口,(可以包含其他默认,静态,私有方法) //函数式接口的使用,创建一个函数式接口 public interface FunctionalInterfa ...

  4. 笔记-迎难而上之Java基础进阶7

    序列化流 把对象以流的方式写入到文件中保存,叫做对象的序列化 把文件中保存的对象,以流的方式读取出来,叫做对象大反序列化 对象的序列化流_ObjectOutputtream继承自OutputStrea ...

  5. 笔记-迎难而上之Java基础进阶2

    Set集合 import java.util.*; public class HashSetDemo{ public static void main(String[] args){ //Set接口的 ...

  6. 笔记-迎难而上之Java基础进阶6

    import java.io.*; public class InputStreamDemo{ public static void main(String[] args) throws IOExce ...

  7. 笔记-迎难而上之Java基础进阶5

    Lambda表达式无参数无返回值的练习 //定义一个接口 public interface Cook{ public abstract void makeFood(); } public class ...

  8. 笔记-迎难而上之Java基础进阶4

    内部类创建多线程 //匿名内部类创建多线程 public class InnerClassThread{ public static void main(String[] args){ //回忆一下之 ...

  9. 第二十八节:Java基础-进阶继承,抽象类,接口

    前言 Java基础-进阶继承,抽象类,接口 进阶继承 class Stu { int age = 1; } class Stuo extends Stu { int agee = 2; } class ...

随机推荐

  1. 使用git连接到Github

    直奔主题,使用git连接到Github步骤如下: 1. 安装git yum install git 或者 sudo get-apt install git git-core 2. 全局配置 git c ...

  2. GPS NMEA-0183协议介绍【转】

    本文转载自:http://blog.csdn.net/haofeng82/article/details/4439349 找到的一篇关于GPS常用的一种协议的介绍,希望对大家有用 NMEA-0183 ...

  3. python-unittest单元测试框架

    可以理解为是已经帮我们封装好的东东,可以完成执行用例\预期与实际结果的对比等. import unittest 封装好的单元测试框架,可以直接使用 编写的测试类的继承unittest.TestCase ...

  4. leetcode 169. Majority Element 多数投票算法(Boyer-Moore Majority Vote algorithm)

    题目: Given an array of size n, find the majority element. The majority element is the element that ap ...

  5. POI2014

    ...一个shabi和一堆神题的故事 今天只写了两道 之后随缘更吧 啊 顺便 snake我是不会更的 bzoj3829 POI2014 Farmcraft mhy住在一棵有n个点的树的1号结点上,每个 ...

  6. 1076 Forwards on Weibo (30)(30 分)

    Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...

  7. BZOJ_1025_[SCOI2009]游戏_DP+置换+数学

    BZOJ_1025_[SCOI2009]游戏_DP+置换 Description windy学会了一种游戏.对于1到N这N个数字,都有唯一且不同的1到N的数字与之对应.最开始windy把数字按 顺序1 ...

  8. css 跳转电脑分辨率

    因为我们经常在项目中要适配各种屏幕,为了方便前端的开发和测试.我们可以直接把电脑的分辨率调整到需要适配的最小的分辨率,其实还有一种更直接粗暴的方法.直接按F12打开控制台,在收拉浏览器就能看到目前的分 ...

  9. ESFramework Demo -- P2P通信Demo(附源码)

    现在我们将在ESFramework Demo -- 文件传送Demo 的基础上,使用ESPlus提供的第四个武器,为其增加P2P通信的功能.在阅读本文之前,请务必先掌握ESFramework 开发手册 ...

  10. 51nod 1443 路径和树——最短路生成树

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1443 不只是做一遍最短路.还要在可以选的边里选最短的才行. 以为是 ...