简体   繁体   中英

private class ArrayList (set)

I have a class named "classroom" and i want send one arraylist with classroom.setMaterias(Arraylist) . Is this code:

Class Clasroom

public class Aula implements java.io.Serializable {
private String nombre;
private String grupo;
private int tutor;
ArrayList<String> materias = new ArrayList<String>();  // ¿How create arraylist?
public Aula() {
  // Constructor
}

public String getNombre() {
 return nombre;
}

public void setNombre(String nombre) {
 this.nombre = nombre;
}

I would like to know if I could, for example, send an "arraylist" through a SET and then make the "arraylist" that I created previously in my class "classroom" be exactly the same

I would not know how to create the arraylist, or the set or get methods. Can you help me please?

PD: This is the JSON ARRAY i talking about:

  if (obj.has("materias")) {
     JSONArray materias = obj.getJSONArray("materias");
     datos.setArrayList(materias);
    // System.out.println(materias); // ["DWES","DWEC","IW","DAW","IE"]
    Class Clasroom

       public class Aula implements java.io.Serializable {
       private String nombre;
       private String grupo;
       private int tutor;
       ArrayList<String> materias;  // ¿How create arraylist?
       public Aula() {
         // Constructor
        this.setArrayList(new ArrayList<>()); //Here you initialize the arraylist when you create an instance of this class.
       }

    public String getNombre() {
     return nombre;
    }

    public void setNombre(String nombre) {
     this.nombre = nombre;
    }


  //Here are the getters and setters.
   public ArrayList<String> getList(){
     return this.materias;
   }

    private void setArrayList(ArrayList<String> list){
      this.materias = list;
    }

The proper way of doing it is using getters and setters and using List interface rather than ArrayList<> directly.

List<String> materias = Collections.emptyList(); 

public Aula() {
  // Constructor
}

public List<String> getMaterias() {
    return materias;
  }

  public void setMaterias(List<String> materias ) {
    this.materias = materias ;
  }

  public void addMaterias(String materia) {
    materias.add(materia);
  }

You can have additional addMaterias() method to add entries to your List .

public class Aula implements java.io.Serializable {
  private List<String> materia = new ArrayList<>();
  ...
  public void setMateria1(final List<String> aMateria) {
    this.materia = aMateria;
  }
  public void setMateria2(final List<String> aMateria) {
    this.materia.clean();
    this.materia.addAll(aMateria);
  }
}

setMateria1() replaces the list with the given argument, thus any changes (EG deletion of items,) made later to the one, is reflected in the other.

While setMateria2() copies the argument's items, thus deletion or insertion to any of them does not change the other one.

Also ArrayList is a concrete implementation of the interface List . It is preferable to declare variables as the base class or interface, instead of a concrete implementation.

public class Aula implements java.io.Serializable {
    ArrayList<String> materias = new ArrayList<String>(); 
    ...
    public ArrayList<String> getMaterias(){
       return materias;
    }

   public void setMaterias(JSONList materias) throws JSONException {
       materias.clear();
       for(int i=0;i<materias.length();i++)
          this.materias.add(materias.getString(i));
    }
}

And put the exact same code into the classroom class.

Second way is to set the Lists in the constructor:

public class Aula implements java.io.Serializable {
    ArrayList<String> materias = new ArrayLis<>();
    ...
    public Aula(JSONList materias) throws JSONException {
        for(int i=0;i<materias.length();i++)
          this.materias.add(materias.getString(i)); 
    }
    public ArrayList<String> getMaterias(){
       return materias;
    }
}

Again same for classroom. And than you create them eg.

Aula aula = new Aula(materias);
Classroom classroom = new Classroom(materias);

This is assuming you have Strings in your list. Otherwise it depends on your data in the list. If it contains other Lists it they need to be merged or skipped and so on...

If the json is not all Strings(eg has Sublists and Objects) and it should match the actual structure of your json I'd need that structure too and most probably an Arraylist of Strings might be the wrong Container for such a json - tree.

btw. better change classroom to Classroom(capital C for the classname) ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM