Java 如何在两个方法之间传递HashMap
我有两种方法:一种是创建HashMap,另一种是从第一种方法获取HashMap。有人能帮我怎么做吗 代码:Java 如何在两个方法之间传递HashMap,java,Java,我有两种方法:一种是创建HashMap,另一种是从第一种方法获取HashMap。有人能帮我怎么做吗 代码: public HashMap createHash(布尔包含){ HashMap newMap=新HashMap(); 如果(!包括){ 做点什么 } 返回newMap; } 公共ArrayList getHash(布尔包含){ HashMap newMap=new newMap.createHash();//获取错误或更低 HashMap newMap=newCreateHash();/
public HashMap createHash(布尔包含){
HashMap newMap=新HashMap();
如果(!包括){
做点什么
}
返回newMap;
}
公共ArrayList getHash(布尔包含){
HashMap newMap=new newMap.createHash();//获取错误或更低
HashMap newMap=newCreateHash();//也有错误
//这里我想使用newMap
归还某物;
}
编辑:两个方法都在同一个类中方法是驻留在某个类上的非静态函数,,您没有向我们展示该类。按照当前的定义,调用此方法的正确语法为:
//create an instance
MyFoo foo = new Foo(); // instance of class you haven't shown us
//invoke the createHash method on the foo instance...
Map<Object, Integer> newMap = foo.createHash(true);
您正在使用的语法:
new createHash();
不会编译。new关键字仅适用于构造函数。您需要根据方法签名向createhash方法传递一个布尔值。TS表示这两个方法属于同一类。因此,不需要执行“MyFoo foo=new MyFoo();之类的操作。当然,我想这是一个更新。在这种情况下,只需直接调用createHash方法。如果您想使用use constructor并在同一行中调用一个方法,您可以这样做:………Map myMap=(new MyFoo()).getSomething();
Map<Object, Integer> newMap = createHash(true);
new createHash();