0

I have a List with 2 lists inside , like a matrix, after I populate this list, I excecute some calculations with a Loop, to populate a third List, but this doesnt work correctly. I have an if statemnt to check if the values are the same, but it only works once, after it found a match it doesnt find another match again, sorry for the code im a begginer

Here is the code:

List<List<String>> dynamic2D = new ArrayList<List<String>>();
dynamic2D.add(new ArrayList<String>());
dynamic2D.add(new ArrayList<String>());

dynamic2D.get(0).add("type2"); //populate first inner list
dynamic2D.get(0).add("type3");
dynamic2D.get(0).add("type1");
dynamic2D.get(0).add("type2");

dynamic2D.get(1).add("1"); //populate second inner list
dynamic2D.get(1).add("3");
dynamic2D.get(1).add("5");
dynamic2D.get(1).add("6");

ArrayList<String> amountOfTypes = new ArrayList<>(); //After deleting repeated values I get the amount of types existing on my first inner list
amountOfTypes.add("type1");
amountOfTypes.add("type2");
amountOfTypes.add("type3");


ArrayList<Integer> amountOfValuesOfTypes =new ArrayList<>();
// I populate this list with 0 values to later make a Sum of each value existing in the second inner list that correspond to the first inner list

for(int i = 0; i<amountOfTypes.size();i++){ // Here I have a list with 3 zero values, {0,0,0}, because there are 3 types of values
    amountOfValuesOfTypes.add(0); 
}

// Now here is the problem, Im trying to sum the respective values of each type, that correspond with the second inner list. in order to get the raw amount.

for(int i = 0; i<amountOfTypes.size();i++){
    for(int j = 0; i<dynamic2D.get(0).size();j++){ // a second inner loop to compare the value of my main list(that has all the values) with the filtered list (that only have values that do not repeat)
        if(amountOfTypes.get(i) == dynamic2D.get(0).get(j)){
            int secondListIntToString= Integer.valueOf(dynamic2D.get(1).get(j)); // Here I get the corresponding match Integer Value
            amountOfTypes.set(i,amountOfValuesOfTypes.get(i)+secondListIntToString); // Here I take the values that match by THE CRITERIA OF HAVING THE SAME VALUE IN THE FIRST INNER LIST, and make a sum to have the full value, but when there is a match it only sum ONCE, and it doesnt happen again

        }
    }

}

Thanks for any help.

1 Answers1

0

i can't understand the variable cantIntensidades.get(i) here is a proposition for code organizing

        List<ArrayList<String>> dynamic2D = new ArrayList<ArrayList<String>>();
        ArrayList<String>tempo = new ArrayList<>();

        tempo.add("type2");//populate first inner list
        tempo.add("type3");
        tempo.add("type1");
        tempo.add("type2");
        dynamic2D.add(new ArrayList<String>(tempo));
        tempo.clear();

        tempo.add("1");//populate second inner list
        tempo.add("3");
        tempo.add("5");
        tempo.add("6");
        dynamic2D.add(new ArrayList<String>(tempo));


        ArrayList<String> amountOfTypes = new ArrayList<>(); //After deleting repeated values I get the amount of types existing on my first inner list
        amountOfTypes.add("type1");
        amountOfTypes.add("type2");
        amountOfTypes.add("type3");


        ArrayList<Integer> amountOfValuesOfTypes =new ArrayList<>();
        // I populate this list with 0 values to later make a Sum of each value existing in the second inner list that correspond to the first inner list

        for(int i = 0; i<amountOfTypes.size();i++){ // Here I have a list with 3 zero values, {0,0,0}, because there are 3 types of values
            amountOfValuesOfTypes.add(0); 
        }

        // Now here is the problem, Im trying to sum the respective values of each type, that correspond with the second inner list. in order to get the raw amount.

        for(int i = 0; i<amountOfTypes.size();i++){
            for(int j = 0; j<dynamic2D.get(0).size();j++){ // a second inner loop to compare the value of my main list(that has all the values) with the filtered list (that only have values that do not repeat)
                if(amountOfTypes.get(i).equals(dynamic2D.get(0).get(j))){
                    int secondListIntToString= Integer.valueOf(dynamic2D.get(1).get(j)); // Here I get the corresponding match Integer Value
                    amountOfTypes.set(i,amountOfTypes.get(i)+secondListIntToString); // Here I take the values that match by THE CRITERIA OF HAVING THE SAME VALUE IN THE FIRST INNER LIST, and make a sum to have the full value, but when there is a match it only sum ONCE, and it doesnt happen again
                }
            }

        }
yasser
  • 91
  • 1
  • 8