Dynamic Array - Android

1

How to create a dynamic array in this way:

//Apenas um exemplo abaixo:

String valor0 = "1;2;3;4;5;6;7;8;9;0";
String valor1 = "1;2;3;4;5;6;7;8;9;0";
String valor2 = "1;2;3;4;5;6;7;8;9;0";

//Um array dentro de outro array, porém sem definir seu tamanho
String[][] valor = {};

//E então definir os valores do segundo [] com split também
valor[0][] = valor0.split(";");
valor[1][] = valor1.split(";");
valor[2][] = valor2.split(";");

So he would create the keys and after creating, set the values of the other array inside each key with the split. The size should be with the split, because this will be dynamic, will change constantly. In cases of just being a normal array, I know that split populate it dynamically without having to set the size, but I'd like to do the same with a two-dimensional array.

The example does not work, of course, it's just for show.

The array looks something like this:

array (size=3)
  0 => 
    array (size=10)
      0 => string '1' (length=1)
      1 => string '2' (length=1)
      2 => string '3' (length=1)
      3 => string '4' (length=1)
      4 => string '5' (length=1)
      5 => string '6' (length=1)
      6 => string '7' (length=1)
      7 => string '8' (length=1)
      8 => string '9' (length=1)
      9 => string '0' (length=1)
  1 => 
    array (size=10)
      0 => string '1' (length=1)
      1 => string '2' (length=1)
      2 => string '3' (length=1)
      3 => string '4' (length=1)
      4 => string '5' (length=1)
      5 => string '6' (length=1)
      6 => string '7' (length=1)
      7 => string '8' (length=1)
      8 => string '9' (length=1)
      9 => string '0' (length=1)
  2 => 
    array (size=10)
      0 => string '1' (length=1)
      1 => string '2' (length=1)
      2 => string '3' (length=1)
      3 => string '4' (length=1)
      4 => string '5' (length=1)
      5 => string '6' (length=1)
      6 => string '7' (length=1)
      7 => string '8' (length=1)
      8 => string '9' (length=1)
      9 => string '0' (length=1)

I made this example in php

    
asked by anonymous 05.04.2018 / 22:31

3 answers

1

If you need the array to be dynamic use an implementation of List , such as ArrayList .

According to the example that gave only the rows would need to be dynamic, in which case you could define the array as:

ArrayList<String[]> valor

This allows you to add as many array of% s as you want. The added arrays themselves do not have to be the same size and so they can catch the size coming from String . The addition is made using the method split instead of the index.

Example:

String valor0 = "1;2;3;4;5;6;7;8;9;0";
String valor1 = "1;2;3;4;5;6;7;8;9;0";
String valor2 = "1;2;3;4;5;6;7;8;9;0";

ArrayList<String[]> valor = new ArrayList<>();

valor.add(valor0.split(";")); //adiciona cada linha com add
valor.add(valor1.split(";"));
valor.add(valor2.split(";"));

Now to use just two add one for the rows and another for the columns:

for (String[] linha : valor){ //percorrer cada linha
    for (String item : linha){ //percorrer cada item/coluna na linha
        System.out.print(item + " ");
    }
    System.out.println();
}

See this example in Ideone

Note that with for to access a position, you must use the ArrayList method. So if you want to later access the second value of the first line you would have to do:

String segundoPrimeiraLinha = valor.get(0)[1];
//------------------------------------^

Where in get would:

String segundoPrimeiraLinha = valor[0][1];
    
05.04.2018 / 23:41
1

Just to show that this is possible with a multidimensional array, just like the AP requests.

String valor0 = "1;2;3;4;5;6;7;8;9;0";
String valor1 = "1;2;3;4;5;6;7;8;9;0";
String valor2 = "1;2;3;4;5;6;7;8;9;0";

String[][] valor = new String[3][];

valor[0] = valor0.split(";");
valor[1] = valor1.split(";");
valor[2] = valor2.split(";");

for(String[] linha : valor) {           
    for (String item : linha) {     
        System.out.print(item + " ");
    }
    System.out.println();
}

See the Ideone .

    
06.04.2018 / 00:13
0

Using List you can create them dynamically following the code :

List<List<String>> valor = new ArrayList<ArrayList<String>>();
valor.add(Arrays.asList(valor0.split(";"));
valor.add(Arrays.asList(valor1.split(";"));
valor.add(Arrays.asList(valor2.split(";"));
    
05.04.2018 / 23:44