将字符串转换为对象名Java

将字符串转换为对象名Java,java,string,object,type-conversion,Java,String,Object,Type Conversion,如果没有任何类型的接口(数组、映射等),这可能是一个简单的问题,甚至是不可能的,但我想知道是否有可能将对象名转换为字符串,以便我可以作为参数传递。我有两个类Paciente和Sintomas,它们有多个对象,我需要将它们作为参数传递给函数,但我不想使用数组(必须是这样),如果不手动为每个对象插入,我无法找到其他方法 Paciente Paciente1 = new Paciente("001", "Ana Melo", 33, ""); Paciente Paciente2 = new Paci

如果没有任何类型的接口(数组、映射等),这可能是一个简单的问题,甚至是不可能的,但我想知道是否有可能将对象名转换为字符串,以便我可以作为参数传递。我有两个类Paciente和Sintomas,它们有多个对象,我需要将它们作为参数传递给函数,但我不想使用数组(必须是这样),如果不手动为每个对象插入,我无法找到其他方法

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. 
Paciente Pacient1=新的Paciente(“001”,“Ana Melo”,33“”);
Paciente Paciente 2=新Paciente(“002”,“鲁伊科斯塔”,13“”);
Paciente Paciente 3=新Paciente(“003”,“Joana Martins”,85“”);
Paciente Paciente 4=新Paciente(“004”,“Pedro Torres”,53“”);
Paciente Paciente 5=新Paciente(“005”,“Ana Gomes”,93“”);
Paciente Paciente 6=新Paciente(“006”,“豪尔赫·科斯塔”,56“”);
Sintomas Sintoma1=新的Sintomas(“001”、“febre”);
Sintomas Sintoma2=新的Sintomas(“001”、“dores”);
Sintomas Sintoma3=新的Sintomas(“001”、“machas”);
Sintomas Sintoma4=新的Sintomas(“002”、“febre”);
Sintomas Sintoma5=新的Sintomas(“002”、“manchas”);
Sintomas Sintoma6=新Sintomas(“003”、“febre”);
Sintomas Sintoma7=新Sintomas(“003”、“dores”);
Sintomas Sintoma8=新Sintomas(“004”、“febre”);
Sintomas Sintoma9=新的Sintomas(“006”,“manchas”);
Sintomas Sintoma10=新的Sintomas(“006”、“dores”);
//现在我想把一个函数作为参数传递如下:
对于(int i=0;i<6;i++)
K段插入(“Paciente”+(i+1));
//而不是制造
K段插入(客户1);
K段插入(客户2);
K段插入(第3段);
//等等。

类似的方法应该可以使用(由于大小限制,您的意思是没有数组),请注意,必须在某个位置添加数据,也可以从txt或其他方式加载数据,但必须在某个位置定义数据

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
}
List pacientes=new ArrayList();//没有大小限制,如果太小会自动展开
添加(新的Paciente(“,”,“);
对于(Paciente-Paciente:pacientes){//循环pacientes中的所有患者
kSession.insert(paciente);//为每个条目向会话添加一个paciente
}
当然,对于任何类或对象也可以这样做

这一切实际上归结为,您希望如何存储和访问数据,以及您需要在哪里存储和访问数据。使用ArrayList和Map可以方便地更改数据列表的大小和内容,但对于任何数据,都必须首先插入

作为旁注,如果患者有身份证,则使用地图

Map<String, Paciente> pacientes = new HashMap<>();
Map pacientes=newhashmap();
提供了一种快速访问患者的方法,如果需要,树状图结构按键排序

其他选择可能是: 管理数据的包装类的工作方式与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;
    }
}
公共类包装器{
private List pacientes=new ArrayList();
公共无效添加Paciente(Paciente Paciente){
如果(!this.pacientes.contains(paciente))//防止多个条目
添加(paciente);
}
公共无效添加客户端(列出客户端){
for(Paciente-Paciente:pacientes)//使用add方法添加所有患者
这是addPaciente(paciente);
}
公共列表getPacientes(){
把这个还给帕森特斯;
}
}
然后,您可以将患者添加到kSession,如前所述

最后,Paciente没有理由拥有Sintoma的列表,比如

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"
}
public-class-Paciente{
私有列表sintomas=newarraylist();
公共地址Sintomas(Sintomas Sintomas){
如果(!this.sintomas.contains(sintomas))
this.sintomas.add(sintomas);
}
//rest与“包装器”中的上述原理相同
}

通过这种方式,您可以获得一个Paciente,并添加一个Sintomas,然后当您希望检查Pacientes Sintomas时,您可以从该Paciente中获取Sintomas的列表,对于好奇的人来说,
Paciente
Sintomas
在西班牙语中是“患者”和“症状”的意思。所以您基本上想要评估。。。使用数组…对象没有名称。使用引用。您是否正在寻找
映射
?您必须定义所有的值,但是问题有点模糊,您想要一个容器吗?这不可能是一张单子吗?好。。。。你可以使用地图,但它并不比这个列表更好context@cricket_007谢谢你的评论,我想我错过了那一行。已删除我的答案;)然而,这是一种非常奇怪的解决问题的方法,不使用数组来回答问题,但我不想使用数组或映射。我更希望java通过将名称作为字符串传递来获取对象。可能是不可能的,我不知道。@MiguelRuivo您在这里描述的当然是映射,它是一种基于键(例如字符串)存储值的方法,然后使用所述字符串映射获取值,作为参考,如果您指的是对象类型,则将其另存为.getClass().ToString()然后通过循环得到它,虽然它在寻找相同的.getClass().toString()注意,这是实现,而不是接口,它将在那时寻找。但您必须意识到,除非您将其保存在数据结构中或按其名称查找,否则无法查找