【问题标题】:How to add arrays to a list and iterate to get the first values of each array?如何将数组添加到列表并迭代以获取每个数组的第一个值?
【发布时间】:2019-03-19 00:53:26
【问题描述】:

这就是问题陈述的方式:

假设有 4 个字符串数组 - [open,providers] , [1,2] , [yes,no] , [oe,pr]

我想将 4 个数组添加到名为 'series' 的列表中。

所以系列列表现在就像 - [[open,providers],[1,2],[yes,no],[oe,pr]]

现在我想遍历列表以从列表中的每个数组中获取第一个值,即将 [open,1,yes,oe] 和 [providers,2,no,pr] 作为两个单独的数组或列表。

如何解决这个问题? 帮助代码段! 谢谢 !

【问题讨论】:

    标签: arrays list arraylist iteration


    【解决方案1】:

    你应该这样做。我在这里测试过,它工作正常。只需改进您的println 即可随心所欲地显示您的文字。现在由你决定。研究代码。

    import java.util.ArrayList;
    
    
    public class Main {
    
        public static void main(String args[]) {
            String[] array1 = {"open", "providers"};
            String[] array2 = {"1", "2"};
            String[] array3 = {"yes", "no"};
            String[] array4 = {"oe", "pr"};
    
            ArrayList<String[]> result = myArrays(array1, array2, array3, array4);
    
            for(int i = 0; i < result.size(); i++) {
                for(int j = 0; j < result.get(i).length; j++) {
                    if(i == 0) {
                        System.out.println("Result of first array: " + result.get(i)[j]);
                    } else {
                        System.out.println("Result of second array: " + result.get(i)[j]);
                    }   
                }
            }
        }
    
    
        public static ArrayList<String[]> myArrays(String[] array1, String[] array2, String[] array3, String[] array4) {
    
            String[][] series = new String[4][];
            series[0] = array1;
            series[1] = array2;
            series[2] = array3;
            series[3] = array4;
    
            String[] firstResult = new String[4];
            String[] secondResult = new String[4];
    
            for(int i = 0; i < series.length; i++) {
                for(int j = 0; j < series[i].length; j++) {
                    if(j == 0) {
                        firstResult[i] = series[i][j];
                    } else {
                        secondResult[i] = series[i][j];
                    }
                }
            }
    
            ArrayList<String[]> finalResult = new ArrayList<String[]>();
    
            finalResult.add(firstResult);
            finalResult.add(secondResult);
    
            return finalResult;
        }
    }
    

    【讨论】:

    • 这看起来很棒!现在如果数组中有更多类似 {open,prov,...,...}{1,2,3,....} 怎么办
    • 它也可以,但是您必须添加更多参数。如果您需要三个单独的结果,请添加第三个结果。如果您想要 5 个数组作为输入,请创建并添加第五个。以此类推。
    【解决方案2】:
        String[] arr1 = {"open","providers"};
        String[] arr2 = {"1","2"};
        String[] arr3 = {"yes","no"};
    
        List<String[]> addList = new ArrayList<String[]>();
        for (int n = 0; n < arr1.length; n++) {
            String[] rowArray = { arr1[n], arr2[n],
                    arr3 [n]};
            addList.add(rowArray);
    
        }
    

    这是提供的,数组的长度相似,可以很容易地容纳!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-05
      • 2016-11-13
      • 1970-01-01
      • 1970-01-01
      • 2023-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多