简体   繁体   English

如果其中一个值为空,如何获取 Java 中的元组? 索引出界

[英]How to get the Tuples in Java, if one of the Value is empty? IndexOutOfBound

public class App {
    public static void main(String[] args) {
   
    List<Pair<String, String>> SubPartandMaster = new ArrayList<Pair<String, String>>();
        

    List<String> wtpmList = new ArrayList<String>();
    wtpmList.add("1");
    wtpmList.add("2");
    wtpmList.add("3");
    wtpmList.add("4");
    wtpmList.add("5");
   
    List<String> wtpList = new ArrayList<String>();
    wtpList.add("a");
    wtpList.add("b");
    wtpList.add("c");

    System.out.println(wtpmList);
    System.out.println(wtpList);
    for (int i = 0; i < wtpmList.size(); i++) {
        SubPartandMaster.add(new Pair<String, String>(wtpmList.get(i), wtpList.get(i)));
    }

    }
}

I know the second list doesn't have more values but I want to store null in that case.我知道第二个列表没有更多的值,但在这种情况下我想存储null For example例如

[(1,a),(2,b),(3,c),(4,null),(5,null)]

Instead of this, I get error而不是这个,我得到错误

[1, 2, 3, 4, 5]
[a, b, c]
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3
        at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
        at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
        at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
        at java.base/java.util.Objects.checkIndex(Objects.java:359)
        at java.base/java.util.ArrayList.get(ArrayList.java:427)
        at App.main(App.java:28)

I know the reason as I cannot retrieve more from wtpList as it has 3 elements.我知道原因,因为我无法从 wtpList 中检索更多内容,因为它有 3 个元素。 But I can store null if the values are not there.但是如果值不存在,我可以存储 null 。

You iterate over two list of different sizes:您遍历两个不同大小的列表:

for (int i = 0; i < wtpmList.size(); i++) {
    SubPartandMaster.add(new Pair<String, String>(wtpmList.get(i), wtpList.get(i)));
}

one has five elements, the other three.一个有五个元素,其他三个。 This leads to java.lang.IndexOutOfBoundsException as soon as i=3 .一旦i=3 ,这就会导致java.lang.IndexOutOfBoundsException

I know the second list doesn't have more values but I want to store null in that case.我知道第二个列表没有更多的值,但在这种情况下我想存储 null 。 For example例如

Assuming that wtpmList is always bigger then (or equal to) wtpList, you can simply iterate over the smallest list first:假设 wtpmList 总是大于(或等于)wtpList,您可以简单地先遍历最小的列表:

for (int i = 0; i < wtpList.size(); i++) {
    SubPartandMaster.add(new Pair<String, String>(wtpmList.get(i), wtpList.get(i)));
}

and add the nulls, after:并在之后添加空值:

for (int i = wtpList.size(); i < wtpmList.size(); i++) {
    SubPartandMaster.add(new Pair<String, String>(wtpmList.get(i), null));
}

Not assuming anything about the list then you have to change your code to cover all the cases.不对列表做任何假设,那么您必须更改代码以涵盖所有情况。

  int biggest_list = Math.max(wtpmList.size(), wtpList.size());
  for (int i = 0; i < biggest_list; i++) {
       if(i >= wtpmList.size())
          SubPartandMaster.add(new Pair<String, String>(null, wtpList.get(i)));
       else if (i >= wtpList.size())
          SubPartandMaster.add(new Pair<String, String>(wtpmList.get(i), null));
       else
          SubPartandMaster.add(new Pair<String, String>(wtpmList.get(i), wtpList.get(i)));
    }

You could override get so that it works like poll and return a null value if no element is present:您可以覆盖get以便它像poll一样工作,如果不存在任何元素,则返回 null 值:

List<String> wtpList = new ArrayList<String>() {
  @Override
  public String get( int idx ) {
    return( idx < size() ? super.get( idx ) : null );
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM