Dynamic Array - Android

Asked

Viewed 206 times

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 within each key with the split. The size should be with the split, because this will be dynamic, will change constantly. In cases of being just a normal array, I know that the split populates it dynamically without having to define the size, but I would like to do the same with a two-dimensional array.

The example does not work, of course, is only to show.

The array would look 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 did this example in php

  • 1

    It seems to me you’re looking for ArrayList<String[]> and not String[][]

  • @Isac worse than I do not know actually kkkkk, could make an example :3

  • @Wotonsampaio you want integer or string array?

  • @Felipe de Strings

  • @Felipe was bad, it is pq in php I ended up leaving as int, already fixed here kkk

  • Quiet is that if it was whole would have a job to convert, before playing in vector

Show 1 more comment

3 answers

1


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

According to the example you gave only the lines 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 Strings you want. The arrays themselves added don’t have to be the same size so they can pick up the size that comes from the split. The addition is made with the method add instead of by 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 for one for rows and one for 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

It is worth remembering that with a ArrayList to access a position you must use the method get. So if you want later to access the second value of the first line would have to do:

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

Where in a String[][] would:

String segundoPrimeiraLinha = valor[0][1];
  • Man, I love you kkkkkkkk

  • @Wotonsampaio We’re here to help :)

1

Just to show that this is possible with a multidimensional array, as 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 on Ideone.

  • And if I didn’t have the size of the main array, being actually [][], it would also be possible?

  • It depends on how you save the data. However whatever the shape you know, or may always know, the number of lines(dimension of the "main array")

  • What is that String chave = "0,1,2";?

  • Would be the keys, but in vdd they don’t matter, I’ll even take here

  • It’s like I set the size of the array from that size, and then put the data into the other array, e.g.: array = chave.split(",") Then the main array would have a size of 3, so now I would add the values in the other array array[1][] = valor0.split(";");

  • It was just a crazy logic to explain my doubt

  • So I’m right when I say you always know the number of lines?

  • No, because, like, the values come from a db, and every time I add a new user, it increases, so I wouldn’t know how many would come, so I would need to be dynamic

  • Unless there was another array that just piled up , then I would create this new two-dimensional array using the size of that first array

  • The best thing would be for you to ask another question with the real scenario because, if I understood correctly, this should be done with another type of collection.

  • I think it is not necessary :/, I tested here with the arraylist and it worked well, I realized that you were interested in the subject kkk, but basically the doubt was to create a two-dimensional array without knowing its size, neither the main nor the array inside each key, so inserting it dynamically using the split()

Show 6 more comments

0

Using List you can create them dynamically follows 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(";"));
  • Dude, your example of an exeption in the 3 line where values :3 are added

  • Added the libraries?

  • Yes, I tested ideone.com, the error when adding was in vdd pq missed to close parentheses, so it looks like: valor.add(Arrays.asList(valor0.split(";"))); , but even so gave another Exception : Main.java:18: error: incompatible types: ArrayList<ArrayList<String>> cannot be converted to List<List<String>>

  • Check it out : https://ideone.com/VwTqoA

  • I think it would be right: ArrayList<ArrayList<String>> valor = new ArrayList<ArrayList<String>>();&#xA; valor.add((ArrayList<String>) Arrays.asList(valor0.split(";")));&#xA; valor.add((ArrayList<String>) Arrays.asList(valor1.split(";")));&#xA; valor.add((ArrayList<String>) Arrays.asList(valor2.split(";")));

  • Not sure, but only so android studio stopped showing as error

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.