How can I remove an index from a String array?

I have the following code that checks if an element exists in a string array, and if not, it will add a new element, otherwise it will be removed, but I cannot find a way to remove the index if it exists.

TextView StudentId = (TextView) v.findViewById(R.id.idChild);
TextView SchoolId = (TextView) v.findViewById(R.id.schoolId);
IdSchool = String.valueOf(SchoolId.getText());
IdStudents = String.valueOf(StudentId.getText());


aMap = new HashMap<String, GPSSchools>();
if (!aMap.containsKey(idSchool)) {
    mGpsSchools = new GPSSchools();
    aMap.put(idSchool, mGpsSchools);
    aMap.get(idSchool).setStudens_ids(idChild);
} else {
    String ia = aMap.get(idSchool).getStudens_ids();
    String[] iaArray = ia.split(";");
    String res = Arrays.toString(iaArray);

    if (!res.contains(IdStudents)) {
        aMap.get(IdSchool).set(ia + ";" + IdAluno);
    } else {
        ary = new HashMap<String, String[]>();
        String ia = aMap.get(IdEscola).getStudens_ids();
        String[] iaArray = ia.split(";");

        ary.put(IdStudents, iaArray);

        if (!ary.containsValue(IdStudents)) {
            aMap.get(IdSchool).setStudens_ids(ia + ";" + IdStudents);
        } else {
            ary.remove(IdStudents);
        }
    }
}

      

Model:

public class GPSEscolas  implements Serializable{

    private static final long serialVersionUID = 1L;


    private Integer id_escola;
    private Set<String> ids_alunos;
    private double distancia;
    private Float latitude;
    private Float longitude;


    public Integer getId_escola() {
        return id_escola;
    }

    public void setId_escola(Integer id_escola) {
        this.id_escola = id_escola;
    }

    public Set<String> getStudens_ids() {
    return Studens_ids;
    }

    public void setStudens_ids(Set<String> studens_ids) {
    Studens_ids = studens_ids;
    }

    public double getDistancia() {
        return distancia;
    }

    public void setDistancia(double distancia) {
        this.distancia = distancia;
    }

    public Float getLatitude() {
        return latitude;
    }

    public void setLatitude(Float latitude) {
        this.latitude = latitude;
    }

    public Float getLongitude() {
        return longitude;
    }

    public void setLongitude(Float longitude) {
        this.longitude = longitude;
    }
}

      

+3


source to share


3 answers


The easiest and most enjoyable way is to do getIds_alunos()

a Set<String>

.

Otherwise, forget about using a string array as well. Since split uses regex, just keep using regex immediately:

mGpsEscolas = aMap.get(IdEscola).getIds_alunos();
if (mGpsEscolas == null) {
    mGpsEscolas = new GPSEscolas();
    aMap.put(IdEscola, mGpsEscolas);
    mGpsEscolas.setIds_alunos(IdAluno);
} else {
    String ia = mGpsEscolas.getIds_alunos();
    if (!ia.matches(ia, "(.*;)?" + IdAluno + "(;.*)?")) {
        ia += ";" + IdAluno;
    } else {
        //Remove IdEscola if exists.
        ia = ia.replaceFirst(";" + idAluno + "((;.*)?)$", "$1");
    }
    mGpsEscolas.setIds_alunos(ia);
}

      

As Set<String>

:

The student ID field can be used immediately:



private Set<String> ids_alunos = new TreeSet<>();

mGpsEscolas = aMap.get(IdEscola);
if (mGpsEscolas == null) {
    mGpsEscolas = new GPSEscolas();
    aMap.put(IdEscola, mGpsEscolas);
    mGpsEscolas.getIds_alunos().add(IdAluno);
} else {
    Set<String> ia = mGpsEscolas.getIds_alunos();
    if (!ia.contains(IdAluno)) {
        ia.add(IdAluno);
    } else {
        //Remove IdEscola if exists.
        ia.remove(idAluno);
    }
}

      

Or even:

    if (!ia.remove(IdAluno)) {
        ia.add(IdAluno);
    }

      

This should work. The getter getIds_alunos()

provides access to the actual set of fields (not a copy). Hence, you can add / remove in the field itself.

+1


source


Apache Commons ArrayUtils has a method for this:

Removes the first occurrence of the specified element from the specified array. All subsequent elements are shifted to the left (subtracts one of their indices). If the array does not contain such an element, no elements are removed from the array.



iaArray = ArrayUtils.removeElement(iaArray, element)

      

+1


source


You cannot change the length of the array, but there are several ways:

  • System.arraycopy(a, del + 1, a, del, a.length - 1 - del)

    and install the last item null

    .

  • Instead of using, array

    try usingArrayList

0


source







All Articles