如果java是值传递的,为什么我的对象在执行方法后会发生变化?

时间:2015-05-21 14:11:16

标签: java

我正在调试我的方法,它以某种方式更新了我的element变量。即使我不在方法中使用该变量。

CODE:

private static List<Mapping> createFormFieldsMapping(ArrayList<String> CDOfields,
            List<Mapping> fieldMappings, Element element) {
        System.out.println(" - Creating field mappings for "+element.name);
        for (Mapping fieldMapping : fieldMappings){
            if (fieldMapping.targetEntityFieldId!=null){
                String formField = getContactFieldNameById(fieldMapping.targetEntityFieldId);
                formField = formField.trim();
                formField = formField.replaceAll("-", "");
                formField = formField.replaceAll("_", "");
                formField = formField.replaceAll(" ", "");
                formField = formField.toLowerCase();
                Boolean matchFound = false;
                for (String cdoField : CDOfields){
                    String[] cdoFieldSplit = cdoField.split(";",-1);
                    String cdoFieldModified =cdoFieldSplit[1].trim();
                    cdoFieldModified = cdoFieldModified.replaceAll("-", "");
                    cdoFieldModified = cdoFieldModified.replaceAll("_", "");
                    cdoFieldModified = cdoFieldModified.replaceAll(" ", "");
                    cdoFieldModified = cdoFieldModified.toLowerCase();
                    if (cdoFieldModified.equals(formField)){
                        fieldMapping.targetEntityFieldId = cdoFieldSplit[0];
                        matchFound = true;
                        break;
                    }
                    if (!matchFound){
                        // WRITE NOT MATCHED FORM FIELD TO A FILE
                    }
                }
            }
        }

element.processingSteps.targetEntityFieldId正在改变

这就是我调用方法的方式:

List<Mapping> fieldMapping = new ArrayList<Mapping>();
Iterator<ProcessingStep> i = element.processingSteps.iterator();

while (i.hasNext()) {
       ProcessingStep step = i.next(); 
       if (step.type.equals("FormStepCreateUpdateContactFromFormField")){
           fieldMapping = step.mappings;
           step.execute = "never";
           //i.remove();
        }
     }
// Update contact field IDs with CDO field IDs
    fieldMapping = createFormFieldsMapping(CDOfields, fieldMapping, element);

我想要的是复制字段映射,通过该方法处理它,然后返回并将其添加到fieldMappings列表中。

问题是step.mappingselement的一部分,但step.mappings正在ArrayList fieldMapping。通过这种方式,我认为该元素永远不应被任何东西编辑。

1 个答案:

答案 0 :(得分:1)

我相信这是因为在此代码中更改fieldMapping也会更改元素,因为它们都是&#34;引用&#34;对同一个对象:

fieldMapping.targetEntityFieldId = cdoFieldSplit[0];

要验证是这种情况,请添加一个条件调试中断(或if语句和打印/日志),以检查两个对象实例的相等性。

这种情况正在发生,因为您在下面的代码中将fieldMapping设置为等于element.processingSteps.iterator()指向的对象。

Iterator<ProcessingStep> i = element.processingSteps.iterator();

while (i.hasNext()) {
       ProcessingStep step = i.next(); 
       if (step.type.equals("FormStepCreateUpdateContactFromFormField")){
           fieldMapping = step.mappings;
           step.execute = "never";
           //i.remove();
        }
     }
// Update contact field IDs with CDO field IDs
    fieldMapping = createFormFieldsMapping(CDOfields, fieldMapping, element);

如果您不想要这种行为,那么您需要&#34;深层复制&#34;初始化fieldMapping时的step.mappings对象。