- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在处理 3 中工作,并且在理解 HashMap 的返回时遇到困难。我有一张 map ,Map<String, Chromosome> genes = new HashMap<String, Chromosome>()
它使用我的类(class),
class Chromosome{
Genotype geneOne;
Genotype geneTwo;
Chromosome(){ ... }
Chromosome(Genotype gOne, Genotype gTwo){ ... }
void setGeneOne(Genotype gene){ ... }
void setGeneTwo(Genotype gene){ ... }
Genotype getDomGene(){ ... }
Genotype getRecGene(){ ... }
}
class Genotype{
Object value;
float weight;
public Genotype(int value, float weight){ ... }
public Genotype(int[] value, float weight){ ... }
public Genotype(String value, float weight){ ... }
public Genotype(float value, float weight){ ... }
public Object getValue(){ ... }
public float getWeight(){ ... }
public void setValue(int value){ ... }
public void setValue(int[] value){ ... }
public void setValue(String value){ ... }
public void setValue(float value){ ... }
}
我的想法是,当我从 map “获取”一个值时,我应该能够从那里访问它的方法。 IE。
class Flower{
Map<String, Chromosome> genes;
Flower(){
genes = new HashMap<String, Chromosome>();
genes.put("color", new Chromosome(new Genotype(64, 1.0), new Genotype(25,0.5)));
Genotype test = genes.get("color").getDomGene(); //should return the first param passed to the new chromosome
}
}
我希望避免每次使用返回的对象时都必须声明它。经过 20 分钟的谷歌搜索,我似乎找不到任何有关此工作的信息,那么为什么这不起作用,可以采取哪些措施来解决它?
最佳答案
您应该在 getDomGene
方法中返回 genOne。
染色体类别。
package gen;
class Chromosome {
Genotype geneOne;
Genotype geneTwo;
Chromosome() {
System.out.println("Chromosome.Chromosome");
}
Chromosome(Genotype gOne, Genotype gTwo) {
System.out.println("Chromosome.Chromosome");
}
void setGeneOne(Genotype gene) {
System.out.println("Chromosome.setGeneOne");
}
void setGeneTwo(Genotype gene) {
System.out.println("Chromosome.setGeneTwo");
}
Genotype getDomGene() {
System.out.println("return genOne");
return geneOne;
}
Genotype getRecGene() {
System.out.println("return genTwo");
return geneTwo;
}
}
基因型类别
package gen;
class Genotype {
Object value;
float weight;
public Genotype(int value, float weight) {
System.out.println("Genotype.Genotype");
}
public Genotype(int[] value, float weight) {
System.out.println("Genotype.Genotype");
}
public Genotype(String value, float weight) {
System.out.println("Genotype.Genotype");
}
public Genotype(float value, float weight) {
System.out.println("Genotype.Genotype");
}
public Object getValue() {
System.out.println("Genotype.getValue");
return null;
}
public void setValue(String value) {
System.out.println("Genotype.setValue");
}
public void setValue(float value) {
System.out.println("Genotype.setValue");
}
public void setValue(int value) {
System.out.println("Genotype.setValue");
}
public void setValue(int[] value) {
System.out.println("Genotype.setValue");
}
public float getWeight() {
System.out.println("Genotype.getWeight");
return 0;
}
}
花类。
package gen;
import java.util.HashMap;
import java.util.Map;
class Flower {
Map<String, Chromosome> genes;
Flower() {
genes = new HashMap<>();
genes.put("color", new Chromosome(new Genotype(64, 1.0f), new
Genotype(25, 0.5f)));
Genotype test = genes.get("color")
.getDomGene(); //should return the first param passed to the new chromosome
}
public static void main(String[] args) {
new Flower();
}
}
打印
Genotype.Genotype
Genotype.Genotype
Chromosome.Chromosome
return genOne
return genOne
意味着您可以访问 Chromosome
类的 geneOne
字段,这是它的第一个参数。
关于java - 从 HashMap 获取方法 get() 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47129924/
我有不同的结构,它们都包含一个 HashMap与 String作为键,但具有不同的值类型。例如,一个结构有一个类型为 HashMap 的成员, 另一个将有一个 HashMap 类型的成员, 等等。 我
我想制作一个包含学生姓名和科目的板,每个学生在每个科目中都有一个成绩(或者没有..他可以离开考试而不写,然后他的案子将是空的)。我只想使用 HashMap。我的意思是,它会是这样的: HashMap>
是否有内存和速度高效的方法来在 HashMap 中动态存储唯一键:值对? key 保证是唯一的,但它们的数量经常变化。插入和删除必须很快。 我所做的是包含有符号距离场的八叉树(非线性/完整)。八叉树经
有谁知道为什么选择通过 LinkedList 而不是另一个 Hashmap 来实现 HashMap 的存储桶。如果桶本身变成了 HashMap,那么 contains 或 get 的时间复杂度似乎是
我想创建一个具有嵌套结构的 HashMap,就像这个复杂的示例: { type: boy name: Phineas father: type: man
这个问题在这里已经有了答案: How do I create a global, mutable singleton? (7 个答案) 关闭 7 年前。 我想要一个可扩展的字典,将 Object 与
HashMap> hm = new HashMap>(); hm.put("Title1","Key1"); for(int i=0;i hm1 = new H
我必须修改当前代码以适应 Spring MVC。我有 HashMap hashmap = new HashMap(); request.setAttribute("dslrErrors", hashm
我正在尝试进行一些错误捕获。 错误应该检查数组的长度是否小于 2,并检查 HashMap 是否包含用户输入的键。 捕获的错误必须仅使用 if 语句,并且必须使用 .length() 方法,并且必须使用
在 stackoverflow 上提出另一个问题后,(Java- Why this program not throwing concurrent Modification exception)我开始
我有两个类,想使用 org.dozer.Mapper( http://dozer.sourceforge.net/ ) 将 Female 对象的属性映射到 Male 对象。 第一类是: public
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be
是否有任何方法可以检查 HashMap 是否包含一组特定的键(这些键是在数组中给出的)。当我尝试类似下面的代码时,它返回 false。 map.containsKey(arrayOf("2018-01
跟进我的问题:How To Access hash maps key when the key is an object 我想尝试这样的事情:webSearchHash.put(xfile.getPa
我有一个可扩展的 ListView ,对于每个 child ,我需要有 4 个“额外”或字符串或其他名称来调用它:- 子标题- 描述- 链接1- 链接2 跟着教程,创建 ListView 、不同的 p
我想确保这是正确的,因为如果不正确,它可能会破坏我的应用程序。 我有这个: private static HashMap> balance = new HashMap<>(); 如果我得到这样的值:
我想做以下事情: 为某个键查找Vec,并将其存储以备后用。 如果它不存在,则为键创建一个空的 Vec,但仍将其保存在变量中。 如何有效地做到这一点?自然地,我认为我可以使用 match: use st
我想做以下事情: 为某个键查找Vec,并将其存储以备后用。 如果它不存在,则为键创建一个空的 Vec,但仍将其保存在变量中。 如何有效地做到这一点?自然地,我认为我可以使用 match: use st
我想做以下事情: 为某个键查找Vec,并将其存储以备后用。 如果它不存在,则为键创建一个空的 Vec,但仍将其保存在变量中。 如何有效地做到这一点?自然地,我认为我可以使用 match: use st
我想做以下事情: 为某个键查找Vec,并将其存储以备后用。 如果它不存在,则为键创建一个空的 Vec,但仍将其保存在变量中。 如何有效地做到这一点?自然地,我认为我可以使用 match: use st
我是一名优秀的程序员,十分优秀!