Tech Jargon - Decoded
Ir al canal en Telegram
Confused by tech terms? Don’t worry, we’ve got you 🤝 We make things simple, one concept at a time. Learn daily Easy & clear Turn Confusion into clarity. #tech #it #softwareengineer #cs #development
Mostrar más2 017
Suscriptores
Sin datos24 horas
-77 días
-4030 días
Archivo de publicaciones
Selection Sort on an Array
public class SelectionSort {
public static void main(String[] args) {
int[] arr = {64, 25, 12, 22, 11};
selectionSort(arr);
System.out.println("Sorted array:");
printArray(arr);
}
static void selectionSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
static void printArray(int[] arr) {
for (int i = 0; i < arr.length; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
}Bubble Sort on an Array
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {5, 1, 4, 2, 8};
bubbleSort(arr);
printArray(arr);
}
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}Binary Search in a Sorted Array
public class BinarySearch {
public static int binarySearch(int[] arr, int target) {
int low = 0;
int high = arr.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
public static void main(String[] args) {
int[] arr = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};
int target = 23;
int index = binarySearch(arr, target);
if (index == -1) {
System.out.println("Element not found");
} else {
System.out.println("Element found at index: " + index);
}
}
}Linear Search in an Array
public class LinearSearch {
public static int linearSearch(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
int[] numbers = {5, 10, 15, 20, 25};
int searchElement = 15;
int index = linearSearch(numbers, searchElement);
if (index == -1) {
System.out.println("Element not found.");
} else {
System.out.println("Element found at index: " + index);
}
}
}Find the Second Largest Element in Array
public class SecondLargest {
public static void main(String[] args) {
int[] arr = {12, 35, 1, 10, 34, 1};
int secondLargest = findSecondLargest(arr);
System.out.println("Second largest element: " + secondLargest);
}
public static int findSecondLargest(int[] arr) {
if (arr.length < 2) {
return -1;
}
int largest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;
for (int num : arr) {
if (num > largest) {
secondLargest = largest;
largest = num;
} else if (num > secondLargest && num != largest) {
secondLargest = num;
}
}
if (secondLargest == Integer.MIN_VALUE) {
return -1;
}
return secondLargest;
}
}Check if an Array is a Palindrome
public class PalindromeArray {
public static boolean isPalindrome(int[] arr) {
int left = 0;
int right = arr.length - 1;
while (left < right) {
if (arr[left] != arr[right]) {
return false;
}
left++;
right--;
}
return true;
}
public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 2, 1};
int[] arr2 = {1, 2, 3, 4, 5};
System.out.println(isPalindrome(arr1));
System.out.println(isPalindrome(arr2));
}
}Transpose of a Matrix
public class MatrixTranspose {
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}};
int rows = matrix.length;
int cols = matrix[0].length;
int[][] transpose = new int[cols][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}
System.out.println("Original Matrix:");
printMatrix(matrix);
System.out.println("Transpose Matrix:");
printMatrix(transpose);
}
public static void printMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
}Sum of Rows and Columns in a 2D Matrix
public class MatrixSum {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int rows = matrix.length;
int cols = matrix[0].length;
int[] rowSum = new int[rows];
int[] colSum = new int[cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
rowSum[i] += matrix[i][j];
colSum[j] += matrix[i][j];
}
}
System.out.println("Row Sums:");
for (int i = 0; i < rows; i++) {
System.out.println("Row " + (i + 1) + ": " + rowSum[i]);
}
System.out.println("
Column Sums:");
for (int j = 0; j < cols; j++) {
System.out.println("Column " + (j + 1) + ": " + colSum[j]);
}
}
}2D Array Traversal
public class TwoDArrayTraversal {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.out.println("Elements of the 2D array:");
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}Prefix Sum of Array
public class PrefixSum {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int[] prefixSum = calculatePrefixSum(arr);
for (int i = 0; i < prefixSum.length; i++) {
System.out.print(prefixSum[i] + " ");
}
}
public static int[] calculatePrefixSum(int[] arr) {
int n = arr.length;
int[] prefixSum = new int[n];
prefixSum[0] = arr[0];
for (int i = 1; i < n; i++) {
prefixSum[i] = prefixSum[i - 1] + arr[i];
}
return prefixSum;
}
}Reverse an Array
public class ReverseArray {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int n = arr.length;
for (int i = 0; i < n / 2; i++) {
int temp = arr[i];
arr[i] = arr[n - 1 - i];
arr[n - 1 - i] = temp;
}
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}Find Maximum and Minimum in an Array
public class MinMaxArray {
public static void main(String[] args) {
int[] numbers = {5, 2, 9, 1, 5, 6};
int min = numbers[0];
int max = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] < min) {
min = numbers[i];
}
if (numbers[i] > max) {
max = numbers[i];
}
}
System.out.println("Minimum: " + min);
System.out.println("Maximum: " + max);
}
}Declaring and Initializing Arrays in Java
public class ArrayInitialization {
public static void main(String[] args) {
int[] numbers = new int[5];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
System.out.println("First element: " + numbers[0]);
String[] names = {"Alice", "Bob", "Charlie"};
System.out.println("Second name: " + names[1]);
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.out.println("Element at [1][2]: " + matrix[1][2]);
}
}👨💻Happy to hear that many of you have been using the Interactive Code Explainer for getting code explaination, and I’d love to hear your thoughts! 💬
What did you like? What could be made better?
Feel free to share your feedback by dropping a comment on my LinkedIn post 👇
🔗 https://www.linkedin.com/posts/sai-pradeep-875742268_codelearning-webdevelopment-reactjs-activity-7350843865475506176-BG9S
Every suggestion helps to make the tool smarter, smoother, and more helpful for everyone! 🚀
Sorting: Arranging the elements in a particular order (e.g., ascending or descending).
- Filtering: Creating a new array with elements that meet certain criteria.
- Aggregation: Calculating sums, averages, or other statistical measures.
**6. Important Considerations ⚠️**
- ArrayIndexOutOfBoundsException: This occurs if you try to access an element using an index that is outside the valid range (0 to length - 1). Avoid it by checking the array's length before accessing elements.
- Arrays have a fixed size: Once created, you can't easily resize them. If you need a dynamic size, consider using ArrayList (from the Collections framework).
- Arrays in Java are objects: They are created using the `new` keyword.
**7. Best Practices ✅**
- Initialize arrays when you declare them to avoid null pointer exceptions.
- Use meaningful variable names for your arrays.
- When working with 2D arrays, make sure to loop through all rows and columns correctly.
- Use enhanced for loops when modifying array element is not required.
**8. Tip 💡**
- Practice, practice, practice! The more you work with arrays, the more comfortable you'll become with them. Try solving coding problems that involve arrays to strengthen your skills.
Arrays are powerful tools for organizing and managing data in Java. With a good understanding of their concepts and best practices, you'll be well-equipped to tackle a wide range of programming challenges! Happy coding! 💻
Okay, let's dive into the world of Arrays in Java! 🚀 Think of arrays as organized containers for storing multiple values of the same type. We'll cover both 1D (one-dimensional) and 2D (two-dimensional) arrays.
Arrays are fundamental to Java programming because they allow us to efficiently manage and manipulate collections of data.
**1. What is an Array? 🧠**
An array is a data structure that holds a fixed-size sequential collection of elements of the same data type. Imagine it like a row of boxes, where each box can hold one item.
- All elements in an array must be of the same type (e.g., all integers, all strings).
- Arrays have a fixed size, determined when you create them. You can't easily change the size later.
- Each element is accessed using its index, starting from 0.
**2. 1D Arrays (Single Row of Boxes) ➡️**
A 1D array is the simplest form of an array. It's like a single row of boxes.
*Declaration:*
int[] numbers; // Declares an array of integers named 'numbers'
String[] names; // Declares an array of strings named 'names'
*Initialization:*
numbers = new int[5]; // Creates an array that can hold 5 integers.
names = new String[3]; // Creates an array that can hold 3 strings.
*Combining Declaration and Initialization:*
int[] numbers = new int[5]; // Creates an array of 5 integers, all initialized to 0.
String[] names = new String[3]; // Creates an array of 3 strings, all initialized to null.
*Initializing with values directly:*
int[] numbers = {1, 2, 3, 4, 5};
String[] names = {"Alice", "Bob", "Charlie"};
*Accessing elements:*
int firstNumber = numbers[0]; // Accesses the first element (index 0). firstNumber will be 1.
String secondName = names[1]; // Accesses the second element (index 1). secondName will be "Bob".
*Modifying elements:*
numbers[0] = 10; // Changes the value of the first element to 10.
names[2] = "David"; // Changes the value of the third element to "David".
*Array Length:*
You can find the length of an array using the `.length` property:
int arrayLength = numbers.length; // arrayLength will be 5
**3. 2D Arrays (Grid of Boxes) ➡️**
A 2D array is like a grid or a table with rows and columns. It's essentially an array of arrays.
*Declaration:*
int[][] matrix; // Declares a 2D array of integers named 'matrix'
*Initialization:*
matrix = new int[3][4]; // Creates a 2D array with 3 rows and 4 columns.
*Combining Declaration and Initialization:*
int[][] matrix = new int[3][4]; // Creates a 3x4 2D array, all elements initialized to 0.
*Initializing with values directly:*
int[][] matrix = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
*Accessing elements:*
int element = matrix[0][2]; // Accesses the element at row 0, column 2. element will be 3.
*Modifying elements:*
matrix[1][3] = 20; // Changes the value at row 1, column 3 to 20.
*Array Length:*
int numberOfRows = matrix.length; // numberOfRows will be 3 (number of rows)
int numberOfColumns = matrix[0].length; // numberOfColumns will be 4 (number of columns in the first row)
**4. Looping Through Arrays 🔄**
You'll often need to iterate through the elements of an array to perform operations. Use `for` loops for this:
*1D Array:*
int[] numbers = {10, 20, 30, 40, 50};
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}
*2D Array:*
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int i = 0; i < matrix.length; i++) { // Loop through rows
for (int j = 0; j < matrix[i].length; j++) { // Loop through columns in the current row
System.out.print(matrix[i][j] + " ");
}
System.out.println(); // Move to the next line after printing each row
}
**5. Array Operations ⚙️**
Arrays are used for various operations:
- Searching: Finding a specific element in the array.
-Object Class and Method Overriding (toString, equals, hashCode)
class Point {
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "Point(" + x + ", " + y + ")";
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Point point = (Point) obj;
return x == point.x && y == point.y;
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + x;
result = 31 * result + y;
return result;
}
public static void main(String[] args) {
Point p1 = new Point(1, 2);
Point p2 = new Point(1, 2);
Point p3 = new Point(3, 4);
System.out.println("p1: " + p1);
System.out.println("p2: " + p2);
System.out.println("p1 equals p2: " + p1.equals(p2));
System.out.println("p1 equals p3: " + p1.equals(p3));
System.out.println("p1 hashCode: " + p1.hashCode());
System.out.println("p2 hashCode: " + p2.hashCode());
System.out.println("p3 hashCode: " + p3.hashCode());
}
}Understanding the 'final' Keyword in Java OOP
final class FinalClass {
final int finalVariable = 10;
final void finalMethod() {
System.out.println("This is a final method.");
}
}
class SubClass {
public static void main(String[] args) {
FinalClass obj = new FinalClass();
System.out.println("Final Variable: " + obj.finalVariable);
obj.finalMethod();
}
}Static Variables, Blocks, and Methods in OOP
class StaticExample {
static int count = 0;
static {
System.out.println("Static block initialized.");
}
public StaticExample() {
count++;
}
public static int getCount() {
return count;
}
public static void main(String[] args) {
System.out.println("Count before objects: " + StaticExample.getCount());
StaticExample obj1 = new StaticExample();
StaticExample obj2 = new StaticExample();
System.out.println("Count after objects: " + StaticExample.getCount());
}
}Encapsulation with Private Fields and Getters/Setters
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age >= 0) {
this.age = age;
} else {
System.out.println("Age cannot be negative.");
}
}
public static void main(String[] args) {
Person person = new Person("Alice", 30);
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
person.setAge(31);
System.out.println("Updated Age: " + person.getAge());
person.setAge(-5);
}
}
¡Ya disponible! Investigación de Telegram 2025 — los principales insights del año 
