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

时间:2022-10-24 14:07:26

I have two ArrayLists in java that have different sizes. ArrayLists 'probs' and 'theDoubles' (sizes 4 and 5 respectively). Array 'dprobs' (Double[5]) is just an Array that copies the values of 'theDoubles'.

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

Only those items that do not have zero-length (see '.size() > 0') are allowed to be added. One of them is zero-length, and gets replaced by a '0.0' double (added to the 'theDoubles' ArrayList).

只允许添加那些没有零长度的项(参见'.size()> 0')。其中一个是零长度,并被一个'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):

我需要显示这个('probs'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

1 个解决方案

#1


0  

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. therefore, move the j++ to your "if" block too. it becomes like this:

很难看到完整的图片,因为没有任何内容显示您打印第二组输出和所需输出的位置。因此,我将根据您发布的第一个集合尝试:只有当您处于指向长度不为零的元素的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;

#1


0  

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. therefore, move the j++ to your "if" block too. it becomes like this:

很难看到完整的图片,因为没有任何内容显示您打印第二组输出和所需输出的位置。因此,我将根据您发布的第一个集合尝试:只有当您处于指向长度不为零的元素的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;