2017-03-27 5 views
-1

インタフェース(配列、地図など)がなくても簡単な質問や不可能なこともありますが、オブジェクト名をStringに変換する可能性があるかどうかを知りたい場合は引数として渡す。私は関数に引数として渡す必要がある複数のオブジェクトを持つ2つのクラスのPacienteとSintomasを持っていますが、配列を使用する必要はありません(そのようにする必要があります)。それぞれを挿入する。このような何かが動作するはず文字列をオブジェクト名に変換するJava

Paciente Paciente1 = new Paciente("001", "Ana Melo", 33, ""); 
Paciente Paciente2 = new Paciente("002", "Rui Costa", 13, ""); 
Paciente Paciente3 = new Paciente("003", "Joana Martins", 85, ""); 
Paciente Paciente4 = new Paciente("004", "Pedro Torres", 53, ""); 
Paciente Paciente5 = new Paciente("005", "Ana Gomes", 93, ""); 
Paciente Paciente6 = new Paciente("006", "Jorge Costa", 56, ""); 

Sintomas Sintoma1 = new Sintomas("001", "febre"); 
Sintomas Sintoma2 = new Sintomas("001", "dores"); 
Sintomas Sintoma3 = new Sintomas("001", "machas"); 
Sintomas Sintoma4 = new Sintomas("002", "febre"); 
Sintomas Sintoma5 = new Sintomas("002", "manchas"); 
Sintomas Sintoma6 = new Sintomas("003", "febre"); 
Sintomas Sintoma7 = new Sintomas("003", "dores"); 
Sintomas Sintoma8 = new Sintomas("004", "febre"); 
Sintomas Sintoma9 = new Sintomas("006", "manchas"); 
Sintomas Sintoma10 = new Sintomas("006", "dores"); 

// now I would like to pass to a function as argument something like this: 

for(int i = 0 ; i < 6 ; i++) 
    kSession.insert("Paciente"+(i+1)); 

// instead of making 

kSession.insert(Paciente1); 
kSession.insert(Paciente2); 
kSession.insert(Paciente3); 

// and so on. 
+0

'Paciente'と' Sintomas'は "pa好奇心を抱く人々のために、スペイン語で「肌」と「症状」を示します。 –

+3

基本的に評価をしたい...配列を使用する... – Li357

+0

オブジェクトには名前がありません。参照を使用します。あなたは '地図'を探していますか? – EJP

答えて

1

(あなたが原因サイズの制約のない配列を意味していないasuming)、どこかにあなたがデータを追加がなければならないことに注意してください、それはTXTか何かからそれをロードすることも可能だが、それ同じofcource

List<Paciente> pacientes = new ArrayList<>(); // no size constraints, automatically expands if too small 

pacientes.add(new Paciente("", "", "")); 

for (Paciente paciente : pacientes) { // loop all Patientes in pacientes 
    kSession.insert(paciente); // add a paciente to the session, for every entry 
} 

は、任意のクラスのために行われ、または

それはすべてが本当にに降りてくるオブジェクトをすることができますいくつかの点で定義する必要があり、どのようにあなたは、データを保存し、アクセスしたい行うとどこくださいそれを保存してアクセスする必要があります。 ArrayListのと地図の申し出に簡単にデータのリストのサイズと内容を変更するユーティリティを使用して、しかし、患者はその後、地図

を使用してIDを持っている場合は任意のデータとしては、最初に注意点として

を挿入する必要があります

Map<String, Paciente> pacientes = new HashMap<>(); 

は、patiensに非常に高速でアクセスする方法を提供し、TreeMap構造体はキーでソートされます。

その他のオプション データを管理するラッパークラスは、ArrayList <と同様に動作しますが、リストから追加、削除などのルールを定義できます。

public class Wrapper{ 
    private List<Paciente> pacientes = new ArrayList<>(); 

    public void addPaciente(Paciente paciente){ 
     if(!this.pacientes.contains(paciente)) // prevent multi entries 
      paciente.add(paciente); 
    } 

    public void addPacientes(List<Paciente> pacientes){ 
     for(Paciente paciente : pacientes) // add all patients using the add method 
      this.addPaciente(paciente); 
    } 

    public List<Paciente> getPacientes(){ 
     return this.pacientes; 
    } 
} 

以前

を説明最後に、Pacienteは、Sintomasのリストを持つことができない理由がないとして、あなたは、その後、kSessionに患者を追加することができ、そのよう

public class Paciente{ 
    private List<Sintomas> sintomas = new ArrayList<>(); 

    public addSintomas(Sintomas sintomas){ 
     if(!this.sintomas.contains(sintomas)) 
      this.sintomas.add(sintomas); 
    } 
    // rest is the same principle as above in "Wrapper" 
} 

いるこのあなたはPacienteを取得し、Sintomasを追加することができます。そして、あなたがPacientes Sintomasをチェックしたいときは、そのPacienteからSintomasのリストを取得するだけです。

+0

@MiguelRuivoこれは何のように動作しますか? – Mikenno

+0

返信いただきありがとうございますが、配列や地図を使用したくありません。私はむしろ文字列として名前を渡すことによってオブジェクトを取得するJavaをしたい。おそらく私は知らないだろう。 – Miguel

+0

@MiguelRuivo @MiguelRuivoあなたがここで説明しているのは、地図が何であるかのexcatlyです。それは、例えば文字列のようなキーに基づいて値を格納し、それを元の文字列Map オブジェクトTYPEは、.getClass()。toStrin()として保存してから、同じ.getClass()。toString()を探していると思ってループを取得します。これは実装ではなく、 。しかし、あなたはそれをデータ構造に保存したり、それを名前で行うのでなければ、何かを探すことができないことを認識しなければなりません – Mikenno

関連する問題