Search code examples
javalisttuplesindexoutofboundsexceptionjava-pair-rdd

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. 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. But I can store null if the values are not there.


Solution

  • 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 know the second list doesn't have more values but I want to store null in that case. For example

    Assuming that wtpmList is always bigger then (or equal to) wtpList, you can simply iterate over the smallest list first:

    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)));
        }