0% found this document useful (0 votes)
10 views50 pages

ArrayList notes

Complete arraylist(Java)

Uploaded by

Jiya Gaba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views50 pages

ArrayList notes

Complete arraylist(Java)

Uploaded by

Jiya Gaba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

2D Arrays &

ArrayLists
Today’s checklist
1. Basic of 2D Arrays
2. Problems based on 2D Arrays
3. 2D ArrayList
4. ArrayList vs Arrays
5. Basic STL functions of ArrayList
6. Problem based on 2-D ArrayList
What and Why?

Array - collection of same data type


~ Linear List

is matrix
2D Array - is a
grid ,
it
Representation of 2D array
Declaration of a 2-Dimensional
Array
int[][] arr = new int[4][2];

int[][] arr = {{1,2},{3,4},{5,6},{7,8}};

In Java, in 2D Arrays, if we are directly initializing it, then we do not


mention the no. of rows and columns, but if we are only declaring and
allocating the memory, then we need to mention both the rows and
columns.
Array of arrays
Initialisation of a 2-Dimensional
Array
int[][] arr = { { 1234, 56 }, { 1256, 43 }, { 1434, 32 }, { 1312, 96 } } ;

int[][] arr = { {12, 34, 56}, { 78, 91, 23} } ;


Traversal through 2D array
Taking 2D array as input from
the user
Ques:
Q1 : Write a program to store roll number and marks
obtained by 4 students side by side in a matrix.
Ques:
Q2 : Write a JAVA program to find the largest element of a
given 2D array of integers.
Ques:
Q3 : Write a program to print sum of all the elements of a
2D matrix.
Ques:
Q4 : Write a program to add two matrices.
Ques:
Q5 : Write a program to print the transpose of the matrix
entered by the user and store it in a new matrix.
Ques:
Q5 : Write a program to print the transpose of the matrix
entered by the user and store it in a new matrix.
Ques:
Q5 : Write a program to print the transpose of the matrix
entered by the user and store it in a new matrix.
Ques:
Q5 : Write a program to print the transpose of the matrix
entered by the user and store it in a new matrix.
Ques:
Q6 : Write a program to change the given matrix with its
transpose.

[Leetcode 867]
-
Ques:
Q7 : Write a program to rotate the matrix by 90 degrees
clockwise.

[Leetcode 48]
Ques:
Q7 : Write a program to rotate the matrix by 90 degrees
clockwise.

[Leetcode 48]
Ques:
Q8 : Write a program to print the matrix in wave form.

0 1 2
1 2 3
0
4 5 6

1
7 8 9

2
0
Ques:
Q9 : Write a program to print the matrix in spiral form.

0 1 2
1 2 3
0
4 5 6

1
7 8 9

2
0
[Leetcode 54]
2D ArrayList
● List<List<Integer>> v = new ArrayList<>();
● List<List<Integer>> v = new ArrayList<>(m);
List<List<Integer>> v = new ArrayList<>(m);
for (int i = 0; i < m; i++) {
v.add(new ArrayList<>(n));
}
Advantages of ArrayList over
arrays
Basic STL functions in ArrayList
● add()
● remove()
● get()
● size()
● clear()
● isEmpty()
● Contains()
● indexOf()
● toArray()
·
10

3 ot
01 2
(40 503 a = 23d = 2003
a =
<10 ,
20
, 303 b =
,

e =
2210 ,
20 , 303 ,
suoisos 33,
, 25033
Ques:
Q10 : Given an integer ‘numRows’, generate Pascal's
triangle.

[Leetcode 118]
Ques:
Q11 : Write a program to print the multiplication of two
matrices given by the user.
Ques:
Q11 : Write a program to print the multiplication of two
matrices given by the user.
Ques:
Q12 : Score after flipping matrix .

[Leetcode 861]
Ques:
Q12 : Score after flipping matrix .

[Leetcode 861]
Ques: 01 L 3

Q12 : Score after flipping matrix . 32 2


O

2
E
32
111 I

I W O I

+ I 11 I
-

[Leetcode 861]
Ques:
Q13 : Write an efficient algorithm that searches for a value target
in an m x n integer matrix which has the following properties :
● Integers in each row are sorted in ascending from left to right.

● Integers in each column are sorted in ascending from top to


bottom.

[Leetcode 240]
Ques:
Q14 : Given an m x n integer matrix matrix, if an element is
0, set its entire row and column to 0's.

2
0
[Leetcode 73]
THANK YOU

You might also like