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 of Groovy and Java, which is the correct way to create the list?
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 of Groovy and Java, which is the correct way to create the list?
6
I’m guessing you own one array of String and that is seeking to get the first two characters. You didn’t mention whether they should have whole type on the second list, so I’m considering that the second list will also be strings. If you need them whole, use Integer#parseInt
.
You can use String#substring
to "cut" the first two characters of each string. Or you can use the point as the delimiter for the method String#split
, breaking the string into a array and getting their first index.
I don’t know Groovy very well but I risked an answer. They may have more efficient ways than I tried here.
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]
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]
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]
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]
4
Stayed 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!
3
You cannot remove elements from a basic array in Java, Collections
and ArrayList
so you can remove correctly.
To help you, I took your array and created a new one with Arraylist,.
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 have tested and works perfectly, if you have more questions take a look at the following documentation :
https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
2
In Groovy:
def lista = ['01.um','02.dois','03.tres']
Option 1 (if the numbers are always formed by two digits):
def novaLista = lista.collect {it.take(2)}
def novaLista = lista*.take(2) //versao compacta
Option 2 (numbers are formed by a number of arbitrary digits, but there is always the "." to separate them):
def novaLista = lista.collect {it.tokenize(".").first()}
See working on Ideone.
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 point (literal). The 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 a string array (String[]
) and the result of tokenize()
is a ArrayList
.
0
Complementing the @Renan response, it follows 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: could have used Static import for the code to be leaner.
Browser other questions tagged java array list groovy
You are not signed in. Login or sign up in order to post.