[英]In java, how do I increase the index of two array(list)s of different sizes at the same time?
I have two ArrayLists in java that have different sizes. 我在Java中有两个具有不同大小的ArrayList。 ArrayLists 'probs' and 'theDoubles' (sizes 4 and 5 respectively).
ArrayLists'probs'和'theDoubles'(大小分别为4和5)。 Array 'dprobs' (Double[5]) is just an Array that copies the values of 'theDoubles'.
数组'dprobs'(Double [5])只是一个复制'theDoubles'值的数组。
Only those items that do not have zero-length (see '.size() > 0') are allowed to be added. 仅允许添加那些长度不为零的项目(请参见“ .size()> 0”)。 One of them is zero-length, and gets replaced by a '0.0' double (added to the 'theDoubles' ArrayList).
其中之一是零长度,并被替换为“ 0.0”双精度字(添加到“ theDoubles” ArrayList中)。
My problem is that I do not know how to get this desired output: 我的问题是我不知道如何获得此期望的输出:
i=1 j=0
i=2 j=1
i=3 j=2
i=4 j=3
j=4
I get this instead: 我得到这个代替:
i=1 j=1
i=2 j=2
i=3 j=3
i=4 j=4
Here is my code: 这是我的代码:
for (int i = 0; i < 5; i++) {
if (probs.get(i).size() > 0) {
System.out.println("i: " + i);
System.out.println("j: " + j);
theDoubles.add(Double.parseDouble(probs.get(i).get(0).getValue()));
dprobs[j] = theDoubles.get(j);
} else {
theDoubles.add(0.0);
}
j++;
}
return dprobs;
I need to display this ('probs' ArrayList contents): 我需要显示以下内容(“问题” ArrayList内容):
0.0
0.0049522
0.0020487
0.0013568
0.0015332
and I am only getting this: 我只得到这个:
0.0049522
0.0020487
0.0013568
0.0015332
because 'j' starts at '1' (it ignores the first item, which is '0.0' at index 0). 因为“ j”从“ 1”开始(它会忽略第一项,在索引0处为“ 0.0”)。
Any ideas? 有任何想法吗?
Thanks 谢谢
It is hard to see the full picture because nothing shows where you print the second set of outputs and desired outputs. 很难看到完整的图片,因为在第二组输出和所需的输出上没有显示任何内容。 therefore i will try based on the first set that you posted: you need to increase j only when you are at an i which points to an element that is not zero length.
因此,我将根据您发布的第一个集合进行尝试:仅当您在i指向不为零长度的元素时,才需要增加j。 therefore, move the j++ to your "if" block too.
因此,也将j ++移到“ if”块中。 it becomes like this:
它变成这样:
for (int i = 0; i < 5; i++) {
if (probs.get(i).size() > 0) {
System.out.println("i: " + i);
System.out.println("j: " + j);
theDoubles.add(Double.parseDouble(probs.get(i).get(0).getValue()));
dprobs[j] = theDoubles.get(j);
j++;
} else {
theDoubles.add(0.0);
}
}
return dprobs;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.