gpt4 book ai didi

java - 对arraylist中的具体数据求和

转载 作者:行者123 更新时间:2023-12-01 20:11:09 25 4
gpt4 key购买 nike

我正在寻找一种根据特定条件总结 ArrayList 中某些特定数据的好方法,但我无法使其按预期工作。例如,我的ArrayList包含以下数据序列Domain,IP,Total Number:

[gmail.com", "172.11.0.89", 1, gmail.com", "172.11.0.89", 60, "gmail.com", "192.168.0.66", 13] 

我想做的是循环遍历 ArrayList 并检索 IP 地址。之后,我将得到下一个索引,即总数。我将检查整个 ArrayList 是否有任何类似的 IP。如果我找到类似的 IP,我会做同样的事情,并将总数与之前的总数相加。例如,172.11.0.89 的总数应包含 61,因为 60+1 = 61。

但是我没有得到那个输出。这是我的示例输出:

[gmail.com", "172.11.0.89", 1, gmail.com", "172.11.0.89", 60, "gmail.com", "192.168.0.66", 13] 
The IP "172.11.0.89"
The IP "172.11.0.89"
The IP "192.168.0.66"
The Data in Final Data is :["172.11.0.89", 1, "172.11.0.89", 75, "172.11.0.89", 149, "192.168.0.66", 223]

这是我的源代码:

System.out.println(domainDailyData1);
for(int l = 1;l<domainDailyData1.size();l+=3) // getting the total count
{
String tempIP1 = domainDailyData1.get(l);
System.out.println("The IP "+tempIP1);
for(int e = 1;e<domainDailyData1.size();e+=3)
{
String tempIP2 = domainDailyData1.get(l);

if(tempIP2.equals(tempIP1))
{
String str1 = domainDailyData1.get(e+1);

int temp1 = Integer.parseInt(str1);
num1 += temp1;
}
}
FinalData1.add(tempIP1);
FinalData1.add(String.valueOf(num1));

}
System.out.println("The Data in Final Data is :"+FinalData1);

最佳答案

修正错误

正如评论中提到的,您忘记初始化一些变量。首先,由于您没有在代码中明确定义,我假设 FinaData1List<String> .

  1. 您忘记初始化num1
  2. 您需要记住您已经扫描了特定的 IP 地址
  3. 在第二个 for 循环中,您混淆了迭代器:您使用了 get(l)而不是get(e) 。与get(l) ,您将永远拥有tempIP2.equals(tempIP1) = truetempIP1 = domainDailyData1.get(l)

经过更正,您的代码如下所示:

public static void main(String... aArgs) {
List<String> domainDailyData1 = Arrays.asList(new String[]{
"gmail.com", "172.11.0.89", "1",
"gmail.com", "172.11.0.89", "60",
"gmail.com", "192.168.0.66", "13"});

// >>> convention: don't use capital letter as the first letter of a variable
List<String> finalData1 = new ArrayList<>();

// getting the total count
for (int l = 1; l < domainDailyData1.size(); l += 3) {
String tempIP1 = domainDailyData1.get(l);

// 2. to avoid looping an IP that you already counted
if (!finalData1.contains(tempIP1)) {

System.out.println("The IP " + tempIP1);

// 1. num1 initialisation
int num1 = 0;

for (int e = 1; e < domainDailyData1.size(); e += 3) {

// 3. iterator confusion
String tempIP2 = domainDailyData1.get(e);

if (tempIP2.equals(tempIP1)) {
String str1 = domainDailyData1.get(e + 1);
int temp1 = Integer.parseInt(str1);
num1 += temp1;
}
}

finalData1.add(tempIP1);
finalData1.add(String.valueOf(num1));
}
}

System.out.println("The Data in Final Data is :" + finalData1);
}

使用代码中的输入进行测试。

Java 流:更有趣

如果你碰巧使用 Java 8,那么你可以用流玩一下。这个想法是在很大程度上依赖于您的列表始终正确排序的事实:

For any i in [0, list.size()], you have:

  • if i % 3 = 0 then you have the domain name

  • if i % 3 = 1 then you have the ip address (format does not matter)

  • if i % 3 = 2 then you have the visit count which is a proper integer

This also means that the list size is always a multiple of 3: list.size() % 3 = 0.

因此,我将执行以下操作:

  1. 查找所有 IP 地址按其分组
  2. 找到下一个元素(即观看次数)并将其求和

给出:

public static void main(String... aArgs) {

List<String> domainDailyData1 = Arrays.asList(new String[]{
"gmail.com", "172.11.0.89", "1",
"gmail.com", "172.11.0.89", "60",
"gmail.com", "192.168.0.66", "13"});

// Result is a map as it is a pairing <IpAddress, Count>
Map<String, Integer> countMap = Stream
// Start with index = 1 and then jump by step of 3
.iterate(1, i -> i + 3)
// Get the number of ip address to scan. A row a basically the
// triplet {domain, ip address, count} as I defined for the input.
//
// Integer division: if list size is below 3, then the result is
// 0 and nothing happens. If the list size is not a multiple of
// 3, for example, 11, then the result is 3 and last rows (index
// from 8 to 10) are ignored
.limit(domainDailyData1.size() / 3)
// optional line but if you want to know what's currently happening...
.peek(i -> System.out.println("Checking IP Address: " + domainDailyData1.get(i)))
// Result is a map as it is a pairing <IpAddress, Count>
.collect(Collectors.toMap(
// The key is of course the ip address which is at
// position i. What's nice with Map is that if a another
// identical ipaddress is found, the key are merged. That's
// why it is necessary to define a merge function
i -> domainDailyData1.get(i),
// fetch the count associated with index i. As one never
// trust unknown input, any invalid count means zero
i -> {
try {
// Just get the count. The i+1 is not supposed
// to trigger the ArrayOutOfBoundException as
// guaranteed by the .limit() above
Integer count = Integer.parseInt(domainDailyData1.get(i + 1));
return count;
}
catch (NumberFormatException e) {
// silent exception catching but you can do
// anything here
return 0;
}
},
// If two ip addresses are found, just sum the count
// The static method reference form is Integer::sum
(oldCount, newCount) -> oldCount + newCount
)
);

System.out.println(countMap);
}

如果你想复制粘贴进行测试

  • 没有解释
  • 并带有静态方法引用
  • 没有 peek()
  • 不进行map定义,直接打印结果:

代码是:

public static void main(String... aArgs) {

List<String> domainDailyData1 = Arrays.asList(new String[]{ "gmail.com", "172.11.0.89", "1", "gmail.com", "172.11.0.89", "60", "gmail.com", "192.168.0.66", "13"});

Stream
.iterate(1, i -> i + 3)
.limit(domainDailyData1.size() / 3)
.collect(Collectors.toMap(
i -> domainDailyData1.get(i),
i -> {
try {
return Integer.parseInt(domainDailyData1.get(i + 1));
}
catch (NumberFormatException e) {
return 0;
}
},
Integer::sum))
.entrySet()
.stream()
.forEach(System.out::println);
}

这只是为了好玩,所以这段代码显然可能不是 100% 防错。

<小时/>

来源:

关于java - 对arraylist中的具体数据求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46691681/

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