List of values of a single data type stored in a continous memory location
-
Arrays are immutable in size
-
Default value is 0
-
Access an index not present in array - Index out of bounds
-
Continous memory
- Advantage - Access of any value in array is a constant time operation
- Disadvanage - Size of array is fixed
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
System.out.println(a[i]);
}
- Sum of all values in array
int sum = 0;
for(int i = 0; i < n; i++) {
sum = sum + a[i];
}
System.out.println(sum);
-
Print all even number in array
-
Print all prime numbers in array
-
Largest element and its index
int a[] = {8, 4, 9, 3, 5, 4, 1, 9, 7};
int n = a.length;
int mx = Integer.MIN_VALUE;
int mxi = -1;
for(int i = 0; i < n; i++) {
if(a[i] > mx) {
mx = a[i];
mxi = i;
}
}
System.out.println(mx + " " + mxi);
- Can be equal to largest
int smx = Integer.MIN_VALUE;
int smxi = -1;
for(int i = 0; i < n; i++) {
if(i == mxi) {
continue;
}
if(a[i] > smx) {
smx = a[i];
smxi = i;
}
}
System.out.println(smx + " " + smxi);
- Cannot be equal to largest
int smx = Integer.MIN_VALUE;
int smxi = -1;
for(int i = 0; i < n; i++) {
if(a[i] == mx) {
continue;
}
if(a[i] > smx) {
smx = a[i];
smxi = i;
}
}
System.out.println(smx + " " + smxi);
A small continous part of array in the same order as in array
- Print all subarrays of the array
int a[] = {8, 4, 9, 3, 2};
int n = a.length;
for(int st = 0; st < n; st++) {
for(int ed = st; ed < n; ed++) {
for(int i = st; i <= ed; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
}
- Print the sum of each subarray
for(int st = 0; st < n; st++) {
for(int ed = st; ed < n; ed++) {
int sum = 0;
for(int i = st; i <= ed; i++) {
sum = sum + a[i];
}
System.out.println(st + " " + ed + " " + sum);
}
}
// ----------------------------------------------
for(int st = 0; st < n; st++) { // O(n^2)
int sum = 0;
for(int ed = st; ed < n; ed++) {
sum = sum + a[ed];
System.out.println(st + " " + ed + " " + sum);
}
}
- Find the subrray with the largest sum
int a[] = {8, -4, -9, 3, 2};
int n = a.length;
int mx = Integer.MIN_VALUE;
for(int st = 0; st < n; st++) {
for(int ed = st; ed < n; ed++) {
int sum = 0;
for(int i = st; i <= ed; i++) {
sum = sum + a[i];
}
if(sum > mx) {
mx = sum;
}
}
}
System.out.println(mx);
- Building
int mx = Integer.MIN_VALUE;
for(int i = 0; i < n; i++) {
if(a[i] > mx) {
mx = a[i];
}
}
int p = mx;
for(int i = 1; i <= mx; i++) {
for(int j = 0; j < n; j++) {
if(a[j] >= p) {
System.out.print("*\t");
}
else {
System.out.print("\t");
}
}
System.out.println();
p--;
}
- int a[][] = new int[row][col];
- int n = a.length;
- int m = a[0].length;
int a[][] = new int[4][3];
for(int r = 0; r < 4; r++) {
for(int c = 0; c < 3; c++) {
a[r][c] = sc.nextInt();
}
}
for(int r = 0; r < 4; r++) {
for(int c = 0; c < 3; c++) {
System.out.print(a[r][c] + " ");
}
System.out.println();
}
- Pair in array having sum k
for(int r = 0; r < n; r++) {
int sum = 0;
for(int c = 0; c < m; c++) {
sum += a[r][c];
}
System.out.println(r + " " + sum);
}
for(int c = 0; c < m; c++) {
int sum = 0;
for(int r = 0; r < n; r++) {
sum += a[r][c];
}
System.out.println(c + " " + sum);
}
// Max in each row
// Max in each col
// Max in entire matrix
- int -> Integer, long -> Long, String, double -> Double, float -> Float
add()
size()
get()
set()
size()
remove()
isEmpty()
clear()
lastIndexOf()
IndexOf
toArray
ArrayList<Integer> a = new ArrayList<Integer>();
a.add(10);
System.out.println(a);
a.add(20);
a.add(30);
System.out.println(a);
int n = a.size();
System.out.println(n);
a.set(2, 50); // a[2] = 50;
System.out.println(a);
get
System.out.println(a.get(1)); // a[1]
for(int i = 0; i < a.size(); i++) {
System.out.println(a.get(i));
}
remove
a.remove(1); // O(n)
System.out.println(a);
ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> row = new ArrayList<Integer>();
row.add(10);
row.add(20);
a.add(row);
ArrayList<Integer> row2 = new ArrayList<Integer>();
row2.add(10);
row2.add(20);
a.add(row2);
System.out.println(a);
System.out.println(a.get(0));
System.out.println(a.get(0).get(1));
for(int i = 0; i < a.size(); i++) {
for(int j = 0; j < a.get(i).size(); j++) {
System.out.print(a.get(i).get(j)); // a[i][j]
}
System.out.println();
}
ArrayList<Integer> row3 = new ArrayList<Integer>();
row3.add(30);
row3.add(50);
a.set(1, row3);
System.out.println(a);
- Sorting
int a[] = {2, 4, 1, 9, 2, 5, 4};
int n = a.length;
Arrays.sort(a);
int l = 0;
int h = n-1;
while(l < h) {
int t = a[l];
a[l] = a[h];
a[h] = t;
l++;
h--;
}
for(int i = 0; i < n; i++) {
System.out.println(a[i]);
}