嗨,伙计们,我有一个问题,获得正确的输出分配。我正在寻找一种方法来复制一组元素从一个并行数组(其中包含一个字符串-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,它将不返回任何值(void)。它会检查当前数组中的数字是否在新数组中,如果是,它会将任何重复的持续时间添加到当前持续时间中。如果没有,它将向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;
}
这个find方法将用于检查电话号码是否已经放在新数组中,如果是,则确定该号码的位置。
例如,如果数组中包含
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]);
}
}
正如您提到的,该方法的返回类型应该是void
,所以我假设您只需要打印总持续时间。因此,不需要构建像NewNumbers
或NewDurations
这样的新数组。
如果绝对有必要将它们保存在数组中,请在评论中告诉我。