我是Java的初学者.我读到structsC中的类似于Java中的类,但我有以下疑问.
我有一个课程如下:
public class operations {
public Integer[] stream;
public Integer[] functi;
public String[] name;
public Integer[] funcgroup;
}
Run Code Online (Sandbox Code Playgroud)
我从用户那里得到一个输入name,并将其与类中的name数组进行比较,如果匹配,我想返回与该名称对应的所有其他字段的记录.
例如.如果名称对应String[5]的话,我想对应的[5] ..即输出的所有记录stream[5],functi[5],functigroup[5].
我怎样才能做到这一点?
编辑现在我的程序看起来像这样:
public class operations extends DefFunctionHandler {
public ArrayList<Integer> stre = null;
public ArrayList<Integer> functii = null;
public ArrayList<String> nmee = null;
public ArrayList<Integer> funcigroup = null;
public ArrayList<Integer> sourcee = null;
public void filter(String x){
DefFunctionHandler defi = new DefFunctionHandler();
functii = defi.getFunc();
stre = defi.getStream();
nmee = defi.getName();
funcigroup = defi.getFuncgroup();
sourcee = defi.getSource();
Map<String, operations> map = new HashMap<String, operations>();
operations operations = new operations(0, 0, x, 0, 0);
map.put(x, operations);
operations op = map.get("flush");
System.out.println(op.toString());
}
Run Code Online (Sandbox Code Playgroud)
我得到一条消息说我必须为参数(int,int,string,int,int)的操作声明一个构造函数.任何人都可以告诉我,我的Map接口实现是否正确?
小智 5
您应该将操作对象存储到Map中
地图使用键/值,您将ID放入地图,然后您可以检索相应的键.
在您的示例中,使用类Operation:
public class Operation {
public int stream;
public int functi;
public name;
public int funcgroup;
}
Run Code Online (Sandbox Code Playgroud)
和这样的地图:
Map<String, Operation> map = new HashMap<String, Operation>();
Operation operation = new Operation(0,0,"name5", 0);
map.put("name5", operation);
Run Code Online (Sandbox Code Playgroud)
您可以使用以下命令检索Operation对象:
Operation op = map.get("name5");
Run Code Online (Sandbox Code Playgroud)