1

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. 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:

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)

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?

3
  • Just. Use. A. Map. Commented Sep 20, 2015 at 20:55
  • You're probably going to want to use a different data structures; arrays aren't enough for you. Learn about classes that implement the Set interface in Java docs.oracle.com/javase/tutorial/collections/interfaces/set.html Although, being that you want there to be a relationship between two entities, a Map may be of more use to you: docs.oracle.com/javase/tutorial/collections/interfaces/map.html Commented Sep 20, 2015 at 20:55
  • Okay noted,thanks. I wanted to know if there's a way to create a method that creates a new pair of parallel arrays (phone numbers and durations, again) where each different incoming phone number is stored exactly once, and the duration is the total duration of all the calls from that phone number so It then prints these arrays. a void method perhaps? Commented Sep 20, 2015 at 21:12

3 Answers 3

0

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).

Or, if you want to stick with the String implementation, you can change the logic in the findAllCalls() method.

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");
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, i would like to stick to string implementation as I am new to Mapping. There seems to be a problem with your code. it doesn't print out the total duration, instead it prints out the first matching phone number and duration.
there's still a problem. It doesn't add the two durations. Instead, it prints out the second matching phone number and duration.
Tested against more cases, getting correct output. Try a dry run of your code, there might be a typo.
0

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?"

The answer is: There is no (inexpensive) way.

Use a hash map instead. Have a look at 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.

On insert do the following:

  • For each phone number-duration pair do:
    • Is there already an element in the HashMap of the specified key?
    • 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.

Comments

0

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 (ints). Therefore, you'd declare your HashMap as follows (Note you can't instantiate Map, it is an interface):

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.