从文本文件一次向数组列表添加 3 个字符的最有效方法是什么?
What is the most efficient way to add 3 characters at a time to an araylist from a text file?
假设您有一个包含 "abcdefghijklmnop" 的文本文件,并且您必须一次将 3 个字符添加到字符串类型的数组列表中。因此,数组列表的第一个单元格为 "abc",第二个单元格为 "def",依此类推,直到输入所有字符。
public ArrayList<String> returnArray()throws FileNotFoundException
{
int i = 0
private ArrayList<String> list = new ArrayList<String>();
Scanner scanCharacters = new Scanner(file);
while (scanCharacters.hasNext())
{
list.add(scanCharacters.next().substring(i,i+3);
i+= 3;
}
scanCharacters.close();
return characters;
}
public ArrayList<String> returnArray()throws FileNotFoundException
{
private ArrayList<String> list = new ArrayList<String>();
Scanner scanCharacters = new Scanner(file);
String temp = "";
while (scanCharacters.hasNext())
{
temp+=scanCharacters.next();
}
while(temp.length() > 2){
list.add(temp.substring(0,3));
temp = temp.substring(3);
}
if(temp.length()>0){
list.add(temp);
}
scanCharacters.close();
return list;
}
在这个例子中,我读入了文件中的所有数据,然后以三个为一组进行解析。 Scanner 永远无法回溯,因此使用 next 会遗漏一些您正在使用的数据。您将获得单词组(由空格分隔,Java 的默认分隔符),然后将前 3 个字母子串掉。
IE:
亚历克西·沃扎曼
会给你:
ALE 和 WOW
我的示例的工作方式是获取一个字符串中的所有字母,并连续从三个字母中提取字符串,直到没有更多字母为止,最后,它添加余数。就像其他人所说的那样,最好阅读不同的数据解析器,例如 BufferedReader。另外,如果你想继续使用你现在的方法,我建议你研究一下substrings和Scanner。
请使用下面的代码,
ArrayList<String> list = new ArrayList<String>();
int i = 0;
int x = 0;
Scanner scanCharacters = new Scanner(file);
scanCharacters.useDelimiter(System.getProperty("line.separator"));
String finalString = "";
while (scanCharacters.hasNext()) {
String[] tokens = scanCharacters.next().split("\t");
for (String str : tokens) {
finalString = StringUtils.deleteWhitespace(str);
for (i = 0; i < finalString.length(); i = i + 3) {
x = i + 3;
if (x < finalString.length()) {
list.add(finalString.substring(i, i + 3));
} else {
list.add(finalString.substring(i, finalString.length()));
}
}
}
}
System.out.println("list" + list);
这里我使用了 Apache String Utils 的 StringUtils.deleteWhitespace(str) 来删除文件中的空白 space tokens.and for 循环中的 if 条件来检查三个子字符串char 在字符串中可用,如果它不可用,那么剩下的任何字符都将转到包含以下字符串的 list.My 文本文件
asdfcshgfser ajsnsdxs 第一行和第二行
sasdsd fghfdgfd
执行程序后结果如下,
list[asd, fcs, hgf, ser, ajs, nsd, xs, sas, dsd, fgh, fdg, fd]
假设您有一个包含 "abcdefghijklmnop" 的文本文件,并且您必须一次将 3 个字符添加到字符串类型的数组列表中。因此,数组列表的第一个单元格为 "abc",第二个单元格为 "def",依此类推,直到输入所有字符。
public ArrayList<String> returnArray()throws FileNotFoundException
{
int i = 0
private ArrayList<String> list = new ArrayList<String>();
Scanner scanCharacters = new Scanner(file);
while (scanCharacters.hasNext())
{
list.add(scanCharacters.next().substring(i,i+3);
i+= 3;
}
scanCharacters.close();
return characters;
}
public ArrayList<String> returnArray()throws FileNotFoundException
{
private ArrayList<String> list = new ArrayList<String>();
Scanner scanCharacters = new Scanner(file);
String temp = "";
while (scanCharacters.hasNext())
{
temp+=scanCharacters.next();
}
while(temp.length() > 2){
list.add(temp.substring(0,3));
temp = temp.substring(3);
}
if(temp.length()>0){
list.add(temp);
}
scanCharacters.close();
return list;
}
在这个例子中,我读入了文件中的所有数据,然后以三个为一组进行解析。 Scanner 永远无法回溯,因此使用 next 会遗漏一些您正在使用的数据。您将获得单词组(由空格分隔,Java 的默认分隔符),然后将前 3 个字母子串掉。 IE: 亚历克西·沃扎曼 会给你: ALE 和 WOW
我的示例的工作方式是获取一个字符串中的所有字母,并连续从三个字母中提取字符串,直到没有更多字母为止,最后,它添加余数。就像其他人所说的那样,最好阅读不同的数据解析器,例如 BufferedReader。另外,如果你想继续使用你现在的方法,我建议你研究一下substrings和Scanner。
请使用下面的代码,
ArrayList<String> list = new ArrayList<String>();
int i = 0;
int x = 0;
Scanner scanCharacters = new Scanner(file);
scanCharacters.useDelimiter(System.getProperty("line.separator"));
String finalString = "";
while (scanCharacters.hasNext()) {
String[] tokens = scanCharacters.next().split("\t");
for (String str : tokens) {
finalString = StringUtils.deleteWhitespace(str);
for (i = 0; i < finalString.length(); i = i + 3) {
x = i + 3;
if (x < finalString.length()) {
list.add(finalString.substring(i, i + 3));
} else {
list.add(finalString.substring(i, finalString.length()));
}
}
}
}
System.out.println("list" + list);
这里我使用了 Apache String Utils 的 StringUtils.deleteWhitespace(str) 来删除文件中的空白 space tokens.and for 循环中的 if 条件来检查三个子字符串char 在字符串中可用,如果它不可用,那么剩下的任何字符都将转到包含以下字符串的 list.My 文本文件 asdfcshgfser ajsnsdxs 第一行和第二行 sasdsd fghfdgfd
执行程序后结果如下,
list[asd, fcs, hgf, ser, ajs, nsd, xs, sas, dsd, fgh, fdg, fd]