How to remove 1 element from the Labels vector

Asked

Viewed 70 times

-2

Label[] posi={textf99, textf00, textf01, textf02, textf03, textf04, textf05, textf06, textf07, 
            textf08, textf09, textf10b, textf11, textf12, textf13, textf14, textf15, textf16, textf17, 
            textf18, textf19, textf20b, textf21b, textf22, textf23, textf24, textf25, textf26, textf27, 
            textf28, textf29, textf30b, textf31b, textf32b, textf33, textf34, textf35, textf36, textf37, 
            textf38, textf39, textf40b, textf41b, textf42b, textf43b, textf44, textf45, textf46, textf47, 
            textf48, textf49, textf50b, textf51b, textf52b, textf53b, textf54b, textf55, textf56, textf57, 
            textf58, textf59, textf60b, textf61b, textf62b, textf63b, textf64b, textf65b, textf66, textf67, 
            textf68, textf69, textf70b, textf71b, textf72b, textf73b, textf74b, textf75b, textf76b, textf77, 
            textf78, textf79, textf80b, textf81b, textf82b, textf83b, textf84b, textf85b, textf86b, textf87b, 
            textf88, textf89, textf90b, textf91b, textf92b, textf93b, textf94b, textf95b, textf96b, textf97b, 
            textf98b}; 

how to remove for example the label textf85b of the array?

  • Label[indicedesejado] = null this is the closest to removing an item from an object array.

  • must be posi[indicedesejado] = null or not

  • yes, it should be posi same, and you do not remove the Dice, only cancels the position where the object is

2 answers

0


I agree with the previous answer where the best option would be to use a list. However, it will not be possible to simply convert using Arrays.asList directly.

    List<Label> list = Arrays.asList(posi);
    list.remove(label);

This will generate an exception: java.lang.Unsupportedoperationexception. What the asList documentation says':

"This method Acts as bridge between array-based and Collection-based"

It’s like you can use it as a query, to list, but to manipulate it as a List because it’s actually like behind it’s still an array.

To get it right you really need to create a List:

    Label l0 = new Label("textf00");
    Label l1 = new Label("textf01");
    Label l2 = new Label("textf02");
    Label l3 = new Label("textf03");
    Label l4 = new Label("textf04");

    Label[] posi = { l0, l1, l2, l3, l4 };

    List<Label> list = new ArrayList<Label>(Arrays.asList(posi));

    list.remove(l1);

0

The size of an array cannot be changed, so a new array has to be created. It then copies the elements prior to the index to be removed and the elements subsequent to that index. There are several possibilities, here some:

The oldest, using System.arraycopy ((much) in the past could cause JVM crash):

Label[] remover(int indice, Label[] original) {
    // TODO validação do indice
    Label[] resultado = new Label[original.length-1];
    System.arraycopy(original, 0, resultado, 0, indice);
    System.arraycopy(original, indice+1, resultado, indice, resultado.length-indice);
    return resultado;
}

in contrast using Stream:

Label[] remover(int indice, Label[] original) {
    // TODO validação do indice
    return IntStream.concat(IntStream.range(0, indice), 
                            IntStream.range(indice+1, original.length))
                    .mapToObj(i -> original[i])
                    .toArray(Label[]::new);
}

or

Label[] remover(int indice, Label[] original) {
    // TODO validação do indice
    return Stream.concat(
        Arrays.stream(original, 0, indice),
        Arrays.stream(original, indice+1, original.length)
        ).toArray(Label[]::new);
}

Without Stream using a ArrayList, in my opinion, the easiest way to be understood:

Label[] remover(int indice, Label[] original) {
    // TODO validação do indice
    var lista = new ArrayList<>(List.of(original));  // ou ArrayList<>(Arrays.asList(original));
    lista.remove(indice);
    return lista.toArray(new Label[0]);
}

Browser other questions tagged

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