简体   繁体   English

Android Java将两个类似的for循环合并为一个循环

[英]Android java make two similar for loops into one loop

Im trying to merge these two for loops together so that i can use it for one fillview. 我试图将这两个for循环合并在一起,以便我可以将其用于一个fillview。

Ive been trying to try for a few days but am unsure how to do this so would appreciate some help! 我已经尝试了几天,但是不确定如何执行此操作,因此将不胜感激!

Any help is appreciated 任何帮助表示赞赏

    int maxDataSetSize = Math.max(data1.size(), Math.max(data2.size(), data3.size())) ;

    for (int i = 0; i < maxDataSetSize; i++) {
        String dataset1Value = data1.size() > i ? data1.get(i) : null;
        String dataset2Value = data2.size() > i ? data2.get(i) : null;
        String dataset3Value = data3.size() > i ? data3.get(i) : null;

        View statsRowview = getLayoutInflater().inflate(R.layout.stats_row, null);

        fillView(statsRowview, dataset1Value, dataset2Value, dataset3Value);

        tableStats.addView(statsRowview);
    }


    int maxDataSetSize2 = Math.max(data4.size(), Math.max(data5.size(), data6.size()));
    for (int i = 0; i < maxDataSetSize2; i++) {
        String dataset4Value = data4.size() > i ? data4.get(i) : DEFAULT_COLOR;
        String dataset5Value = data5.size() > i ? data5.get(i) : DEFAULT_COLOR;
        String dataset6Value = data6.size() > i ? data6.get(i) : DEFAULT_COLOR;

        View statsRowview = getLayoutInflater().inflate(R.layout.stats_row, null);

        fillView2(statsRowview, dataset4Value, dataset5Value, dataset6Value);

        tableStats.addView(statsRowview);
    }
}

Try this: 尝试这个:

int maxDataSetSize = Math.max(data1.size(), Math.max(data2.size(), data3.size())) ;
int maxDataSetSize2 = Math.max(data4.size(), Math.max(data5.size(), data6.size()));

for (int i = 0, j = 0; i < maxDataSetSize || j < maxDataSetSize2; i++, j++) {

    View statsRowview = getLayoutInflater().inflate(R.layout.stats_row, null);

    if(i < maxDataSetSize){
        String dataset1Value = data1.size() > i ? data1.get(i) : null;
        String dataset2Value = data2.size() > i ? data2.get(i) : null;
        String dataset3Value = data3.size() > i ? data3.get(i) : null;
        fillView(statsRowview, dataset1Value, dataset2Value, dataset3Value);
    }


    if(j < maxDataSetSize2){
        String dataset4Value = data4.size() > j ? data4.get(j) : DEFAULT_COLOR;
        String dataset5Value = data5.size() > j ? data5.get(j) : DEFAULT_COLOR;
        String dataset6Value = data6.size() > j ? data6.get(j) : DEFAULT_COLOR;
        fillView2(statsRowview, dataset4Value, dataset5Value, dataset6Value);
    }

    tableStats.addView(statsRowview);
}

This should work. 这应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java中是否存在FOR…IN…类似于Python的循环? - Is there FOR … IN … loop in Java similar to Python one? 重构两个相似的if循环,其值略有不同(Java) - Refactoring two similar if loops with slightly different values (Java) 使用两个循环,但只有一个循环运行。 计算机科学导论(Java) - Using two loops but only one loop runs through. Intro to computer science.(Java) 如何在Java中优化两个for循环(for循环内的for循环) - How to optimize two for loops (a for loop inside a for loop) in java 查找BigO-一个while循环与两个for循环嵌套 - Finding BigO - One while-loop nested with two for-loops 在一个while循环内的两个嵌套while循环之间切换 - Switching between two nested while loops within one while loop 一个循环中的两个操作与两个循环在每个循环中执行相同的操作 - Two operations in one loop vs two loops performing the same operations one per loop java中使用两个for循环遍历二维数组 - Using two for loops to loop through a 2D array in java 查找两个嵌套的while循环的循环不变java - finding loop invariant for two nested while loops java Java两个外部for循环,两个内部for循环添加到字符串中,第一个不执行任何操作 - Java two for outer for loops with two inner for loops that add to a string and the first one doesn't do anything
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM