I have a question about creating dynamic arrays and iterating through these arrays. I have a method setVoltage that takes as parameters a string and a double value. I needed some way to store these values so I created two arrays. What i need to do is iterate through the String array to see if the string parameter already exists and if it does, to set the corresponding voltage at that index. If it doesn't exist, i need to add the string device to the string array and also add the double voltage to the double array. Can someone check out my code and see what I am missing? I'm running into trouble because I want the array to realize the string is already in the array, or append it to the end of the array but I'm stuck on how to use index variables to achieve this. Thank you!
我有一个关于创建动态数组和迭代这些数组的问题。我有一个方法setVoltage,它将参数作为字符串和double值。我需要一些方法来存储这些值,所以我创建了两个数组。我需要做的是遍历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++;
}
}
}
4 个解决方案
#1
2
It sounds like perhaps you want a Map<String,Double>
instead. This will let you store voltage values keyed by device name, and you can easily find, insert, and remove devices. See, for example, HashMap
:
听起来好像你想要一个Map
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");
Your example code then reduces to simply:
然后,您的示例代码简化为:
private Map<String,Double> deviceVoltages = new HashMap<String,Double>();
public synchronized void setVoltage(String device, double voltage) {
deviceVoltages.put(device, voltage);
}
Performance will exceed that of your array, and you will not have a hard-coded limit on number of devices.
性能将超过阵列的性能,并且您不会对设备数量进行硬编码限制。
See generic Map
documentation for links to other types of maps in the JDK.
有关JDK中其他类型映射的链接,请参阅通用映射文档。
See also ConcurrentHashMap
if a finer synchronization granularity is desired and/or acceptable.
如果需要和/或可接受更精细的同步粒度,另请参见ConcurrentHashMap。
An alternative, if you must use arrays, is to create a Device
class that encapsulates all the relevant information about a device and use a single array to simplify your logic, e.g.:
如果必须使用数组,另一种方法是创建一个Device类,它封装有关设备的所有相关信息,并使用单个数组来简化逻辑,例如:
static class Device {
String name;
double voltage;
}
Device[] devices = new Device[10];
You can add new devices by replacing a null
element in the array. You can update devices by finding the one with the specified name and changing its voltage
field. You can optionally use a List<Device>
to avoid hard-coded size limits.
您可以通过替换数组中的null元素来添加新设备。您可以通过查找具有指定名称的设备并更改其电压字段来更新设备。您可以选择使用List
#2
0
It would be way better to use a Map<String,Double>
in this scenario. But if you still want to use two arrays, your current code is not appending to the end of the array. Say if i=0
and deviceList[i] != device
, then your code immediately overwrites deviceList[i]
with the new value in the else
block rather than appending it to the end of the array. To append, you have to move the code in the else part to after the for loop
.
在这种情况下使用Map
#3
0
Your code will always overwrite the device if the device name was not found. It would be better if you used a Map but if you are required to use arrays you can try the following
如果找不到设备名称,您的代码将始终覆盖设备。如果你使用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;
}
}
You'll have to write some additional code to deal with a full array.
您将不得不编写一些额外的代码来处理完整的数组。
#4
0
This is how I would do it as-is:
我就是这样做的原样:
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;
}
I would recommend doing the set logic outside the loop. Additionally I would also recommend a Map for this kind of scenario since this kind of logic is simpler. A Map will already do what you are asking automatically (replace if exists otherwise append).
我建议在循环外做集合逻辑。此外,我还建议为这种情况使用Map,因为这种逻辑更简单。 Map会自动执行您要求的操作(如果存在则替换,否则添加)。
#1
2
It sounds like perhaps you want a Map<String,Double>
instead. This will let you store voltage values keyed by device name, and you can easily find, insert, and remove devices. See, for example, HashMap
:
听起来好像你想要一个Map
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");
Your example code then reduces to simply:
然后,您的示例代码简化为:
private Map<String,Double> deviceVoltages = new HashMap<String,Double>();
public synchronized void setVoltage(String device, double voltage) {
deviceVoltages.put(device, voltage);
}
Performance will exceed that of your array, and you will not have a hard-coded limit on number of devices.
性能将超过阵列的性能,并且您不会对设备数量进行硬编码限制。
See generic Map
documentation for links to other types of maps in the JDK.
有关JDK中其他类型映射的链接,请参阅通用映射文档。
See also ConcurrentHashMap
if a finer synchronization granularity is desired and/or acceptable.
如果需要和/或可接受更精细的同步粒度,另请参见ConcurrentHashMap。
An alternative, if you must use arrays, is to create a Device
class that encapsulates all the relevant information about a device and use a single array to simplify your logic, e.g.:
如果必须使用数组,另一种方法是创建一个Device类,它封装有关设备的所有相关信息,并使用单个数组来简化逻辑,例如:
static class Device {
String name;
double voltage;
}
Device[] devices = new Device[10];
You can add new devices by replacing a null
element in the array. You can update devices by finding the one with the specified name and changing its voltage
field. You can optionally use a List<Device>
to avoid hard-coded size limits.
您可以通过替换数组中的null元素来添加新设备。您可以通过查找具有指定名称的设备并更改其电压字段来更新设备。您可以选择使用List
#2
0
It would be way better to use a Map<String,Double>
in this scenario. But if you still want to use two arrays, your current code is not appending to the end of the array. Say if i=0
and deviceList[i] != device
, then your code immediately overwrites deviceList[i]
with the new value in the else
block rather than appending it to the end of the array. To append, you have to move the code in the else part to after the for loop
.
在这种情况下使用Map
#3
0
Your code will always overwrite the device if the device name was not found. It would be better if you used a Map but if you are required to use arrays you can try the following
如果找不到设备名称,您的代码将始终覆盖设备。如果你使用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;
}
}
You'll have to write some additional code to deal with a full array.
您将不得不编写一些额外的代码来处理完整的数组。
#4
0
This is how I would do it as-is:
我就是这样做的原样:
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;
}
I would recommend doing the set logic outside the loop. Additionally I would also recommend a Map for this kind of scenario since this kind of logic is simpler. A Map will already do what you are asking automatically (replace if exists otherwise append).
我建议在循环外做集合逻辑。此外,我还建议为这种情况使用Map,因为这种逻辑更简单。 Map会自动执行您要求的操作(如果存在则替换,否则添加)。