有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java:ArrayList。清除从传递到映射的ArrayList中删除元素

这是我多年来第一次编写Java代码,我正在编写一个Ghidra脚本,该脚本将系统调用符号映射到它们的调用函数

private HashMap<Symbol, Reference[]> symbolRefs = new HashMap<Symbol, Reference[]>();
private HashMap<Symbol, List<Function>> callerFuncs = new HashMap<Symbol, List<Function>>();

.
.
.

private void mapSysCallToCallerFunctions(FunctionManager funcMan) throws Exception {
    List<Function> funcs = new ArrayList<Function>();
    for(HashMap.Entry<Symbol, Reference[]> entry: this.symbolRefs.entrySet()) {
        for(Reference ref : entry.getValue()) {
            Function caller = funcMan.getFunctionContaining(ref.getFromAddress());
            if(caller != null) {
                funcs.add(caller);
            }
        }
        this.callerFuncs.put(entry.getKey(), funcs);
        funcs.clear();
    }
}

我的问题是我想清除“funcs”列表,以便在下一次迭代中再次使用空列表。由于未知原因,这会导致HashMap中的函数列表也为空。如果我在此处打印HashMap:

private void printCallerSymbolMap() throws Exception {
    for(HashMap.Entry<Symbol, List<Function>> entry: this.callerFuncs.entrySet()) {
        printf("Symbol %s:\n", entry.getKey().toString());
        for(Function func : entry.getValue()) {
            printf("Called by function %s\n", func.getName());
        }
    }
}

我只得到输出:

INFO  Symbol system: (GhidraScript)  
INFO  Symbol system: (GhidraScript) 

但是,当我删除funcs时。clear(),我得到:

INFO  Symbol system: (GhidraScript)  
INFO  Called by function system (GhidraScript)  
INFO  Called by function system (GhidraScript)  
INFO  Called by function main (GhidraScript)  
INFO  Symbol system: (GhidraScript)  
INFO  Called by function system (GhidraScript)  
INFO  Called by function system (GhidraScript)  
INFO  Called by function main (GhidraScript)  

但应该是这样的:

INFO  Symbol system: (GhidraScript)  
INFO  Called by function system (GhidraScript)  
INFO  Called by function system (GhidraScript)  
INFO  Symbol system: (GhidraScript) 
INFO  Called by function main (GhidraScript)  

我有两个系统符号,因为它是重击


共 (1) 个答案

  1. # 1 楼答案

    不是清除列表,而是每次初始化列表

    private void mapSysCallToCallerFunctions(FunctionManager funcMan) throws Exception {
        List<Function> funcs;
        for(HashMap.Entry<Symbol, Reference[]> entry: this.symbolRefs.entrySet()) {
            funcs = new ArrayList<Function>();
            for(Reference ref : entry.getValue()) {
                Function caller = funcMan.getFunctionContaining(ref.getFromAddress());
                if(caller != null) {
                    funcs.add(caller);
                }
            }
            this.callerFuncs.put(entry.getKey(), funcs);
        }
    }