繁体   English   中英

遍历两个数组

[英]Looping through two arrays

我有两个不同的数组,一个双精度的ArrayList和一个字符串的数组

 public class tester {

        private final static String TIME[]={ "8:00", "9:00", "10:00", "11:00", 
            "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00"  };


        public static void main(String[] args){
            ArrayList<Double> stat = new ArrayList<>();
            stat.add(1.0);
            stat.add(2.0);
            stat.add(3.0);
            stat.add(4.0);
            stat.add(5.0);
            stat.add(6.0);
            stat.add(7.0);
            stat.add(8.0);
            stat.add(9.0);
            stat.add(10.0);
            stat.add(11.0);
            stat.add(12.0);
            int i = 0;
            for (String time : TIME) {
                System.out.println(time+" "+stat.get(i));
                i++;
            }

我的问题很简单,如果我想获得每个数组的相同位置来匹配,这是遍历每个数组的最佳方法吗? 这样stat.get(0) ==TIME.get(0)吗?

更新资料

首先,感谢大家的快速响应,我喜欢创建类的想法,但是您需要了解一些知识。

您看到的是一个用于测试数据的测试类。

我知道两个数组的大小总是一样的,这是因为通常stat ArrayList的定义如下:

stat是从数据库中获得的数据的计算值,该stat的值基于时间,然后被发送回GUI以放入图形和表格中。

这意味着对于每个TIME都有一个现有值,因此stat.get(0)始终等于TIME.get(0)==“ 8:00”。

考虑到这一点,您是否仍然认为我应该创建一个类,还是应该让该类显示在下面,然后添加一个包含数据的HashMap,然后遍历该映射以将数据插入到我的GUI中?

public class Statistics {
    private ArrayList<CallQueue> queues = new ArrayList<CallQueue>();
    private ArrayList<Double> averageData = new ArrayList<Double>();
    private Provider p;

    public Statistics(){
         try {
            this.p = new Provider();
        } catch (DopeDBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * This recursive method calculates the average of each time table and then adds its to the arrayList in the following order:
     * 8.00  = 0
     * 9.00  = 1
     * 10.00 = 2
     * 11.00 = 3
     * 12.00 = 4
     * 13.00 = 5
     * 14.00 = 6
     * ect.
     * @param time
     */
    public void calculateAverage(){
        int data = 0;
        for (int i = 8; i < 20; i++) {
            for (CallQueue cq : queues) {
                data += cq.getCallsByTime(i);
            }
            if (data == 0) {
                Main.createErrorMessage("Fejl i dataen! \r\n kontakt venligst ansvarlige i Dope");
            }
            averageData.add((double) data/11);
        }
    }
    /**
     * @author MRCR
     * This method recives the object list from the database and sets the currentlist to the list recived.
     */
    public void setQueues(Calendar start, Calendar end){
        try {
            queues = p.getInformation(start, end, queues);
        } catch (DopeDBException e) {
            // TODO Auto-generated catch block
            Main.createErrorMessage("Message");
        } catch (DopeResultSetException e) {
            // TODO Auto-generated catch block
            Main.createErrorMessage("Message");
        }

    }
    /**
     * This method returns the calculated DataList list.
     * @author MRCR
     * @return averageData
     */
    public ArrayList<Double>getData(Calendar start, Calendar end){
        setQueues(start, end);
        calculateAverage();
        return averageData;
    }



}


import java.util.HashMap;


public class CallQueue {
    private String type;
    private HashMap<Integer, Integer> data = new HashMap<Integer,Integer>();
    public CallQueue(String type){
        this.type = type;
    }
    public String getType(){
        return type;
    }
    public int getCallsByTime(int time){
        return data.get(time);

    }
    public void addCallsByTime(int hourOfDay, int callAmount) {
        data.put(hourOfDay, callAmount);

    }

}

我首先要检查2个数组的长度是否相同。 然后使用for循环进行迭代:

final int timeLength = TIME.length;
if (timeLength != stat.size()) {
    //something may not be right
}
for (int i = 0; i < timeLength; i++) {
    System.out.println(time[i]+" "+stat.get(i));
}
for (int i = 0; i < TIME.length; i++) {
  // use i to access TIME[i] and stat.get(i)
}

但是您必须确保这些数组的长度相同

您还需要考虑长度部分。 您只需要迭代最大可能覆盖数组和列表的最大长度即可。

因此,您可以首先找到它们之间的较短长度。 然后迭代到该长度:-

int lower = TIME.length < stat.size()? TIME.length: stat.size();

for (int i = 0; i < lower; i++) {
    System.out.println(TIME[i] + " : " + stat.get(i);
}

现在,这是迭代两个数组的一部分。

现在,我要说的是 ,如果您必须同时迭代两个数组,则只需创建一个具有属性的class ,即可为其创建数组。

因此,创建具有属性的类: - time ,和stats 然后创建一个List<YourClass> 并遍历类实例的列表。

if (TIME.length!=stat.size()) {
    // handle error
}

int count = stat.size();
for (int i=0; i<count; ++i) {
    double d = stat.get(i);
    String s = TIME[i];
}

然而

如注释中所指出的,您应该定义一个将收集两个数组的信息的类。

例如:

公共类MyTime {私有double值; 私有字符串标签; }

要么

在那种特殊情况下,我怀疑您可以使用时间格式化功能来替换您的字符串数组。

String.format("%1$d:00", (int) myDouble);

暂无
暂无

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

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