CodingInJava

Nothing much here, Just basic Java Coding that I’m wanting to learn from years!

Learning the basic program problems from here
Feel free to visit my portfolio from here
Follow me on LinkedIn: in/AVidhanR


The Below codes, sure are handy

// if return type is List<Integer>
return Arrays.asList(a, b);

I’m lazy, so I did this

import java.util.Arrays;
// gives the sum!
int s = Arrays.stream(array).sum();

In 2D matrix, the below code is handy

// for traversing and other element level operations
for (int[] rows : matrix) {
    for (int ele : rows) {
        sum += ele;
    }
}

While using String’s as char’s the below code is handy,

for (char c : v.toCharArray()) {
    // something here...    
}

To find the length of an array,

int[] arr = new int[] {10, 11, 12, 13};
int lengthOfArray = arr.length;

To find the length of a number,

int n = 1234;
int l = String.valueOf(n).length(); // 4

Typical git-cli things,

git add . && git commit -m "" && git push
# Fetch the latest changes from the remote repository
git fetch origin
git pull origin main

The below code check

// I ain't understood a thing the below code
for (int t = n; t > 0; t /= 2) {
    b = (t % 2) + b;
}

// so I used the below abstract method
return Integer.toBinaryString(num);

Well, if you want to return int values as an array of int for example; the below code is handy,

return new int[] {1, 2, 3}; // or {a, b, c} if they have data init.

The find command sure is handy