How to remove 2 digits from each list item?

4

I have a list / array:

var lista['01.um','02.dois','03.tres']

I need to create a new list like this:

lista['01','02','03']

I know little about Groovy and Java, what is the correct way to create the list?

    
asked by anonymous 19.06.2016 / 20:55

6 answers

6

I'm guessing that you have a string array and you're looking to get the first two characters. You did not mention if they should have integer type in the second list, so I'm considering that the second list will also be strings. If you need them to be integers, use Integer#parseInt .

You can use String#substring to "cut" the first two characters of each string. Or you can use the dot as a delimiter for the String#split ", breaking the string into an array and getting its first index.

I do not know Groovy very well but I risked an answer. They may have more efficient ways than I tried here.

Java

String[] array = {"01.um", "02.dois", "03.tres"};

for(int i = 0; i < array.length; i++)
    array[i] = array[i].substring(0, 2);

System.out.println(Arrays.toString(array));
// [01, 02, 03]

# result

String[] array = {"01.um", "02.dois", "03.tres"};

for(int i = 0; i < array.length; i++)
    array[i] = array[i].split("\.")[0];

System.out.println(Arrays.toString(array));
// [01, 02, 03]

# result

Groovy

lista = ['01.um', '02.dois', '03.tres']

for(i = 0; i < lista.size(); i++)
    lista[i] = lista[i].substring(0,2)

println lista
// [01, 02, 03]

# result

lista = ['01.um', '02.dois', '03.tres']

for(i = 0; i < lista.size(); i++)
    lista[i] = lista[i].tokenize('.')[0]

println lista
// [01, 02, 03]

# result

    
19.06.2016 / 23:10
4

It looks like this:

String[] arrayTipos= tiposSelecionados;

for(int i = 0; i < arrayTipos.length; i++)
    arrayTipos[i] = arrayTipos[i].substring(0, 2);

String[] consulta= "SELECT telas FROM interfaceUsuario WHERE tipo in(" + Arrays.toString(arrayServicos) +")";

return consulta;

Thank you!

    
20.06.2016 / 15:10
3

You can not remove elements from a basic array in Java, try Collections and ArrayList so you can remove them correctly.

To help you, I got your array and created a new one with ArrayList, to create and delete what you want.

Example of your code with ArrayList:

import java.util.*;

            String[] array = new String[] { "01.um", "02.dois", "03.tres" };

    System.out.println("Anterior : " + Arrays.toString(array));

    List<String> list = new ArrayList<String>(Arrays.asList(array));
    list.removeAll(Arrays.asList("01.um"));
    list.removeAll(Arrays.asList("02.dois"));
    list.removeAll(Arrays.asList("03.tres"));
    list.add("01");
    list.add("02");
    list.add("03");
    array = list.toArray(array);

    System.out.println("Atual : " + Arrays.toString(array));

I've tested it and it works perfectly, if you have more questions, take a look at the following documentation:

link

/ p>     
19.06.2016 / 22:57
2

In Groovy:

def lista = ['01.um','02.dois','03.tres']

Option 1 (if numbers are always two-digit):

def novaLista = lista.collect {it.take(2)}
def novaLista = lista*.take(2) //versao compacta

Option 2 (numbers are formed by an arbitrary number of digits, but there are always "." to separate them):

def novaLista = lista.collect {it.tokenize(".").first()}

See working at Ideone .

    
30.06.2016 / 19:39
0

Complementing the @Renan response, follow one of the forms of java using version 8:

List<String> lista = Arrays.asList(new String[] {"01.um", "02.dois", "03.tres"});

List<String> listaModificada = lista.stream()
    .map(item -> item.substring(0, 2))
    .collect(Collectors.toList());

System.out.println(listaModificada);

Note: I could have used static import to make the code leaner.

    
07.03.2017 / 02:18
0

You can use split () or tokenize () to break the string into parts. After that, you can use collect to create the new list.

def lista = ['01.um', '02.dois', '03.tres']

def novaLista1 = lista.collect { it.split(/\./)[0] }
def novaLista2 = lista.collect { it.tokenize('.')[0] }

println novaLista1 // => [01, 02, 03]
println novaLista1.class.name // java.util.ArrayList
println novaLista2 // => [01, 02, 03]
println novaLista2.class.name // java.util.ArrayList

The split method is given a regular expression to find the (literal) point. Tokenize works only with a string inside quotes, without having to escape the point.

Although the result is the same in the two choices above, the product of split() is an array of String ( String[] ) and the result of tokenize() is ArrayList .

    
07.03.2017 / 02:03