Remove Switch case by a for?

-2

I do not know how to face the following:

I have the following code part:

  for(int i = 0; i < 2; i++){
            ArrayList<String> dade = new ArrayList<String>();
            switch (i) {
                case 0:
                    dade.add(new String("1-"));
                    dade.add(new String("a)"));
                    break;
                case 1:
                    dade.add(new String("2-"));
                    dade.add(new String("b)"));
                    break;
            }
            dades.add(dade);
        }

The idea is to fill the array strings with 2 data. I mean the screen now shows me the following

1- a)

2- b)

The problem is that I do not want it to be using Switch / Case since the data has to be filled in manually. And I do not want there to be a maximum ..

How can I do it? I know it's a long question but I do not know how to put it.

The user will enter two data and it will be placed in a recyclerView.

That is, the result of the screen is the following, which is what I need. The problem is that these data are entered by the user and I have a swtich case. I hope to explain better now.

thanks!

    
asked by Montse Mkd 16.10.2018 в 15:33
source

2 answers

0
/*Lo que hace el siguiente codigo es agregar de manera automatica (hasta un limite de 27) las letras en el orden que pide:
1-a)
2-b) 
etc.
Se crear un arreglo con las letras necesarias:*/
    String[] abc = new String{"a","b","c",....};
/*    Agregas un limite para el for Puse 27 por las letras del abecedario*/

        for(int i = 0; i < 27; i++){
                ArrayList<String> dade = new ArrayList<String>();
/*Aqui se hace una contatenacion de los datos, (i+1) es para que que no empiece a contar desde el 0.*/


  dade.add(new String((i+1) +"-" +abc[i] + ")"));
            dades.add(dade);
        }
    
answered by 16.10.2018 в 15:44
0

I do not finish understanding your question,
You need to be able to make a list of lists with 2 values with the X-Y format) where X is number and Y is a letter of the alphabet, right?

for(int i = 0; i < size; i++){
    ArrayList<String> dade = new ArrayList<String>();
    dade.add(String.valueOf((i+1)+"-"));
    if(i+65<=90){
        dade.add(String.valueOf(((char)(i+65))+")"));
    }else{
        int hlpr = i/26; // cuantas veces nos pasamos de la Z?
        if(hlpr<2){
            String aux = String.valueOf(((char)(hlpr+64)));
            int j = (i - (25*hlpr))+64;
            j = j<0? j*-1 : j;
            aux+=String.valueOf(((char)(j)));
            dade.add(String.valueOf(aux+")"));
        }else{
            //ya si te pasas de 2 veces, quizás necesites una función recursiva para seguir añadiendo Letras
        }
    }
    dades.add(dade);
}

If so, with a cycle more or less like this, you should be able to add letters according to the size delivered, occupying char whose numeric value is governed by the table ASCII

    
answered by 16.10.2018 в 17:31