gpt4 book ai didi

Java - 将 ArrayList 的元素存储到单独的 block 中

转载 作者:行者123 更新时间:2023-12-02 06:04:47 26 4
gpt4 key购买 nike

这是我的全部代码,总而言之,它标准化了两个文本文件,然后打印出结果。

import java.io.*;
import java.util.*;

public class Plagiarism {

public static void main(String[] args) {

Plagiarism myPlag = new Plagiarism();

if (args.length == 0) {
System.out.println("Error: No files input");
}
else if (args.length > 0) {
try {
for (int i = 0; i < args.length; i++) {
BufferedReader reader = new BufferedReader (new FileReader (args[i]));
List<String> foo = simplify(reader);
for (int j = 0; j < foo.size(); j++) {
System.out.print(foo.get(j));
}
}
}
catch (Exception e) {
System.err.println ("Error reading from file");
}
}
}

public static List<String> simplify(BufferedReader input) throws IOException {
String line = null;
List<String> myList = new ArrayList<String>();

while ((line = input.readLine()) != null) {
myList.add(line.replaceAll("[^a-zA-Z0-9]","").toLowerCase().trim());
}
return myList;
}

}

我想要实现的下一点是:使用命令行,第三个参数将是用户输入的任何整数( block 的大小)。然后我必须使用它将该数组的元素存储到重叠的单独 block 中。 EG:猫坐在垫子上, block 大小为 4。 block 1 为:Thec block 2:heca block 3:ecat,依此类推,直到到达数组末尾。

有什么想法吗?

提前谢谢大家。

最佳答案

要获取 block 大小,请使用以下命令:

if(args.length != 4)
return;
int blockSize = Integer.valueOf(args[3]);

这是一个可以帮助您的示例

import java.util.*;

public class Test {
public static void main(String[] args) {
String line = "The dog is in the house";
line = line.replace(" ", "");
List<String> list = new ArrayList<String>();
for (int i = 0; i <= line.length() - 4; i++)
list.add(line.substring(i, i + 4));
System.out.println(list);

}

输出:

[Thed, hedo, edog, dogi, ogis, gisi, isin, sint, inth, nthe, theh, heho, ehou, hous, ouse]

这就是你想做的

关于Java - 将 ArrayList 的元素存储到单独的 block 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22389905/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com