在Android Studio中过滤和计算数组值

时间:2019-06-24 20:18:03

标签: java android

我正在尝试制作测验/测试应用程序。我有4个值作为字符串(从以前的活动中,由用户通过单选按钮选择)传递给上一个“结果”活动,如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_auswertung);

    Intent intent = getIntent();
    final String strSommerWinter = intent.getStringExtra("strSommerWinter");
    final String strUrlaubsart = intent.getStringExtra("strUrlaubsart");
    final String strRegion = intent.getStringExtra("strRegion");
    final String strAnreise = intent.getStringExtra("strAnreise");

}

它们都是数字(我猜想可以/应该将它们更改为整数),例如1、1、6、10。 然后我有这个数组:

int [][] destiBewertung = {{3,4,2,0,1,3,5,0,0,2,3,3,5,4},
                            {1,3,5,4,4,4,3,3,2,5,0,0,5,4},
                            {3,3,1,4,5,4,2,5,4,3,0,0,5,4},
                            {1,3,4,4,4,3,4,3,0,5,5,3,4,4},
                            {1,3,3,4,4,5,2,4,4,5,0,0,5,4},
                            {3,4,3,1,1,3,5,0,1,0,5,5,5,0},
                            {3,5,5,2,2,3,5,0,0,1,5,5,5,3},
                            {3,5,5,0,1,3,5,0,0,0,5,5,5,0},
                            {3,2,1,3,4,4,1,5,0,2,5,4,0,1},
                            {3,1,5,3,1,3,2,2,0,5,3,0,5,0},
                            {3,4,3,3,3,2,5,0,2,5,5,5,5,3},
                            {3,5,3,3,5,3,2,4,5,5,0,0,5,4},
                            {3,4,3,4,4,3,4,3,3,5,0,0,5,3},
                            {3,3,2,4,4,3,3,5,1,5,5,5,5,3},
                            {3,5,3,0,2,3,5,0,0,0,5,5,5,0},
                            {3,5,3,2,2,3,5,1,3,4,3,0,5,3},
                            {3,4,5,1,2,3,5,0,0,0,5,5,5,3},
                            {3,4,2,2,3,3,5,2,3,1,3,3,5,1},
                            {3,4,3,4,3,2,5,2,2,5,4,3,5,3},
                            {3,4,5,1,1,3,5,0,0,0,5,5,5,0},
                            {3,3,2,4,5,4,2,4,4,5,5,0,2,0},
                            {1,3,0,4,5,5,0,5,4,3,3,4,0,5},
                            {3,2,3,4,5,4,2,4,2,5,3,3,4,3},
                            {3,4,3,2,1,3,5,0,2,4,5,3,5,3},
                            {3,4,3,3,2,3,5,0,0,5,5,5,5,5},
                            {3,5,3,1,1,3,5,0,0,0,5,5,5,0},
                            {3,5,3,1,1,3,5,0,0,0,5,5,5,4},
                            {3,4,0,5,5,5,1,5,4,0,3,0,4,0},
                            {3,4,4,1,2,3,5,2,1,3,0,0,5,4},
                            {3,5,3,2,1,3,5,0,1,0,5,5,5,0},
                            {3,4,3,1,1,3,5,0,0,0,5,5,5,0},
                            {3,5,4,0,0,2,5,1,0,1,1,4,4,4},
                            {1,5,3,1,1,3,5,0,2,4,3,3,5,3},
                            {3,4,4,1,0,3,5,0,0,0,5,5,5,0},
                            {1,2,1,4,5,5,1,5,3,5,5,5,5,4},
                            {3,3,3,2,1,3,5,1,0,0,5,5,5,1},
                            {2,1,1,3,5,5,1,5,5,0,5,5,5,0},
                            {3,1,1,4,5,4,0,5,5,0,5,5,3,0},
                            {1,1,0,5,5,4,0,5,0,5,3,3,0,5},
                            {1,3,3,4,5,5,3,5,3,5,2,0,4,4}
};

第一列(位置0?)为1、2或3。如果第一字符串(strSommerWinter)为1,则仅应进一步考虑第一个值为1的行。 最后3个字符串(strUrlaubsart,strRegion,strAnreise)是应加总的数组的列。这张图片应该使我更清楚地知道要做什么: screenshot

我想我需要ArrayList和一个新的数组作为结果..但是我不知道如何开始。非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您可以使用以下功能:

private List<Integer> getSelectedRowSums(
        int sommerWinter,
        int urlaubsart,
        int region,
        int anreise,
        int[][] destiBewertung
) {
    List<Integer> selectedRowSumList = new ArrayList<>();
    for (int[] columns : destiBewertung) {
        if (columns[0] == sommerWinter) {
            int sum = columns[urlaubsart] + columns[region] + columns[anreise];
            selectedRowSumList.add(sum);
        }
    }
    return selectedRowSumList;
}