gpt4 book ai didi

java - HashMap 自己改变值

转载 作者:行者123 更新时间:2023-12-02 04:20:41 26 4
gpt4 key购买 nike

我正在使用 jsf,并且在 bean 中遇到一些问题。当我按下 Fine 按钮时,我希望调用 dataTreeBean.addAttributo。问题是它只有在第一次调用时才能正常工作。假设我作为参数传递 (key1, v1) ,它们被正确插入到 attributes 中。当我再次按下按钮 Fine 时,参数为 (key2, v2) ,我发现 (key1, v2) 而不是 (key1, v1) 在方法开始之前的 attributes 中,在方法运行之后我有 (key1, v2)(key2, v2) 。我无法理解发生了什么,也不知道在方法运行之前 attributes 中的值是如何更改的。

treeBean.java

private Attribute a;
private ArrayList<TreeNode> listaClassi;
private TreeNode newNode;
private TreeNode selectedNode;

public void createNodeT() {

TreeNode childNode = new DefaultTreeNode(newNode, null);
recursiveTree(newNode, childNode);
recursiveTree2(newNode, selectedNode);
crAtt(newNode);
newNode = null;

}
public void recursiveTree(TreeNode node, TreeNode c) {

List<TreeNode> children = node.getChildren();
Attribute a = new Attribute();

// if (node.getChildCount() > 0) {

a.setName(node.toString());
a.setDistinguishing(distinguishing);
a.setMandatory(mandatory);
a.setDisplay(display);
a.setDataType("Taxonomy");
a.setSubClasses(node);
aList.add(a);


for (int j = 0; j < node.getChildCount(); j++) {
TreeNode childNode = new DefaultTreeNode(children.get(j), c);
listaClassi.add(children.get(j));
recursiveTree(children.get(j), childNode);
}
}

public void crAtt(TreeNode node) {

a.setName(node.toString());
a.setDistinguishing(distinguishing);
a.setMandatory(mandatory);
a.setDisplay(display);
a.setDataType("Taxonomy");
a.setSubClasses(node);

}

public ArrayList<TreeNode> getListaClassi() {
return listaClassi;
}


public Attribute getA() {
return a; }

dataTreeBean.java:

private HashMap<String, ArrayList<Attribute>> attributes;
private String classe;
private String option;

public void addAttributo(String key, Attribute att) {

if (attributes.get(key) == null) {
ArrayList<Attribute> a = new ArrayList<Attribute>();
a.add(att);
attributes.put(key, a);
} else {
ArrayList<Attribute> a = attributes.get(key);
a.add(att);
attributes.put(key, a);
}

int i = 0;

}
public String getClasse() {
return classe;
}
public void setOption(String option) {
this.option = option;
}

文件.xhtml

    <p:commandButton value="Fine" styleClass="ui-confirmdialog-yes"
action="#{dataTreeBean.addClassi(treeBean.getListaClassi())}"
update=":formData:selected_data_property :br:sel_range aggiungiAttTaxonomy :formClassi:selected_class"
oncomplete="PF('addTaxonomy').hide();"
actionListener="#{dataTreeBean.setOption('Taxonomy')}">
<f:actionListener binding="#{treeBean.createNodeT()}" />
<f:actionListener
binding="#{dataTreeBean.addAttributo(dataTreeBean.getClasse(),treeBean.getA())}" />
</p:commandButton>

最佳答案

我解决了用以下方法替换 treeBean 内的 crAttr 的问题

public void generateAtt(TreeNode node) {

for (Attribute a : aList) {
if (a.getName().equals(newNode.toString()))
this.a = a;
}

}

然后修改createcreateNodeTRel

public void createNodeTRel() {

TreeNode childNode = new DefaultTreeNode(newNode, null);
recursiveTree(newNode, childNode);
recursiveTree(newNode, selectedNode);
generateAtt(newNode);

newNode = null;

}

关于java - HashMap 自己改变值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56637252/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com