Arrays Flashcards
1
Q
What are the 3 ways to instantiate an array?
A
int[] arr1 = new int[3]; int[] arr2 = { 1, 2, 3 }; int[] arr3 = new int[] { 1, 2, 3 };
2
Q
What are the 2 ways to instantiate multidimensional arrays?
A
int[][] matrix = new [1][2]; int[][] matrix2 = {{1,2}};
3
Q
What class could you use to create a stream out of an array?
A
int[][] matrix = {{1,2,3}}; Arrays.stream(matrix)...
4
Q
- What data structure is used to implement varargs?
- Code an example using varargs
A
- arrays
void sum(int... args) { Arrays.stream(args).sum(); }
5
Q
How can an array be converted to a List
?
A
Integer[] arr = { 1, 2, 3 }; List<Integer> list = Arrays.asList(arr); List<Integer> list2 = List.of(arr); System.out.println(list); // [1, 2, 3] System.out.println(list2); // [1, 2, 3]
6
Q
What will the code bellow print?
int[] nums = {1,2,3}; int[] nums2 = {1,2,3}; System.out.println(nums == nums2); System.out.println(Arrays.equals(nums, nums2));
A
- False
- True