查漏补缺(写一点最近新学的东西)

首先是split()方法,以代码举例

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        String command= scanner.nextLine();
        //nextLine()可以接受输入字符串中存在空格
        //next()不能接受输入字符串中存在空格
        String[] date=command.split(" ");
        //split()中的空格是分割标志,会将空格前后分为两部分
        //分割标志可以是除空格外的其它
        for (int i = 0; i < date.length; i++) {
            System.out.println(date[i]);
        }
    }
}

运行效果如下图

ioppunnn yujnj//输入的字符串
ioppunnn
yujnj

split()返回一个字符串数组,所以在代码中,创建了一个字符串数组去接收返回值.
然后是自定义的集合排序方法,以代码举例

//PersonSortable2是一个自定义类里面有 String name和int age两个属性
//set()和get()方法由idea提供
class NameComparator implements Comparator<PersonSortable2>{
    @Override
    public int compare(PersonSortable2 o1, PersonSortable2 o2) {
        return o1.getName().compareTo(o2.getName());
    }
}
class AgeComparator implements Comparator<PersonSortable2> {
    @Override
    public int compare(PersonSortable2 o1, PersonSortable2 o2) {
        return o1.getAge()-o2.getAge();
    }
}
//NameComparator和AgeComparator扩展了Comparator<Object>
//所以必须重写其方法,重写部分为自定义排序方式

代码中的compareTo()方法用于比较两个字符串大小,从0下标开始比较两个字符串中字符的ascll码值,直到一方到达尾部,或者出现不同字符.

ArrayList<PersonSortable2> arrayList=new ArrayList<>();//集合
Collections.sort(arrayList,new NameComparator());
Collections.sort(arrayList,new AgeComparator());
//执行自定义排序代码

最后是io输入和输出的基础写法,以代码举例

//
import java.io.*;
public class Main {
    public static void main(String[] args) {
        String path="文件路径";
        //注意文件路径中的单\要用\\来表示,因为单\是转义字符
        //单个\不能放在字符串中,放了会报错,
        FileReader fileInputStream=null;
        try {
            fileInputStream= new FileReader(path);
            int y;
            while ((y=fileInputStream.read()) != -1){
                System.out.println((char) y);
                //(char)y将y强制转换,打印时可以看到文件中的写入的词
                //被一个个打印出来。
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

代码中会有这么多语句是因为文件路径极易出错,或者不存在.FileReade类型读取文件中的数据是以字符为单位,FileInputStream类型读取文件中的数据是以字节为单位.

import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class weekday2 {
    public static void Main(String[] args) {
        String path="文件路径";
        try {
            FileWriter fileWriter=new FileWriter(path);
            String str;
            for (int i = 0; i < 4; i++) {
                System.out.println("请输入:");
                str=in();
                fileWriter.write(str);
                //将键盘输入写入文件
            }
            fileWriter.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static String in(){
        Scanner scanner=new Scanner(System.in);
        String str=scanner.next();
        return str;
    }
}

这些最近几天写题和上课学到的一部分,还有很多的东西需要去看去学,java的水太深,先写到这里。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值