- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在寻找一种根据特定条件总结 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);
最佳答案
正如评论中提到的,您忘记初始化一些变量。首先,由于您没有在代码中明确定义,我假设 FinaData1
是 List<String>
.
num1
get(l)
而不是get(e)
。与get(l)
,您将永远拥有tempIP2.equals(tempIP1) = true
如tempIP1 = 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 8,那么你可以用流玩一下。这个想法是在很大程度上依赖于您的列表始终正确排序的事实:
For any i in [0, list.size()], you have:
if
i % 3 = 0
then you have the domain nameif
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 integerThis also means that the list size is always a multiple of 3:
list.size() % 3 = 0
.
因此,我将执行以下操作:
给出:
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()
代码是:
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/
我正在尝试在 OCaml 中创建一个函数,该函数在数学中执行求和函数。 我试过这个: sum n m f = if n = 0 then 0 else if n > m then f
我正在尝试找到一个可以帮助我解决问题的公式。 这个公式应该对每个靠近(总是在左边)具有相同名称的单元格的单元格求和(或工作)。如下所示: 将每个大写字母视为 “食谱”并且每个小写字母为 “成分” .在
让它成为以下 python pandas DataFrame,其中每一行代表一个人在酒店的住宿。 | entry_date | exit_date | days | other_columns
我有显示客户来电的数据。我有客户号码、电话号码(1 个客户可以有多个)、每个语音调用的日期记录以及调用持续时间的列。表看起来如下示例。 CusID | PhoneNum | Date
让它成为以下 python pandas DataFrame,其中每一行代表一个人在酒店的住宿。 | entry_date | exit_date | days | other_columns
我得到了两列数据; 答: 2013年12月31日 2013年12月30日 2013年12月29日 2013年12月28日 2013年12月27日 2012年12月26日 B: 10 10 10 10
我对 double 格式的精度有疑问。 示例: double K=0, L=0, M=0; scanf("%lf %lf %lf", &K, &L, &M); if((K+L) 我的测试输入: K
我有以下数组: int[,] myArray1 = new int[2, 3] { { 1, 2, 3 }, { 4, 6, 8 } }; int[,] myArray2 = new int[2, 3
我需要有关报告查询的帮助。我在该方案的底部有一个发票表,需要一种方法来获取总计费金额,同时在此数据库方案中的较高点进行条件过滤。我需要加入其他表,这会导致 SUM 函数返回不需要的结果。 这是我正在使
我有一个使用innodb作为存储引擎的MySQL数据库,并且我有许多采用基本形式的查询: SELECT bd.billing, SUM(CASE WHEN tc.transaction_class
尝试创建一个查询来给出总胜、平和负。我有以下查询 SELECT CASE WHEN m.home_team = '192' AND m.home_full_time_score
我正在尝试生成一份报告,显示排名靠前的推荐人以及他们推荐的人产生了多少收入。 这是我的表格的缩写版本: Users Table ------------------ id referral_user_
我有以下查询,并得到了预期的结果: SELECT IF (a1>b1,'1','0') AS a1r, IF (a2>b2,'1','0') AS a2r,
我尝试了几种不同的解决方案,但都没有成功。我给出的表格是一个示例,其设计和功能与我实际使用的表格类似: PK | Color | Count -------------------
我正在尝试构建一个查询来检查我的库存。 SELECT COUNT(*) AS item_count, reseller_id, sum(sold) as sold_count, sum(refunde
我试图解决一个看起来像下面编写的代码的问题,但由于缺乏知识和阅读 sqlalchemy 文档,我还没有真正找到解决问题的方法。 目标: 如果 year_column 中的年份相同,则获取 sales_
我有一个包含一周中多天的表格。一周中的每一天都有独特的属性,例如冰淇淋是否在这一天成功送达: ID DAY_WEEK ICE_CREAM 1 Monday
首先,我有一个名为store_00的表 id | ref | item | qty | cost | sell 1 22 x1 5 10 15 2 22
我正在编写一个程序,计算每个数字的总和,直到 1000。例如,1+2+3+4+5....+100。首先,我将求和作业分配给 10 个处理器:处理器 0 得到 1-100,处理器 1 得到 101-20
我想在一个循环中一次对多个属性求和: class Some(object): def __init__(self, acounter, bcounter): self.acou
我是一名优秀的程序员,十分优秀!