简体   繁体   English

如何从Java中的并行数组中删除重复项?

[英]How to remove duplicates from a parallel array in Java?

So, I started learning Java and was wondering how parallel arrays of string and int type could be stored exactly once from the source arrays. 因此,我开始学习Java,并想知道如何将字符串和int类型的并行数组从源数组中存储一次。 For example, I have two arrays parallel to each other, one stores the Phone number as a string and the other stores the duration of the calls as a/an int gotten from each phone number. 例如,我有两个彼此平行的数组,一个数组将电话号码存储为字符串,另一个数组将通话时间存储为从每个电话号码获得的整数。

String[] phoneNumbers;           
    phoneNumbers = new String[100];
    int[] callDurations = new int[phoneNumbers.length];
    int size = 0;

    phoneNumbers[0] = "888-555-0000";
    callDurations[0] = 10;
    phoneNumbers[1] = "888-555-1234";
    callDurations[1] = 26;
    phoneNumbers[2] = "888-555-0000";
    callDurations[2] = 90;
    phoneNumbers[3] = "888-678-8766";
    callDurations[3] = 28;

    size = 4;

I wrote a method to find the details of a specific phone number, such as the duration of the specific call "888-555-1234" Here is the method and how I called it: 我编写了一种方法来查找特定电话号码的详细信息,例如特定呼叫的持续时间“ 888-555-1234”。这是该方法及其调用方式:

public static void findAllCalls(String[] phoneNumbers, int[] callDurations, int size, String targetNumber) {
    int match;
    System.out.println("Calls from " + targetNumber + ":");
    match = find(phoneNumbers, size, 0, targetNumber);

    while (match >= 0) {
        System.out.println(phoneNumbers[match] + " duration: " + callDurations[match] + "s");

        match = find(phoneNumbers, size, match + 1, targetNumber);

    }
}

System.out.println("\n\nAll calls from number: ");
    findAllCalls(phoneNumbers, callDurations, size, "888-555-1234");

The output of this code is: 此代码的输出是:

All calls from number: 
Calls from 888-555-1234:
888-555-1234 duration: 26s
888-555-1234 duration: 28s

Process finished with exit code 0

Whereas,the output I want to get instead is: 而我想获得的输出是:

All calls from number: 
Calls from 888-555-1234:
888-555-1234 duration: 54s


Process finished with exit code 0

(26s + 28s) (26s + 28s)

How is it possible in java to make sure there are no duplicates stored in a parallel array and get total duration for each phone number instead of having them separately in the arrays? 在Java中如何确保并行数组中没有重复存储并获取每个电话号码的总持续时间,而不是将它们分别存储在数组中?

The question was: "How is it possible in java to make sure there are no duplicates stored in a parallel array and get total duration for each phone number instead of having them separately in the arrays?" 问题是:“在Java中如何确保并行数组中没有重复存储,并获得每个电话号码的总持续时间,而不是将它们分别存储在数组中?”

The answer is: There is no (inexpensive) way. 答案是:没有(便宜的)方法。

Use a hash map instead. 请改用哈希图。 Have a look at java.utils.HashMap . 看看java.utils.HashMap A hash map is a concept to store values (of any kind) associated to a specific key. 哈希映射是用于存储与特定键关联的(任何类型的)值的概念。 In your case the values would be the durations, the keys would be your phone number. 在您的情况下,值将是持续时间,键将是您的电话号码。 Therefor you should use a String - Integer hash map here. 因此,您应该在此处使用String - Integer哈希映射。

On insert do the following: 在插入时执行以下操作:

  • For each phone number-duration pair do: 对于每个电话号码-持续时间对,请执行以下操作:
    • Is there already an element in the HashMap of the specified key? HashMap中已存在指定键的元素吗?
    • No -> Add phone number and duration 否->添加电话号码和时长
    • Yes -> 是->
      • Get the duration stored 获取存储的持续时间
      • Add the current duration to the stored duration 将当前持续时间添加到存储的持续时间
      • Overwrite the existing item with the new duration calculated 用计算出的新持续时间覆盖现有项目

Later you efficiently can perform a lookup. 稍后,您可以有效地执行查找。

A Map is an object that maps keys to values 映射是将键映射到值的对象

In your case, you want phone numbers (stored in a String ) to correspond to call duration ( int s). 在您的情况下,您希望电话号码(存储在String )对应于通话时间( int )。 Therefore, you'd declare your HashMap as follows ( Note you can't instantiate Map , it is an interface): 因此,您需要按以下方式声明HashMap请注意,您无法实例化Map ,它是一个接口):

Map<String, Integer> callRecords = new HashMap<String, Integer>();

This is a better version because you no longer need to keep track of two different arrays. 这是一个更好的版本,因为您不再需要跟踪两个不同的阵列。 Now, instead of 现在,代替

phoneNumbers[0] = "888-555-0000";
callDurations[0] = 10;

You can write: 你可以写:

callRecords.put("888-555-0000", 10);

As already stated in the answers before, you can use a map - will avoid duplicates in both phoneNumber and callDuration ( Java code to Prevent duplicate <Key,Value> pairs in HashMap/HashTable ). 如之前的答案中所述,您可以使用地图-避免在phoneNumber和callDuration中重复Java代码可防止HashMap / HashTable中的<Key,Value>对重复 )。

Or, if you want to stick with the String implementation, you can change the logic in the findAllCalls() method. 或者,如果您想坚持使用String实现,则可以在findAllCalls()方法中更改逻辑。

public static void findAllCalls(String[] phoneNumbers, int[] callDurations, int size, String targetNumber) 
{
   int match;
   System.out.println("Calls from " + targetNumber + ":");
   //match = find(phoneNumbers, size, 0, targetNumber);
   int i = 0, duration = 0;
   while (i<size)
    {
        if(phoneNumbers[i].equals(targetNumber))
            duration+=callDurations[i];
        i++;
      //System.out.println(phoneNumbers[match] + " duration: " + callDurations[match] + "s");
      //match = find(phoneNumbers, size, match + 1, targetNumber);
   }
   System.out.println(targetNumber+" duration : "+duration+"s");
}

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

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