BufferedReader与Scanner相比有什么好处 [英] what are the benefits of BufferedReader over Scanner

查看:94
本文介绍了BufferedReader与Scanner相比有什么好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是有关图形中深度优先搜索的代码。
谁知道为什么在此代码中使用了bufferedReader类?
,为什么不使用nextInt函数呢?
有什么特权?
是为了加快处理速度吗?
谢谢:)

here's a code about depth first search in graphs. who knows why bufferedReader class were used in this code? and why nextInt function not used instead? what is its privilege? is it for speeding up processing? Thanks :)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Graph
{
int g[][];
int v,e;
int visited[];
void createGraph()throws IOException
{
    int a,b;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("\n Enter Number Of Vertices = ");
    v=Integer.parseInt(br.readLine());
    System.out.print("\n Enter Number Of Edges = ");
    e=Integer.parseInt(br.readLine());  
    g=new int[v+1][v+1];
    for(int i=1;i<=e;i++)
    {
        System.out.print("\n Enter Edge Infomation ");
        System.out.print("\n From =");
        a=Integer.parseInt(br.readLine());
        System.out.print("\n To =");
        b=Integer.parseInt(br.readLine());  
        g[a][b]=g[b][a]=1;
    }
}
void callDFS()
{
    visited = new int[v+1];
    dfs(1);
}   
void dfs(int k)
{
    System.out.print(k + "\t");
    visited[k]=1;
    for(int i=1;i<=v;i++)
    {
        if(g[k][i] !=0 && visited[i]!=1)
        dfs(i);
    }
}
}
class DFS
{
public static void main(String args[])throws IOException
{
    Graph g = new Graph();
    g.createGraph();
    g.callDFS();
}
}


推荐答案

有关您打算如何使用流的问题。存在用于简单线程应用程序的缓冲读取器。这是由于扫描仪缺乏线程安全性。

It's an issue of how you intend to use the stream. A buffered reader exists for simple and threaded applications. This is due to scanner's lack of thread safety.

我想您将从这个问题中得到更多的信息 Scanner vs. BufferedReader

I think you'll get more on this from this question Scanner vs. BufferedReader

这篇关于BufferedReader与Scanner相比有什么好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆