目 录CONTENT

文章目录

Java算法题必备

smallkun
2022-03-21 / 0 评论 / 0 点赞 / 113 阅读 / 513 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-06-12,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我删除。

快速输入输出

快速输入1

// 创建分词器输入流
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
// 转到下一个标记
in.nextToken();
// 输入字符
String str = in.sval;
// 转到下一个标记
in.nextToken();
// 输入数字
double num = in.nval;

快速输入2

public class BufferedReaderInput {
	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // 输入
		String line = in.readLine(); // 输入的一行字符
		System.out.println(line);
	}
}


快速输入的静态工具类

// 快速输入类
	static class Reader {
		static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
		static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

		// 获取下一段文本 ,以空格回车等为分界符
		static String next() throws IOException {
			in.nextToken();
			return in.sval;
		}

		// 获取数字
		static int nextInt() throws IOException {
			in.nextToken();
			return (int) in.nval;
		}

		static double nextDouble() throws IOException {
			in.nextToken();
			return in.nval;
		}

		// 获取一行
		static String nextLine() throws IOException {
			return bf.readLine();
		}
	}


快速输出

// 创建一个输出
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
// 输入
out.println("hello");
// 刷新(需要刷新才能显示)
out.flush();
// 关闭流(不用了就关闭,不然占内存,这是一个好习惯,个人电脑没关系,开发工作时候记得关)
out.close();

实测该方法最快,其次是每次调用PrintWriter的println后紧接着调用flush,最次是调用标准输出(syso)。
该类中有print、println、format、append等常用方法。

输入输出工具类

static class ReaderWriterUtil {

        static StreamTokenizer in = null;
        static BufferedReader bf = null;
        static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));


        //本地
        static {
            try {
                if (FINAL_FLAG_FALSE){
                    in = new StreamTokenizer(new BufferedReader(new FileReader("input.txt")));
                    bf = new BufferedReader(new FileReader("input.txt"));
                }else{
                    in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
                    bf = new BufferedReader(new InputStreamReader(System.in));
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }


        // 获取下一段文本 ,以空格回车等为分界符
        static String next() throws IOException {
            in.nextToken();
            return in.sval;
        }

        // 获取数字
        static int nextInt() throws IOException {
            in.nextToken();
            return (int) in.nval;
        }

        static double nextDouble() throws IOException {
            in.nextToken();
            return in.nval;
        }

        // 获取一行
        static String nextLine() throws IOException {
            return bf.readLine();
        }

        static void print(Object o){
            out.print(o);
            out.flush();
        }

        static void closeResource() throws IOException {
            if (in != null){
                bf.close();
            }
            out.close();
        }
    }
0

评论区