简体   繁体   English

Java ArrayList,如何一次列出一个?

[英]Java ArrayList, how to list one at a time?

So I am just doing a quick exception program where I just ask the user to input 10 names and the names will be transferred to a .txt file. 因此,我正在做一个快速例外程序,在该程序中,我只要求用户输入10个名称,这些名称将被传输到.txt文件中。 I have to us an arrayList, so the problem I have now is, every time the user inputs a name, the counsel will output all the previous names before it as well. 我需要一个arrayList,所以现在的问题是,每当用户输入名称时,顾问也会在其之前输出所有以前的名称。 Can someone please show me how to print the ArrayList one at a time? 有人可以告诉我如何一次打印一个ArrayList吗?

Ex. Please enter name 1: Wei
Please enter name 2: ming
Please enter name 3: guo

At the moment, this is how my output looks like: 目前,这是我的输出结果:

    /*      
* Please enter name 1:[wei]
Please enter name 2:[wei, ming]
Please enter name 3:[wei, ming, guo]
Please enter name 4:[wei, ming, guo, wei]
Please enter name 5:[wei, ming, guo, wei, ming]
Please enter name 6:[wei, ming, guo, wei, ming, dfuo]
Please enter name 7:[wei, ming, guo, wei, ming, dfuo, wer]
Please enter name 8:[wei, ming, guo, wei, ming, dfuo, wer, sdf]
Please enter name 9:[wei, ming, guo, wei, ming, dfuo, wer, sdf, sdf]
Please enter name 10:[wei, ming, guo, wei, ming, dfuo, wer, sdf, sdf, sdf]
*/

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class NamesDemo {

    public static void main(String[] args) {
        String NamesDemo = "names.txt";
        PrintWriter outputStream = null;
        try {
            outputStream = new PrintWriter(NamesDemo);
        } catch (FileNotFoundException e) {
            System.out.println("Error opening the file " + NamesDemo);
            System.exit(0);
        }

        System.out.println("Enter 10 names: ");
        Scanner kb = new Scanner(System.in);
        ArrayList<String> name = new ArrayList<String>();

        for (int i = 0; i < 10; i++) {
            name.add(kb.next());
            outputStream.println("Please enter name " + (i + 1) + ":" + name);

        }
        outputStream.close();
        System.out.println("Thse names were sent to file: " + NamesDemo);
    }

}

Simply print the last entered value instead of the whole list : 只需打印最后输入的值而不是整个列表:

for (int i =0; i<10; i++){
    String newValue = kb.next();
    name.add(newValue);
    outputStream.println("Please enter name " + (i+1) + ":" + newValue);

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何一次遍历一个Java Arraylist - How to Iterate through a Java Arraylist one at a time 如何从 Java ArrayList 一次显示一项 - How to show one Item at a time from Java ArrayList 如何一次拥有Arraylist中每个项目的通知-Android Java - How possess notification for every item in the Arraylist in one time - Android Java 在 Java 中,如何创建一个 ArrayList,该列表中的一个元素是另一个 ArrayList,每个元素都是唯一的? - In Java, how do you create an ArrayList, with one element of that list being another ArrayList, that is unique for each element? 如何转换ArrayList <ArrayList<String> &gt;列表 <List<String> &gt;在Java? - How to convert an ArrayList<ArrayList<String>> to List<List<String>> in Java? 如何执行带有循环的ArrayList程序,该循环一次呈现列表的每个索引? - How shall I execute an ArrayList program with a loop that renders each index of the list one at a time? 如何在Java中的arraylist中获取带时间的升序日期 - How to get ascending dates with time in arraylist in java 如何在java中的另一个Arraylist中也打印出单词 - How to print out word in one Arraylist also in another Arraylist in java 如何使用Java将数据从一个ArrayList添加到另一个ArrayList? - How to add data from one ArrayList to another ArrayList using Java? 如何使用java 8将二维对象Set / ArrayList转换为一个Flat Set / List - How to Convert two dimension object Set/ArrayList into one Flat Set/List using java 8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM