简体   繁体   English

从文本文件/扫描仪存储字符串。 数组还是内置的?

[英]Storing strings from a text file/scanner. Array or built-in?

This code snippet is to to read a text file, turn the lines into objects through a different public (non changeable) class(externalClass). 此代码段将读取一个文本文件,通过不同的公共(不可更改)类(externalClass)将这些行变成对象。

The external class does nothing but turn strings (lines from the .txt through nextLine) into objects, and is fully functional. 外部类除了将字符串(从.txt通过nextLine的行)转换为对象外,什么也不做,并且功能齐全。 The scanner(scanner3) is assigned to the text file. 扫描仪(scanner3)被分配给文本文件。

        while (scanner3.hasNext()) {
               externalClass convertedlines = new externalClass(scanner3.nextLine());

I'm not new to programming, but as I'm new to java, I do not know if this requires me to create an array, or if the returned objects are sorted in some other way. 我对编程并不陌生,但是由于对Java不熟悉,所以我不知道这是否需要我创建数组,或者返回的对象是否以其他方式排序。 ie is the "importedlines" getting overwritten with each run of the loop(and I need to introduce an array into the loop), or are the objects stored in some way? 即,“ importedlines”是否在每次循环运行时都会被覆盖(并且我需要在循环中引入一个数组),或者对象是以某种方式存储的?

The question may seem strange, but with the program I am making it would be harder (but definitely not impossible) if I used an array. 这个问题似乎很奇怪,但是如果使用数组,我正在使程序变得更困难(但绝对不是不可能)。

Any help would be appreciated. 任何帮助,将不胜感激。

As requested, externalClass: 根据要求,externalClass:

public class exernalClass {
    private String line;

    externalClass(String inLine){   
        line = inLine;
    }

    public String giveLine() {
        return line;
    }
}

You are right, the convertedlines will be overwritten in every run of the loop. 没错, convertedlines行将在循环的每次运行中被覆盖。

Depending on what you want to do with the lines afterwards and if you know how big the files you read are some implementation of Collection (like an ArrayList or LinkedList ) may be a better fit than an array. 取决于之后要处理的行以及是否知道读取的文件是Collection某种实现(例如ArrayListLinkedList )的大小,它可能比数组更合适。

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

相关问题 使用扫描仪分析文件中的输入。 JAVA - Parsing input from a file using Scanner. JAVA Android Studio:将扫描程序中的字符串存储在/ assets中的文件的String数组中 - Android Studio: Storing Strings from a scanner in a String array from a file in /assets 将文本文件中的字符串存储到包含不同行的数组中 - Storing Strings from a Text File into an array including different lines 使用扫描仪时逻辑错误。 文本文件的第一行未打印到标准输出 - Wrong logic in using scanner. The first line of the text file is not printed out to stdout 读取文本文件中的整数并使用扫描仪和异常存储到数组 - Read text file for integers and storing to array using scanner and exceptions 扫描器用于输入文件并将数据对象存储在数组中的输入文件中 - Scanner for input file and storing data objects from input file in array 将文本从文件存储到数组 - Storing text from file to array 从文本文件中读取整数并存储到单独的变量中? (扫描器) - Reading Integers from Text File and Storing Into Separate Variables? (Scanner) 使用扫描仪将单词的出现次数及其计数存储在文件中。(Java) - Store occurences of words in a file and their count,using Scanner.( Java ) Java扫描程序类从文本文件读取制表符分隔的字符串 - Java scanner class reading tab delimited strings from text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM