打印Java的ArrayList的内容一行一行地安慰 [英] Printing Java ArrayList contents line by line to console

查看:87
本文介绍了打印Java的ArrayList的内容一行一行地安慰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的Java编程,这实际上是一个问题,我需要解决家庭作业的一部分:我读文件行的内容用线串转化成供以后处理一个ArrayList。我需要的程序打印到控制台上单独的行ArrayList的内容,但在我运行编译文件的输出打印文件的第一行,那么第一行和第二行打印在一起的下一行,然后打印第一,第二和第三行的程序。

我的这是如何工作的理解是,该方案将采取我的文件,该的FileReader和BufferedReader将抓住文本文件作为字符串中的线,然后在不同的放置在ArrayList中每个字符串ArrayList中合适的位置?是否有人可以告诉我在哪里while循环,我的问题呢?谢谢!

code:

 公共类ArrayListDemo{
    公共静态无效的主要(字串[] args)
    {
    尝试
    {
        ArrayList的<串GT;演示=新的ArrayList<串GT;();
        FR的FileReader =新的FileReader(hi.tpl);
        读者的BufferedReader =新的BufferedReader(FR);
        串线;
        而((行= reader.readLine())!= NULL)
            {
            //添加到ArrayList的
            demo.add(线);
            的System.out.println(演示);
            }        reader.close();
    }赶上(例外五)
        {
            的System.out.println(错误:+ e.getMessage());
            System.exit(0);
        }
    }
}

中获得的输出:

 
猫,鼠
猫,鼠,帽子

期望的输出:

 

帽子


解决方案

每次你看到的输出是ArrayList.toString的()的结果被打印到标准输出。相反,通过你完成后,读取文件的内容到该ArrayList ArrayList中循环:

 ,而((行= reader.readLine())!= NULL){
        //添加到ArrayList的
        demo.add(线);
    }
    reader.close();
    //做有趣的东西与演示这里的ArrayList
    对于(一个String:演示){的System.out.println(S); }

I'm very new to Java programming, and this is actually part of a problem I need to solve for homework: I am reading the contents of a file line by line as a String into an ArrayList for later processing. I need the program to print out to console the contents of the ArrayList on separate lines, but the output after I run the compiled file prints the first line of the file, then prints the first and second lines together on the next line, then prints the first, second and third lines of the program.

My understanding of how this is supposed to work is that the program will take my file, the FileReader and BufferedReader will grab the lines of text in the file as Strings, which are then placed in the ArrayList with each String at a different position in the ArrayList right? Can someone please tell me where in the while loop I'm going wrong? Thanks!

Code:

public class ArrayListDemo

{
    public static void main (String[]args)
    {
    try
    {
        ArrayList<String> demo= new ArrayList <String>();
        FileReader fr= new FileReader("hi.tpl");
        BufferedReader reader= new BufferedReader(fr);
        String line;
        while ((line=reader.readLine()) !=null)
            {
            //Add to ArrayList
            demo.add(line);
            System.out.println(demo);
            }

        reader.close();
    }catch (Exception e)
        {
            System.out.println("Error: "+e.getMessage());
            System.exit(0);
        }
    }
}

Obtained output:

cat
cat, rat
cat, rat, hat

Expected output:

cat
rat
hat

解决方案

The output you are seeing each time is the result of ArrayList.toString() being printed to standard out. Instead, loop through the ArrayList after you've finished reading the contents of the file into the ArrayList:

    while ((line=reader.readLine()) !=null) {            
        //Add to ArrayList
        demo.add(line);            
    }
    reader.close();
    //do fun stuff with the demo ArrayList here
    for (String s : demo) { System.out.println(s); }

这篇关于打印Java的ArrayList的内容一行一行地安慰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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