【问题标题】:How to remove an index from a String Array? [closed]如何从字符串数组中删除索引? [关闭]
【发布时间】:2015-06-15 14:04:22
【问题描述】:

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

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

【问题讨论】:

  • res 是字符串而不是字符串数组

标签: java android arrays string


【解决方案1】:

最简单和最好的方法是将getIds_alunos() 设为Set&lt;String&gt;

否则忘记使用字符串数组。由于 split 使用的是正则表达式,所以请立即继续使用正则表达式:

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

作为Set&lt;String&gt;:

带有学生ID的字段可以立即使用:

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

甚至:

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

这应该可行。 getter getIds_alunos() 可以访问实际字段的集合(不是副本)。因此,您可以在字段本身上添加/删除。

【讨论】:

  • 设置 ia = mGpsEscolas.getIds_alunos();给出类型不兼容的错误
  • 是的,GPSEscolas 中的字段应该改为private Set&lt;String&gt; alunos = new TreeSet&lt;&gt;();
  • 但那是模型,我需要使用 GPSEscolas
  • 那么只能使用String代码;第一部分?
  • 我不明白,第二种方法对我来说是完美的,但不能让它工作
【解决方案2】:

Apache Commons ArrayUtils 有一个方法:

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

iaArray = ArrayUtils.removeElement(iaArray, element)

【讨论】:

    【解决方案3】:

    您无法更改数组的长度,但有几种解决方法:

    1. System.arraycopy(a, del + 1, a, del, a.length - 1 - del),并将最后一项设置为null

    2. 不要使用array,而是尝试使用ArrayList

    【讨论】:

      猜你喜欢
      • 2019-12-04
      • 1970-01-01
      • 2021-03-17
      • 2012-10-02
      • 1970-01-01
      • 2018-02-05
      • 1970-01-01
      • 2020-03-01
      相关资源
      最近更新 更多