I am facing a serious problem now a days.
import java.io. * ;
import java.util. * ;
import java.text. * ;
import java.math. * ;
import java.util.regex. * ;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int d,q,x,y;
ArrayList[] set = new ArrayList[n];
for(int i=0;i < n;i++){
d = in.nextInt();
set[i] = new ArrayList();
for(int j=0;j < d;j++){
set[i].add(in.nextInt());
}
}
q=in.nextInt();
for(int i=0;i < q;i++){
x=in.nextInt();
y=in.nextInt();
try{
System.out.println(set[x-1].get(y-1));
} catch(Exception e){
System.out.println("ERROR!");
}
}
}
}
Above solution is correct and now I am giving the same solution with the different variable name of ArrayList and different Scanner object name I am getting a runtime error.
import java.io. * ;
import java.util. * ;
import java.text. * ;
import java.math. * ;
import java.util.regex. * ;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int d,x,y,q;
ArrayList[] a = new ArrayList[n];
for(int i=0;i < n;i++){
d = sc.nextInt();
a[i] = new ArrayList();
for(int j=0;j < d;j++){
a[i].add(sc.nextInt());
}
}
q=sc.nextInt();
for(int i=0;i < q;i++){
x=sc.nextInt();
y=sc.nextInt();
try{
System.out.println(a[x-1].get(y-1));
} catch(Exception e){
System.out.println("ERROR!");
}
}
}
}
and this Solution sometime gives Runtime Error and some time it gives correct output.
I really need to know the reason behind this issue.