如何从 Java 中的并行数组中删除重复项?
How to remove duplicates from a parallel array in Java?
所以,我开始学习 Java 并且想知道如何从源数组中恰好存储一次字符串和 int 类型的并行数组。例如,我有两个彼此平行的数组,一个将 Phone 数字存储为字符串,另一个将调用持续时间存储为从每个 phone 数字获得的 a/an 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-0000";
callDurations[2] = 90;
phoneNumbers[3] = "888-678-8766";
callDurations[3] = 28;
size = 4;
我写了一个方法来查找特定 phone 号码的详细信息,例如特定呼叫“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");
这段代码的输出是:
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
然而,我想要得到的输出是:
All calls from number:
Calls from 888-555-1234:
888-555-1234 duration: 54s
Process finished with exit code 0
(26s + 28s)
如何在 java 中确保没有重复项存储在并行数组中并获取每个 phone 数字的总持续时间,而不是将它们单独放在数组中?
问题是:"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.utils.HashMap
。哈希映射是一种存储与特定键关联的值(任何类型)的概念。在您的情况下,值是持续时间,键是您的 phone 号码。因此,您应该在此处使用 String
-Integer
哈希映射。
在插入时执行以下操作:
- 对于每个 phone 数字持续时间对,请执行以下操作:
- 指定key的HashMap中是否已有元素?
- 否 -> 添加 phone 数量和持续时间
- 是 ->
- 获取存储的时长
- 将当前持续时间添加到存储的持续时间
- 用计算出的新持续时间覆盖现有项目
稍后您可以高效地执行查找。
A Map is an object that maps keys to values
在您的情况下,您希望 phone 个数字(存储在 String
中)对应于通话时长(int
秒)。因此,您将声明您的 HashMap
如下(注意 您不能实例化 Map
,它是一个接口):
Map<String, Integer> callRecords = new HashMap<String, Integer>();
这是一个更好的版本,因为您不再需要跟踪两个不同的数组。现在,而不是
phoneNumbers[0] = "888-555-0000";
callDurations[0] = 10;
你可以写:
callRecords.put("888-555-0000", 10);
正如之前的答案中所述,您可以使用地图 - 将避免 phoneNumber 和 callDuration (Java code to Prevent duplicate <Key,Value> pairs in HashMap/HashTable) 中的重复项。
或者,如果您想坚持使用 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");
}
所以,我开始学习 Java 并且想知道如何从源数组中恰好存储一次字符串和 int 类型的并行数组。例如,我有两个彼此平行的数组,一个将 Phone 数字存储为字符串,另一个将调用持续时间存储为从每个 phone 数字获得的 a/an 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-0000";
callDurations[2] = 90;
phoneNumbers[3] = "888-678-8766";
callDurations[3] = 28;
size = 4;
我写了一个方法来查找特定 phone 号码的详细信息,例如特定呼叫“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");
这段代码的输出是:
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
然而,我想要得到的输出是:
All calls from number:
Calls from 888-555-1234:
888-555-1234 duration: 54s
Process finished with exit code 0
(26s + 28s)
如何在 java 中确保没有重复项存储在并行数组中并获取每个 phone 数字的总持续时间,而不是将它们单独放在数组中?
问题是:"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.utils.HashMap
。哈希映射是一种存储与特定键关联的值(任何类型)的概念。在您的情况下,值是持续时间,键是您的 phone 号码。因此,您应该在此处使用 String
-Integer
哈希映射。
在插入时执行以下操作:
- 对于每个 phone 数字持续时间对,请执行以下操作:
- 指定key的HashMap中是否已有元素?
- 否 -> 添加 phone 数量和持续时间
- 是 ->
- 获取存储的时长
- 将当前持续时间添加到存储的持续时间
- 用计算出的新持续时间覆盖现有项目
稍后您可以高效地执行查找。
A Map is an object that maps keys to values
在您的情况下,您希望 phone 个数字(存储在 String
中)对应于通话时长(int
秒)。因此,您将声明您的 HashMap
如下(注意 您不能实例化 Map
,它是一个接口):
Map<String, Integer> callRecords = new HashMap<String, Integer>();
这是一个更好的版本,因为您不再需要跟踪两个不同的数组。现在,而不是
phoneNumbers[0] = "888-555-0000";
callDurations[0] = 10;
你可以写:
callRecords.put("888-555-0000", 10);
正如之前的答案中所述,您可以使用地图 - 将避免 phoneNumber 和 callDuration (Java code to Prevent duplicate <Key,Value> pairs in HashMap/HashTable) 中的重复项。
或者,如果您想坚持使用 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");
}