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.
- Prepare
- Java
- Data Structures
- Java Arraylist
- Discussions
Java Arraylist
Java Arraylist
+ 0 comments I use only 1 array list and work much with index, my solution: import java.io.; import java.util.;
public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); ArrayList<Integer> arrayList = new ArrayList<>(); for (int i = 0; i < n; i++) { int d = sc.nextInt(); arrayList.add(d); for (int ii = 0; ii < d; ii++) { int dd = sc.nextInt(); arrayList.add(dd); } } int q = sc.nextInt(); for (int i = 0; i < q; i++) { int x = sc.nextInt(); int y = sc.nextInt(); int chiso = 0; for (int ii = 0; ii < n; ii++) { // đang xét dòng thứ ii int d = arrayList.get(chiso); if (ii + 1 == x) { if (d < y) { System.out.println("ERROR!"); } else { System.out.println(arrayList.get(chiso + y)); } break; } else { chiso += d; chiso += 1; } } } }
}
+ 0 comments I've been exploring Java ArrayLists recently, and it's fascinating how they dynamically resize! Speaking of dynamic changes, I recently stumbled upon some incredible artwork during my visit to Dubai. One particular masterpiece, the Dubai wall painting, left me in awe of the city's vibrant art scene.
+ 0 comments 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) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); ArrayList<ArrayList>list=new ArrayList<ArrayList>(); for(int i=0;i<n;i++) { int limit=sc.nextInt(); ArrayList<Integer>list2=new ArrayList<Integer>(); for(int j=0;j<limit;j++) { list2.add(sc.nextInt()); } list.add(list2); } int q=sc.nextInt(); for(int i=0;i<q;i++) { int x=sc.nextInt(); int y=sc.nextInt(); try{ System.out.println((list.get(x-1)).get(y-1)); } catch(Exception e) { System.out.println("ERROR!"); } } }
}
+ 0 comments public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); ArrayList<ArrayList> arr = new ArrayList(); for(int i = 0; i < n; i++) { ArrayList subArr = new ArrayList(); int d = sc.nextInt(); for(int j = 0; j < d; j++) { int k = sc.nextInt(); subArr.add(k); } arr.add(subArr); } int q = sc.nextInt(); for(int i = 0; i < q; i++) { int x = sc.nextInt(); int y = sc.nextInt(); try { System.out.println((arr.get(x-1)).get(y-1)); } catch (IndexOutOfBoundsException e) { System.out.println("ERROR!"); } } } }
+ 0 comments import java.io.; import java.util.;
public class Solution {
public static void main(String[] args) { List<List<Integer>> list = new ArrayList<>(); Scanner sc= new Scanner(System.in); int n = sc.nextInt(); for(int i = 0; i< n; i++){ int d = sc.nextInt(); List<Integer>list1 = new ArrayList<>(); for(int j=0;j<d;j++){ list1.add(sc.nextInt()); } list.add(list1); } int q = sc.nextInt(); // for query for(int i = 0; i< q; i++){ int x = sc.nextInt(); int y = sc.nextInt(); try{ System.out.println(list.get(x-1).get(y-1)); } catch(Exception ex){ System.out.println("ERROR!"); } } }
}
Load more conversations
Sort 718 Discussions, By:
Please Login in order to post a comment