Take a number (1 to 10 length) transform it to indexes and base of this indexes retrieve the specific Strings from an array in Java

Viewed 61

I want to take from rotatedZeros the numbers in every array, one by one, and transform it to indexes, as to retrieve the String/s from courses an put those array in finalExcelFormula . Or can anybody explain how this is done with Map.

rotatedZeros contains all posible unique numbers make un from all digits.

Ex: rotatedZeros.get(3) = [1,3,4]; I want to return from the courses ArrayList the string at indexes 1 ,3 and 4 and put this array inside finalExcelFormula array. Obs. The numbers are always ascending !

List<Integer[]> rotatedZeros;
List<String[]> finalExcelFormula = new ArrayList<>(1023); 
static List<String> courses = new ArrayList<>();

        courses.add("\"Vue\"");
        courses.add("\"Mathematics\"");
        courses.add("\"Geography\"");
        courses.add("\"Physics\"");
        courses.add("\"Sport\"");
        courses.add("\"Literature\"");
        courses.add("\"Design\"");
        courses.add("\"C++\"");
        courses.add("\"Java\"");
        courses.add("\"C#\"");

private void finalExcelFormula() {
        for(int i = 0; i < rotatedZeros.size(); i++) {
            for(int e = 0; e < rotatedZeros.get(i).length; e++){
                Arrays.stream(finalExcelFormula.get(i)).toArray(courses.get(rotatedZeros.get(i)[e]));
            
            }
        }
    }
2 Answers

to construct the finalExcelFormula, you need tto stream over the Integer[] and create corresponding String[] for them while looking up the courses, something over the lines of:

List<String[]> finalExcelFormula = rotatedZeros.stream()
        .map(rotatedZeroArray -> Arrays.stream(rotatedZeroArray)
                .map(courses::get)
                .toArray(String[]::new))
        .collect(Collectors.toList());

Even though i read your question about 4 times, i have no clue as to what you're trying to do...

so here is a rudimentary hasmap implementation ...

public static void main(String[] args) {

    ArrayList<Integer[]> rotatedZeros = new ArrayList();

    rotatedZeros.add(new Integer[]{1, 3, 4});
    rotatedZeros.add(new Integer[]{1, 2, 5});
    rotatedZeros.add(new Integer[]{1, 2, 3});
    rotatedZeros.add(new Integer[]{0, 1, 2});

    HashMap<Integer, String> things = new HashMap<>();
    things.put(1, "vous");
    things.put(2, "le");
    things.put(3, "vous");
    things.put(4, "couche");
    things.put(5, "avec");
    things.put(6, "moi");

    for (Integer[] rotatedZeroArray : rotatedZeros) {
        StringBuilder sb = new StringBuilder();
        for (Integer integer : rotatedZeroArray) {
            sb.append(things.get(integer)).append(" ");
        }
        System.out.println("output:" + sb.toString().trim());
    }

    // or weirder...
    ArrayList<String> courses = new ArrayList<>();
    courses.add("\"Vue\"");
    courses.add("\"Mathematics\"");
    courses.add("\"Geography\"");
    courses.add("\"Physics\"");
    courses.add("\"Sport\"");
    courses.add("\"Literature\"");
    courses.add("\"Design\"");
    courses.add("\"C++\"");
    courses.add("\"Java\"");
    courses.add("\"C#\"");

    HashMap<Integer, String> things2 = new HashMap();

    int counter = 0;
    for (String course : courses) {
        things2.put(counter++, course);
    }

    for (Integer[] rotatedZeroArray : rotatedZeros) {
        StringBuilder sb = new StringBuilder();
        for (Integer integer : rotatedZeroArray) {
            sb.append(things2.get(integer)).append(" ");
        }
        System.out.println("output:" + sb.toString().trim());
    }
    
}
Related