如何从字符串数组中删除索引?

时间:2015-06-15 14:04:22

标签: java android arrays string

我有以下代码验证一个项目是否存在于字符串数组中,如果没有,它将添加一个新项目,否则它将删除,但我找不到删除索引的方法它存在。

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);
        }
    }
}

型号:

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 个答案:

答案 0 :(得分:1)

Apache Commons ArrayUtils有一个方法:

  

从指定的数组中删除第一次出现的指定元素。所有后续元素都向左移动(从索引中减去一个)。如果数组不包含这样的元素,则不会从数组中删除任何元素。

iaArray = ArrayUtils.removeElement(iaArray, element)

答案 1 :(得分:1)

Easiest and nicest would be to make getIds_alunos() a Set<String>.

Otherwise forget also about using a string array. As split uses a regex, just keep using regular expressions 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 field with student ids can be immediately usable:

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() gives access to the actual field's set (not a copy). Hence you can add/remove on the field itself.

答案 2 :(得分:0)

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

  1. System.arraycopy(a, del + 1, a, del, a.length - 1 - del), and set the last item to null.

  2. Instead of using array, try to use ArrayList