我有一个带有一些数据的小类,称为MyData

public class MyData {
    public String name = "";
    public String nameonly = "";
    public int id = 0;
    public double earn = 0;
    public double paid = 0;
    ....

    public MyData(String name, String nameonly, int id) {
        this.name = name;
        this.nameonly = nameonly;
        this.id = id;
    }
}


然后,我有一个带有此类数组的类,用于特定类型的人,称为AllMyData

public class AllMyData {
    public ArrayList<MyData> cli = new ArrayList<>();
    public ArrayList<MyData> sub = new ArrayList<>();
    public ArrayList<MyData> emp = new ArrayList<>();
    public ArrayList<MyData> exp = new ArrayList<>();
    public ArrayList<MyData> oex = new ArrayList<>();
    public ArrayList<MyData> bin = new ArrayList<>();
    public ArrayList<MyData> ven = new ArrayList<>();
    ....
}


在主类中,我需要将新项目添加到特定数组(如果id不存在),其中我有一个表示AllMyData数组的字符串

public AllMyData elems = new AllMyData();

public void initArray(int id, String name, String tip) {
    //this is an example just for "cli" element and "cli" is in String tip
    if (!checkForId(elems.cli, id)) {
        MyData element = new MyData(name, name, id);
        elems.cli.add(element);
    }
}

private boolean checkForId(ArrayList<MyData> a, int id) {
    for (MyData e : a) {
        if (e.id == id) return true;
    }
    return false;
}


然后,我只需要一个电话,例如:

initArray(5, "Test", "emp");


并希望避免使用switch语句并为每种类型重复代码。在此调用中,“ emp”将为元素elems.emp

有没有办法使用字符串名称访问elems成员而不是创建switch语句?

最佳答案

而是在AllMyData中创建列表的映射。

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        AllMyData data = new AllMyData();
        data.add("foo", new MyData("Hello World", "", 1));
        data.add("cli", Arrays.asList(new MyData("BASH", "", 2), new MyData("SHELL", "", 3)));
        System.out.println(data);
    }
}


AllMyData [map={cli=[MyData [name=BASH, nameonly=, id=2, earn=0.0, paid=0.0], MyData [name=SHELL, nameonly=, id=3, earn=0.0, paid=0.0]], sub=[], oex=[], bin=[], foo=[MyData [name=Hello World, nameonly=, id=1, earn=0.0, paid=0.0]], emp=[], exp=[], ven=[]}]



import java.util.*;

public class AllMyData {
    private Map<String, List<MyData>> map;

    public AllMyData() {
        map = new HashMap<String, List<MyData>>();

        map.put("cli", new ArrayList<>());
        map.put("sub", new ArrayList<>());
        map.put("emp", new ArrayList<>());
        map.put("exp", new ArrayList<>());
        map.put("oex", new ArrayList<>());
        map.put("bin", new ArrayList<>());
        map.put("ven", new ArrayList<>());
    }

    public void add(String key, List<MyData> data) {
        List<MyData> list = get(key);
        if (list == null) {
            map.put(key, data);
        } else {
            list.addAll(data);
            map.put(key, list);
        }
    }

    public void add(String key, MyData data) {
        List<MyData> list = get(key);
        if (list == null) {
            list = new ArrayList<>();
        }
        list.add(data);
        map.put(key, list);
    }

    public List<MyData> get(String key) {
        return map.get(key);
    }

    @Override
    public String toString() {
        return String.format("AllMyData [map=%s]", map);
    }
}


public class MyData {
    public String name = "";
    public String nameonly = "";
    public int id = 0;
    public double earn = 0;
    public double paid = 0;

    public MyData(String name, String nameonly, int id) {
        this.name = name;
        this.nameonly = nameonly;
        this.id = id;
    }

    @Override
    public String toString() {
        return String.format("MyData [name=%s, nameonly=%s, id=%s, earn=%s, paid=%s]", name, nameonly, id, earn, paid);
    }
}

09-25 23:18