gpt4 book ai didi

java - 同时遍历两个数组

转载 作者:行者123 更新时间:2023-12-02 05:54:53 25 4
gpt4 key购买 nike

我必须读取一个具有特定数量字段的csv文件。我必须遍历并检测第一列的连续字符串(我使用数组来读取文件)并且仅对于这些字符串,我想获取文件第三列中它们的 int 值的总和,我已将其存储到另一个数组中。到目前为止,我能够检测连续的相同字符串,但是我如何获取它们的值并获取每个字符串的总和?是否可以通过同时遍历来做到这一点?我没有java经验,请帮忙。谢谢。这是我的代码。csv 文件是这样的,带有随机值:

ip, timestamp,elapsed,..
127.0.0.1,...,1500
127.0.0.2,...,2800
127.0.0.2,...,2400
127.0.0.2,...,2500
127.0.0.3,...,1700
127.0.0.4,...,1600
127.0.0.4,...,1500
127.0.0.5,...,2000

我必须得到这样的东西:127.0.0.2:7700, 127.0.0.4:3100

public static void main(String[] args) {
try {
System.out.println("Give file's name: ");
Scanner in = new Scanner(System.in);
String filename = in.nextLine();
File file = new File(filename);
Scanner inputfile = new Scanner(file);

String csv_data[];
ArrayList<String> ip_list = new ArrayList<String>();
ArrayList<String> elapsed_list = new ArrayList<String>();

String[] ip_array = new String[ip_list.size()];
String[] elapsed_array = new String[elapsed_list.size()];

int i = 0;
int j = 0;
int sum = 0;

while (inputfile.hasNextLine()) {
String line = inputfile.nextLine();
csv_data = line.split(",");
ip_list.add(csv_data[0]);
elapsed_list.add(csv_data[2]);
}
ip_array = ip_list.toArray(ip_array);
elapsed_array = elapsed_list.toArray(elapsed_array);

for (String element : elapsed_array) {
try {
int num = Integer.parseInt(element);
} catch (NumberFormatException fe) {
fe.printStackTrace();
System.out.println(" That's not a number");
}
}
while (i < ip_array.length) {
int start = i;
while (i < ip_array.length && (ip_array[i].equals(ip_array[start]))) {
i++;
}
int count = i - start;
if (count >= 5) {
System.out.println(ip_array[start] + " " + "|" + " " + count);
}
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}

最佳答案

public static void main(String[] args) throws Exception {
List<Data> data = read(getFile());
Map<String, Integer> idSum = groupByIdWithSum(data);
// ...
}

private static File getFile() throws Exception {
try (Scanner scan = new Scanner(System.in)) {
System.out.println("Give file's name: ");
return new File(in.nextLine());
}
}

private static List<Data> read(File file) throws Exception {
try (Scanner scan = new Scanner(file)) {
List<Data> res = new ArrayList<>();

while(scan.hasNextLine()){
String[] lineParts = scan.nextLine().splie(",");
res.add(new Data(lineParts[0], Integer.parseInt(lineParts[2])));
}

return res;
}
}

private static Map<String, Integer> groupByIdWithSum(List<Data> data) {
Map<String, Integer> map = new HashMap<>();

for(Data d : data)
map.put(d.getId(), map.getOrDefault(d.getId(), 0) + d.getElapsed());

return map;
}

final static class Data {
private final String ip;
private final int elapsed;

public Data(String ip, int elapsed) {
this.ip = ip;
this.elapsed = elapsed;
}

public String getId() {
return id;
}

public int getElapsed() {
return elapsed;
}
}

关于java - 同时遍历两个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56020889/

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