[英]Iterating through two arrays, having trouble with indices
我有一个关于创建动态数组和迭代这些数组的问题。 我有一个方法setVoltage,它将参数作为字符串和双精度值。 我需要一些方法来存储这些值,所以我创建了两个数组。 我需要做的是迭代String数组以查看字符串参数是否已经存在,如果存在,则设置该索引处的相应电压。 如果它不存在,我需要将字符串设备添加到字符串数组,并将双电压添加到双数组。 有人可以看看我的代码,看看我错过了什么? 我遇到了麻烦,因为我希望数组实现字符串已经在数组中,或者将它附加到数组的末尾,但我仍然坚持如何使用索引变量来实现这一点。 谢谢!
public final int sizeArray = 10;
private String[] deviceList = new String[sizeArray];
public double[] voltList = new double[sizeArray];
public synchronized void setVoltage(String device, double voltage) {
int index = 0;
for(int i = 0; i < deviceList.length; i++ ){
//if the device name already exists in the device array, overwrite voltage at that index
if(this.deviceList[i].equals(device)){
index = i;
voltList[index] = voltage;
}else{
//set deviceList[i] equal to device, set voltList[i] equal to voltage
deviceList[i] = device;
voltList[i] = voltage;
index++;
}
}
}
听起来好像你想要一个Map<String,Double>
。 这将允许您存储按设备名称键入的电压值,您可以轻松查找,插入和删除设备。 例如,参见HashMap
:
Map<String,Double> deviceVoltages = new HashMap<String,Double>();
deviceVoltages.put("liquifier", 8.4);
deviceVoltages.put("reflarbulator", 900.0);
deviceVoltages.put("liquifier", 13.3); // replaces previous 8.4
System.out.println(deviceVoltages.get("liquifier"));
deviceVoltages.remove("reflarbulator");
然后,您的示例代码简化为:
private Map<String,Double> deviceVoltages = new HashMap<String,Double>();
public synchronized void setVoltage(String device, double voltage) {
deviceVoltages.put(device, voltage);
}
性能将超过阵列的性能,并且您不会对设备数量进行硬编码限制。
有关JDK中其他类型映射的链接,请参阅通用Map
文档。
如果需要和/或可接受更精细的同步粒度,另请参见ConcurrentHashMap
。
如果必须使用数组,另一种方法是创建一个Device
类,它封装有关Device
所有相关信息,并使用单个数组来简化逻辑,例如:
static class Device {
String name;
double voltage;
}
Device[] devices = new Device[10];
您可以通过替换数组中的null
元素来添加新设备。 您可以通过查找具有指定名称的设备并更改其voltage
字段来更新设备。 您可以选择使用List<Device>
来避免硬编码大小限制。
在这种情况下使用Map<String,Double>
会更好。 但是,如果您仍想使用两个数组,则当前代码不会附加到数组的末尾。 假设i=0
且deviceList[i] != device
,那么您的代码会立即用else
块中的新值覆盖deviceList[i]
,而不是将其附加到数组的末尾。 要追加,您必须将else部分中的代码移动到for loop
。
如果找不到设备名称,您的代码将始终覆盖设备。 如果您使用Map会更好,但如果您需要使用数组,则可以尝试以下操作
for(int i = 0; i < deviceList.length; i++ ){
//if the device name already exists in the device array, overwrite voltage at that index
if(this.deviceList[i].equals(device)){
voltList[i] = voltage;
break;
}else if (deviceList[i] == null) {
//set deviceList[i] equal to device, set voltList[i] equal to voltage
deviceList[i] = device;
voltList[i] = voltage;
break;
}
}
您将不得不编写一些额外的代码来处理完整的数组。
我就是这样做的原样:
public void setOrAppend(String device, double voltage) {
int index = 0;
for ( ; index < deviceList.length; i++) {
if (device.equals(deviceList[index]) || deviceList[index] == null) {
break;
}
}
if (index == deviceList.length) {
deviceList = Arrays.copyOf(deviceList, deviceList.length + 1);
voltList = Arrays.copyOf(voltList, voltList.length + 1);
}
deviceList[index] = device;
voltList[index] = voltage;
}
我建议在循环外部进行设置逻辑。 此外,我还建议为这种情况使用Map,因为这种逻辑更简单。 Map会自动执行您要求的操作(如果存在则替换,否则添加)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.