繁体   English   中英

在Java中,如何同时增加两个不同大小的数组(列表)的索引?

[英]In java, how do I increase the index of two array(list)s of different sizes at the same time?

我在Java中有两个具有不同大小的ArrayList。 ArrayLists'probs'和'theDoubles'(大小分别为4和5)。 数组'dprobs'(Double [5])只是一个复制'theDoubles'值的数组。

仅允许添加那些长度不为零的项目(请参见“ .size()> 0”)。 其中之一是零长度,并被替换为“ 0.0”双精度字(添加到“ theDoubles” ArrayList中)。

我的问题是我不知道如何获得此期望的输出:

i=1 j=0
i=2 j=1
i=3 j=2
i=4 j=3
    j=4

我得到这个代替:

i=1 j=1
i=2 j=2
i=3 j=3
i=4 j=4

这是我的代码:

        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;

我需要显示以下内容(“问题” ArrayList内容):

0.0
0.0049522
0.0020487
0.0013568
0.0015332

我只得到这个:

0.0049522
0.0020487
0.0013568
0.0015332

因为“ j”从“ 1”开始(它会忽略第一项,在索引0处为“ 0.0”)。

有任何想法吗?

谢谢

很难看到完整的图片,因为在第二组输出和所需的输出上没有显示任何内容。 因此,我将根据您发布的第一个集合进行尝试:仅当您在i指向不为零长度的元素时,才需要增加j。 因此,也将j ++移到“ if”块中。 它变成这样:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM