简体   繁体   English

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

[英]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.

相关问题 如何比较两个不同大小的列表对象 Java - How to compare two different sizes List Objects Java 如何将两个数组字符串的值求和并存储到Java Selenium中的另一个数组列表中? - How to do sum of two array string's value and store into another array list in java selenium? 如何使用Java在硒中控制两个不同的帐户是否可以同时调用? - how can i control whether two different accounts can call or not at the same time in selenium using Java? 如何渲染不同大小的图块? - How do I Render Tiles at different sizes? 如何在 java 中为同一文本区域使用不同的字体类型和大小 - how can i use different font types and sizes for the same text area in java 从具有两个不同大小/长度的两个arraylist获取相同值的索引 - Get index of the same value from two arraylist with two different sizes/length 如何使用 on 方法获取两个不同大小的数组列表的大小? - How do I use on method to get the sizes of two different-sized arrayslists? JAVA:如何添加两个声明相同名称但值不同的处理值 - JAVA: How do I add two processed values with same name declared but different values 在Java中,如何为同一数据类型在两个不同的列中设置单元格编辑器? - In Java, how do I set a cell Editor in two different columns for the same data type? Java如何创建一个接受两个列表类型的方法,而不是同时 - Java How to make a method that accepts two list types, not at the same time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM