Java 迭代两个数组,索引有问题

Java 迭代两个数组,索引有问题,java,arrays,Java,Arrays,我有一个关于创建动态数组和遍历这些数组的问题。我有一个方法setVoltage,它以字符串和双精度值作为参数。我需要一些方法来存储这些值,所以我创建了两个数组。我需要做的是遍历字符串数组,查看字符串参数是否已经存在,如果已经存在,则在该索引处设置相应的电压。如果它不存在,我需要将字符串设备添加到字符串数组中,并将双电压添加到双数组中。有人能检查一下我的代码,看看我遗漏了什么吗?我遇到了麻烦,因为我想让数组意识到字符串已经在数组中,或者将其附加到数组的末尾,但我一直在研究如何使用索引变量来实现这一

我有一个关于创建动态数组和遍历这些数组的问题。我有一个方法setVoltage,它以字符串和双精度值作为参数。我需要一些方法来存储这些值,所以我创建了两个数组。我需要做的是遍历字符串数组,查看字符串参数是否已经存在,如果已经存在,则在该索引处设置相应的电压。如果它不存在,我需要将字符串设备添加到字符串数组中,并将双电压添加到双数组中。有人能检查一下我的代码,看看我遗漏了什么吗?我遇到了麻烦,因为我想让数组意识到字符串已经在数组中,或者将其附加到数组的末尾,但我一直在研究如何使用索引变量来实现这一点。谢谢大家!

    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++;
            }
        }
}
public final int sizeArray=10;
私有字符串[]设备列表=新字符串[sizeArray];
public double[]voltList=新的double[sizeArray];
公共同步无效设置电压(串设备,双电压){
int指数=0;
for(int i=0;i
听起来你可能想要一张
地图。这将允许您存储由设备名称键入的电压值,并且您可以轻松查找、插入和删除设备。例如,见:


您可以通过替换数组中的
null
元素来添加新设备。您可以通过查找具有指定名称的设备并更改其
电压
字段来更新设备。您可以选择使用
列表
来避免硬编码的大小限制。

在这种情况下,最好使用
映射
。但是,如果仍要使用两个数组,则当前代码不会附加到数组的末尾。如果
i=0
设备列表[i]!=设备
,然后您的代码立即在
else
块中用新值覆盖
设备列表[i]
,而不是将其追加到数组的末尾。若要追加,必须将else部分中的代码移动到
for循环之后

如果找不到设备名称,您的代码将始终覆盖设备。如果您使用映射会更好,但是如果需要使用数组,您可以尝试以下方法

   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;
        }
    }
for(int i=0;i

您必须编写一些额外的代码来处理完整的数组。

我将这样做:

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;
}
公共无效设置或出现(串设备,双电压){
int指数=0;
对于(;索引

我建议在循环之外执行设置逻辑。此外,我还推荐这种场景的映射,因为这种逻辑更简单。映射将自动执行您要求的操作(如果存在则替换,否则追加)。

需要注意的一点是,如果任何其他代码正在维护对
设备列表
电压列表
(可能直接,可能通过
数组.asList()
)的引用,如果添加新元素,则不会修改这些引用的数组。这可能是可以接受的(OP没有将其标记为
最终版
)。使用可变容器,例如
列表
是避免这种情况的一种选择。@JasonC如果不放弃原始数组,则无法调整数组的大小。
static class Device {
    String name; 
    double voltage;
}

Device[] devices = new Device[10];
   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;
}