java - 如何在并行数组中添加相似名称的数据

标签 java

一直试图找出如何仅添加相同数字的数据,以便它产生和输出像这样

总持续时间 调用 555-555-5555: 555-555-5555 持续时间:90s

调用 555-555-1234: 555-555-1234 持续时间:56s

调用 555-555-9876: 555-555-9876 持续时间:35秒

 public class Activity0D {
       public static void main(String[] args) {
          String[] phoneNumbers = new String[100];
          int[] callDurations = new int[phoneNumbers.length];
          int size = 0;

          size = addCall(phoneNumbers, callDurations, size, "555-555-5555", 40);
          size = addCall(phoneNumbers, callDurations, size, "555-555-5555", 20);
          size = addCall(phoneNumbers, callDurations, size, "555-555-5555", 30);
          size = addCall(phoneNumbers, callDurations, size, "555-555-1234", 26);
          size = addCall(phoneNumbers, callDurations, size, "555-555-1234", 30);
          size = addCall(phoneNumbers, callDurations, size, "555-555-9876", 35);

          System.out.println("Phone numbers (initially):");
          printList(phoneNumbers, callDurations, size);


          System.out.println("\nTotal Duration");


          System.out.println("\nEnd of processing.");
       }

  public static void findAllCalls(String[] phoneNumbers, int[] callDurations, int size, String targetNumber) 
   {
      int matchPos;

      System.out.println("Calls from " + targetNumber + ":");
      matchPos = find(phoneNumbers, size, 0, targetNumber);
      while (matchPos >= 0) {
         System.out.println(phoneNumbers[matchPos] + " duration: " + callDurations[matchPos] + "s");

         // Find the next match, starting after the last one
         matchPos = find(phoneNumbers, size, matchPos + 1, targetNumber);
      }
   }

   public static void totalDurations(String[] phoneNumbers, int[] callDurations, int size)
   {
     int totalDuration = 0;
     for (int i = 0; i < size; i++)
     {
       if(find(phoneNumbers, size, 0, "555-555-5555") >= 0)
       {  //Add data the total duration for number "555-555-5555"

       }
       else if(find(phoneNumbers, size, 0, "555-555-1234") >= 0)
       {     //Add data the total duration for number "555-555-1234"
       }
       else if(find(phoneNumbers, size, 0, "555-555-9876") >= 0)
       {  //Add data the total duration for number "555-555-9876"
       }
     }
   }


    }

最佳答案

因此,如果您的电话号码格式正确匹配(并且没有一百万个号码需要检查 - 那么您应该使用数据库或购买足够的内存或其他东西;-)) 您可以使用哈希,因为它的访问时间为 O(1),请参阅 https://en.m.wikipedia.org/wiki/Big_O_notation

class Phones {
    Map<String, Integer> phones;
    int totals;

    Phones() {
        phones = new HashMap<String, Integer>();
        totals = 0;
    }

    public void addCall(String number, int duration) {
        int sumDuration = duration;
        totals += duration;
        if (phones.containsKey(number) {
            sumDuration += phones.get(number).intValue();
        }
        phones.put(number, Integer.valueOf(sumDuration));
    }

    public Map<String, Integer>getPhones() {
        return phones;
    }
    public int getTotals() {
        return totals;
    }
}
<小时/>

您可以像这样使用此类

void addPhones() {
    Phones phones = new Phones();
    phones.addCall("555-555-5555", 40);
    phones.addCall("555-555-5555", 20);
    phones.addCall("555-555-5555", 30);
    phones.addCall("555-555-1234", 26);
...

例如phones.getPhones().get("555-555-1234") 您将获得一个号码的持续时间结果,并使用 phones.getTotals() 您可以立即获得总计。

关于java - 如何在并行数组中添加相似名称的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32545434/

相关文章:

java - Spring Boot 和 Spring Data MongoDB : hiding fields in a ResponseBody

java - 如何使用 Java 将包含代码和不同 namespace 的 XML 转换为对象?

java - 发送和接收包含 double 的 DatagramPacket

java - 在 Hsqldb/H2 中进行单元测试的 SequenceGenerator 问题

java - 将 Java DOM 文档序列化为 XML : Add CData Elements

java - Hibernate ConstraintViolationException : could not insert, SQL 错误

java - 仅使用 AspectJ 将切面应用于带注释的方法(不使用 Spring)

java - java代码性能分析工具

java - 如何找到图像的主色?

JAVA If else 如果用户输入 1 = 0 则退出的进程不起作用