We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public static void main(String[] args){
List<List<String>> list = new ArrayList<>();
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
//Consume the token
scan.nextLine();
//Creating the 2D ArrayList
for (int i = 0; i < n; i++) {
String[] tempArr = scan.nextLine().split(" ");
List<String> tempList = new ArrayList<>();
for (int j = 0; j < tempArr.length; j++) {
tempList.add(tempArr[j]);
}
//add elements to 2D ArrayList
list.add(tempList);
} //2D ArrayList has been created
//Ask the number of queries to test
int q = scan.nextInt();
for (int i = 0; i < q; i++) {
int line = scan.nextInt()-1;
int index = scan.nextInt();
try {
System.out.println(list.get(line).get(index));
}catch (Exception e) {
System.err.println("ERROR!");
}
}
scan.close();
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Arraylist
You are viewing a single comment's thread. Return to all comments →
import java.util.ArrayList; import java.util.List; import java.util.Scanner;
public class ArrayListJava {
}