Java输入输出、常见场景解决方案、文件夹操作

目录

0.常用方法 String、Integer、List、Queue、Stack、HashMap

1. 输入 Scanner、BufferReader

2. 输出 print

3. 文件夹操作


0.常用方法

  • int a = sc.nextInt();        int a = Integer.parseInt(str);        String str = sc.nextline();   
  • String[] strs = str.split(" ");    str.substring(startend)左闭右开
  • str.length();    str.charAt(0);    str.IndexOf('ch');    str.equals("xx")
    • Queue:offer入,poll出,peek
    • Stack :push入,pop出,peek
    • xx.size()    xx.isEmpty()    xx.contians()    xx.clear()
    • Map<Integer,Integer> map = new HashMap<>();
    • map.put(k, v) 添加值   map.get(k)   map.containsKey("xxx") 是否包含某key
    • List<Integer> list = new ArrayList<>();
    • list.add()    list.get()    list.sort();排序      list.sort( (a,b) -> b - a);倒序
    • Collections.sort(list, (a, b) -> b - a);     倒序
  • Arrays.sort(str);数组顺序       Arrays.toString(str)数组转字符串
  • Arrays.stream(str).boxed().sorted((a, b) -> b - a).mapToInt(p -> p).toArray();数组倒序

1. 输入

Scanner 读入

import java.util.*;       Scanner sc = new Scanner(System.in).useDelimiter("\\D")设置忽略的分隔符

  • sc.nextLine()        读一行内容,获得 /n 之前的所有内容为字符串String(可用于读取空行)
  • sc.nextInt()           读下一个字符返回Int型,忽略之间的空格/Tab/Enter等
  • sc.next()               读下一个有效字符串,返回字符串String
  • sc.hasNextLine()  判断有没有下一行,返回True/False
  • Integer.parseInt(string)   字符串转换为Int整型

连续数字,空格分隔直接读:

// 读入一行n,第二行存入数组(法一)
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
    arr[i] = sc.nextInt();
}

读入以空格/逗号分隔的数组:

// 读入首行 n 和 k (法二)
Scanner sc = new Scanner(System.in);
String[] line1 = sc.nextLine().split(" ");
int n = Integer.parseInt(line1[0]);
int k = Integer.parseInt(line1[1]);

// 存入数组(以空格或逗号分隔的数据)
String[] line2 = sc.nextLine().split(" ");
int[] arr = new int[n];
for (int i = 0; i < line2.length; i++) {
    arr[i] = Integer.parseInt(line2[i]);
}

读入带框数组/链表:

Scanner sc = new Scanner(System.in);
String strs = sc.nextLine();
// 处理字符串
    // 输入格式不标准直接return 
    if (strs == null || strs.charAt(0) != '[' || strs.charAt(strs.length()-1) != ']') return;
String[] str = strs.substring(1,strs.length()-1).split(",");
    //长度唯一的特殊情况直接return
    if (str.length == 1) System.out.println(strs);return;
long[] nums = new long[str.length];
for (int i = 0; i < str.length; i++) {
    nums[i] = Long.parseLong(str[i]);
}

BufferReader 读入

import java.io.*;     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

  • br.readline()        按行读取,返回String字符串
    • String str = br.readline();        读取一行字符串
    • String[] strs = br.readline().split(" ");    读取一行字符串,按空格分隔为字符串数组
public static void main(String[] args) throws IOException {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));//创建实例
    String str = br.readLine();  //读取一行字符串
    String[] temp=br.readLine().split(" ");  //读取一行字符串,按空格分隔为字符串数组
}

2. 输出

空格分隔输出数组

    // 输出数组,以空格分隔
    public static void printarr(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (i != arr.length-1) {
                System.out.print(arr[i] + " ");
            } else {
                System.out.print(arr[i]);
            }
        }
    }

按照带框格式输出链表:

    // 按格式输出链表
    public static void PrintList(ListNode head) {
        if (head == null) return;
        ListNode cur = head.next;
        System.out.print("{" + head.val);
        while (cur != null) {
            System.out.print("," + cur.val);
            cur = cur.next;
        }
        System.out.print("}");
    }

3. 文件夹操作

猜你喜欢

转载自blog.csdn.net/qq_52057693/article/details/126330010
今日推荐