简体   繁体   English

使用 HashMap/表或矩阵有什么区别?

[英]Which is the difference between using a HashMap/ a table or a Matrix?

I am trying to import a table in which different data type is stored.我正在尝试导入存储不同数据类型的表。

数据想介绍

As you can see same nº has several rows and I want to have access to each one as the column but I am not sure if the best strategy to manipulate data in Java is using tables.正如您所看到的,相同的 nº 有几行,我想访问每一行作为列,但我不确定在 Java 中操作数据的最佳策略是否使用表。

Which is the difference between using a HashMap/ a table or a Matrix?使用 HashMap/表或矩阵有什么区别?

I would make a class for n, like this:我会为n创建一个类,如下所示:

public class nDegree{ 
    private ArrayList<Integer> prevAct;  //For example, you would add 2 and 4 to this list if your object was for n = 3 etc.
    private ArrayList<Integer> timeRel;
    private ArrayList<Integer> frequency;

    public nDegree(ArrayList<Integer> prevAct, ArrayList<Integer> timeRel, ArrayList<Integer> frequency) {
        this.prevAct = prevAct;
        this.timeRel = timeRel;
        this.frequency = frequency;
    }

    public ArrayList<Integer> getPrevAct() {
        return prevAct;
    }

    public void setPrevAct(ArrayList<Integer> prevAct) {
        this.prevAct = prevAct;
    }
    public ArrayList<Integer> getTimeRel() {
        return timeRel;
    }
    public void setTimeRel(ArrayList<Integer> timeRel) {
        this.timeRel = timeRel;
    }
    public ArrayList<Integer> getFrequency() {
        return frequency;
    }
    public void setFrequency(ArrayList<Integer> frequency) {
        this.frequency = frequency;
    }

}

Then you could create an object of that for each entry, and save it into a HashMap by it's n value, like this:然后你可以为每个条目创建一个对象,并通过它的 n 值将其保存到 HashMap 中,如下所示:

HashMap<Integer,NDegree> vals = new HashMap<Integer,NDegree>();

NDegree one = new NDegree(pList,tList,fList);

vals.put(1,one);

etc.. Then to access each element by it's n value you can just do something like this:等等。然后要通过它的 n 值访问每个元素,您可以执行以下操作:

ArrayList<Integer> oneFrequency = vals.get(<n value>).getFrequency();

If the N values are always sequential, you could also use a List for holding the objects:如果 N 值始终是连续的,您还可以使用 List 来保存对象:

ArrayList<NDegree> vals = new ArrayList<NDegree>();

vals.add(nOne);
...

Then access a particular one like this:然后像这样访问一个特定的:

ndList.get(<n-value>);

Or you can iterate over each value for each N entry, and iterate over the map to access all of the N values or whatever you need.或者,您可以遍历每个 N 条目的每个值,并遍历映射以访问所有 N 个值或您需要的任何值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM