如何将并行数组中的元素复制到 Java 中没有重复项的新并行数组中?

How do I copy elements in a parallel array into a new parallel array with no duplicates in Java?

大家好,我在获取作业的正确输出时遇到了问题。我正在寻找一种方法将一组元素从一个并行数组(包含一个字符串--int)复制到另一个没有重复值的元素。例如: 我有这些并行数组

这是原始的平行阵列:

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-1234";
    callDurations[2] = 2;
    size = 3; 

我想创建一个方法,从原始数组 (phoneNumber & callDuration) 中创建新的平行数组对。此方法将称为 totalDuration,它 return 没有值(空白)。它会检查当前数组中的数字是否在新数组中,如果是,它只会将任何重复的持续时间添加到当前持续时间。如果不是,它将向 NewNumber 数组添加一个新元素,并向 NewDuration 数组添加一个元素。

public static int find(String[] list, int size, int start, String target) {
    int pos = start;

    while (pos < size && !target.equals(list[pos])) {
        pos++;
    }

    if (pos == size)
        pos = -1;

    return pos;
}

此查找方法将用于检查 phone 数字是否已放入新数组中,如果是,则确定该数字的位置。

例如,如果数组包含

    phoneNumbers[0] = "888-555-0000";
    callDurations[0] = 10;
    phoneNumbers[1] = "888-555-1234";
    callDurations[1] = 26;
    phoneNumbers[2] = "888-555-1234";
    callDurations[2] = 2;

打印“888-555-1234”的通话详细信息如下所示:

    all calls from: 
Calls from 888-555-1234:
888-555-1234 duration: 26s
888-555-1234 duration: 2s

新方法的输出应该是(26s +2s):

    all calls from: 
Calls from 888-555-1234:
888-555-1234 duration: 28s

我试图用这段代码解决它,但它给出了错误的输出:

public static void totalDurations(String[] phoneNumbers, int[] callDuration, int size, String target) {
    String[] NewNumbers;
    int[] NewDuration;
    int pos;

    NewNumbers = new String[phoneNumbers.length];
    NewDuration = new int[callDuration.length];
    pos = find(phoneNumbers,size, 0,target);
    while(pos < size && !target.equals(phoneNumbers[pos])) {
        NewNumbers[pos] = phoneNumbers[pos];
        NewDuration[pos] = callDuration[pos];
        System.out.println(NewNumbers[pos] + "duration" + NewDuration[pos] +"s");
    }

}

不相关 我用来获取每次调用的所有详细信息的代码是我的方法 "findAllCalls"

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


        matchPos = find(phoneNumbers, size, matchPos + 1, targetNumber);
    }
}

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

如有任何更正,将不胜感激。

根据你的问题,打印目标数字的总持续时间的方法可以如下所示

    public static int numberIndex(String[] numbers, String target) {
        for(int i = 0; i < numbers.length; i++) {
            if(numbers[i].equals(target)) {
                return i;
            }
        }

        return -1;
    }

    public static void totalDuration(String[] phoneNumbers, int[] callDurations, String target) {
        String[] newNumbers = new String[phoneNumbers.length];
        int[] newDurations = new int[callDurations.length];
        int newIndex = 0;

        for(int i = 0; i < phoneNumbers.length; i++) {
            int oldIndex = numberIndex(newNumbers, phoneNumbers[i]);

            if(oldIndex == -1) {
                newNumbers[newIndex] = phoneNumbers[i];
                newDurations[newIndex] = callDurations[i];
                newIndex++;
            }
            else {
                newDurations[oldIndex] += callDurations[i];
            }
        }

        for(int i = 0; i < newIndex; i++) {
            System.out.println("Total duration for " + newNumbers[i] + ": " + newDurations[i]);
        }
    }

正如您提到的,此方法的 return 类型应为 void,因此我假设您只需要打印总持续时间。为此,无需构造新数组,如 NewNumbersNewDurations

如果绝对有必要将它们保存在数组中,请在评论中告诉我。